Imrpove code formatting.

communication
junos 2020-12-31 14:33:44 +01:00
parent f542a37955
commit 98f945add1
2 changed files with 75 additions and 26 deletions

View File

@ -1,11 +1,22 @@
from datetime import datetime from datetime import datetime
from sqlalchemy import Column, String, Boolean, \
Integer, SmallInteger, BigInteger, \ from sqlalchemy import (
Float, TIMESTAMP, \ TIMESTAMP,
ForeignKey, UniqueConstraint BigInteger,
Boolean,
Column,
Float,
ForeignKey,
Integer,
SmallInteger,
String,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import ARRAY as PSQL_ARRAY
from sqlalchemy.dialects.postgresql import INTEGER as PSQL_INTEGER
from sqlalchemy.dialects.postgresql import JSONB as PSQL_JSONB
from sqlalchemy.dialects.postgresql import TEXT as PSQL_TEXT
from sqlalchemy.ext.declarative import declarative_base, declared_attr from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.dialects.postgresql import ARRAY as PSQL_ARRAY, \
JSONB as PSQL_JSONB, TEXT as PSQL_TEXT, INTEGER as PSQL_INTEGER
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
Base = declarative_base() Base = declarative_base()
@ -31,10 +42,10 @@ class Participant(Base):
tester = Column(Boolean, index=True, nullable=False, default=False) tester = Column(Boolean, index=True, nullable=False, default=False)
def __repr__(self): def __repr__(self):
return 'Participant()' return "Participant()"
def __str__(self): def __str__(self):
return '<Participant {}>'.format(self.username) return "<Participant {}>".format(self.username)
class AWAREsensor(object): class AWAREsensor(object):
@ -49,18 +60,25 @@ class AWAREsensor(object):
@declared_attr @declared_attr
def participant_id(self): def participant_id(self):
return Column(Integer, ForeignKey('participants.id'), nullable=False, index=True) return Column(
Integer, ForeignKey("participants.id"), nullable=False, index=True
)
@declared_attr @declared_attr
def participant(self): def participant(self):
return relationship('Participant', lazy="select", backref=self.__tablename__) return relationship("Participant", lazy="select", backref=self.__tablename__)
@declared_attr @declared_attr
def __table_args__(self): def __table_args__(self):
return UniqueConstraint('device_id', '_id', name=self.__tablename__ + "_twice"), return (
# I think it makes more sense to create a Constraint on device_id and _id rather than relate it to participant_id. UniqueConstraint("device_id", "_id", name=self.__tablename__ + "_twice"),
)
# I think it makes more sense to create a Constraint
# on device_id and _id rather than relate it to participant_id.
# _id is a primary key, auto incremented by AWARE. # _id is a primary key, auto incremented by AWARE.
# However, I would expect it to reset back to 1 if the application was reinstalled, # However, I would expect it to reset back to 1
# if the application was reinstalled,
# similarly to how device_id resets. # similarly to how device_id resets.
@ -82,8 +100,8 @@ class GoogleAR(Base, AWAREsensor):
class Application(Base, AWAREsensor): class Application(Base, AWAREsensor):
__tablename__ = "applications" __tablename__ = "applications"
# package_name = Column(String, nullable=False) # package_name = Column(String, nullable=False)
# application_name = Column(String) # application_name = Column(String)
package_hash = Column(String(64), nullable=False) package_hash = Column(String(64), nullable=False)
play_store_genre = Column(String, nullable=True) play_store_genre = Column(String, nullable=True)
is_system_app = Column(Boolean) is_system_app = Column(Boolean)
@ -97,7 +115,8 @@ class Barometer(Base, AWAREsensor):
class BarometerSensor(Base, AWAREsensor): class BarometerSensor(Base, AWAREsensor):
__tablename__ = "barometer_sensor" __tablename__ = "barometer_sensor"
# Since this table is not really important, I will leave all columns as nullable. (nullable=True by default.) # Since this table is not really important,
# I will leave all columns as nullable. (nullable=True by default.)
double_sensor_maximum_range = Column(Float) double_sensor_maximum_range = Column(Float)
double_sensor_minimum_delay = Column(Float) double_sensor_minimum_delay = Column(Float)
sensor_name = Column(String) sensor_name = Column(String)
@ -222,8 +241,8 @@ class NetworkTraffic(Base, AWAREsensor):
class Notification(Base, AWAREsensor): class Notification(Base, AWAREsensor):
__tablename__ = "notifications" __tablename__ = "notifications"
# package_name = Column(String, nullable=False) # package_name = Column(String, nullable=False)
# application_name = Column(String, nullable=False) # application_name = Column(String, nullable=False)
package_hash = Column(String(64), nullable=False) package_hash = Column(String(64), nullable=False)
play_store_genre = Column(String, nullable=True) play_store_genre = Column(String, nullable=True)
sound = Column(String, nullable=False) sound = Column(String, nullable=False)
@ -268,7 +287,8 @@ class Temperature(Base, AWAREsensor):
class TemperatureSensor(Base, AWAREsensor): class TemperatureSensor(Base, AWAREsensor):
# I left all of these nullable, as we haven't seen any data from this sensor anyway. # I left all of these nullable,
# as we haven't seen any data from this sensor anyway.
__tablename__ = "temperature_sensor" __tablename__ = "temperature_sensor"
double_sensor_maximum_range = Column(Float) double_sensor_maximum_range = Column(Float)
double_sensor_minimum_delay = Column(Float) double_sensor_minimum_delay = Column(Float)
@ -293,9 +313,31 @@ class WiFi(Base, AWAREsensor):
label = Column(String, nullable=True) label = Column(String, nullable=True)
all_AWARE_tables = [ESM, Location, Screen, LightSensor, Call, SMS, Application, Notification, Battery, WiFi, Proximity, all_AWARE_tables = [
Timezone, Processor, NetworkData, NetworkTraffic, Barometer, BarometerSensor, Temperature, ESM,
TemperatureSensor, Bluetooth, Accelerometer, GoogleAR, Speech] Location,
Screen,
LightSensor,
Call,
SMS,
Application,
Notification,
Battery,
WiFi,
Proximity,
Timezone,
Processor,
NetworkData,
NetworkTraffic,
Barometer,
BarometerSensor,
Temperature,
TemperatureSensor,
Bluetooth,
Accelerometer,
GoogleAR,
Speech,
]
all_AWARE_table_names = [table.__tablename__ for table in all_AWARE_tables] all_AWARE_table_names = [table.__tablename__ for table in all_AWARE_tables]
@ -312,5 +354,12 @@ class AppCategories(Base):
package_hash = Column(String(64), index=True, nullable=False, unique=True) package_hash = Column(String(64), index=True, nullable=False, unique=True)
play_store_genre = Column(String, nullable=True) play_store_genre = Column(String, nullable=True)
play_store_response = Column(SmallInteger, nullable=False) play_store_response = Column(SmallInteger, nullable=False)
number_of_attempts = Column(SmallInteger, nullable=False, default=0, onupdate=increment_one) number_of_attempts = Column(
last_attempt = Column(TIMESTAMP(timezone=False), nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) SmallInteger, nullable=False, default=0, onupdate=increment_one
)
last_attempt = Column(
TIMESTAMP(timezone=False),
nullable=False,
default=datetime.utcnow,
onupdate=datetime.utcnow,
)

View File

@ -3,7 +3,7 @@ import unittest
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from config.models import Participant, LightSensor from config.models import LightSensor, Participant
from setup import db_uri from setup import db_uri
@ -30,4 +30,4 @@ class DatabaseConnection(unittest.TestCase):
def test_get_light_data(self): def test_get_light_data(self):
light_0 = self.session.query(Participant).join(LightSensor).first() light_0 = self.session.query(Participant).join(LightSensor).first()
self.assertIsNotNone(light_0) self.assertIsNotNone(light_0)
print(light_0) print(light_0)