class Access(db.Model): __tablename__ = "access" user_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True) class_id = db.Column(db.Integer, db.ForeignKey("class.id"), primary_key=True)
class Class_Student(db.Model): __tablename__ = "class_student" roll_id = db.Column(db.Integer, db.ForeignKey("class.id"), primary_key=True) student_id = db.Column(db.Integer, db.ForeignKey("student.id"), primary_key=True)
class Roll_Student(db.Model): __tablename__ = "roll_student" extend_existing = True roll_id = db.Column(db.Integer, db.ForeignKey("roll.id"), primary_key=True) student_id = db.Column(db.Integer, db.ForeignKey("student.id"), primary_key=True) present = db.Column(db.Boolean, default=False) marked_at = db.Column(db.DateTime, default=None)
class Experiment(CRUDMixin, db.Model): id = db.Column(db.Integer, primary_key=True) hardware = db.Column(db.Text) t_stamp = db.Column(db.DateTime) label = db.Column(db.Text) sensors = db.relationship('Sensor', backref='experiment', lazy='dynamic') def __repr__(self): return '<Experiment %r>, <id %r>' % (self.hardware, self.id)
class Site(CRUDMixin, db.Model): __tablename__ = 'tracking_site' base_url = db.Column(db.String) visits = db.relationship('Visit', backref='site', lazy='select') user_id = db.Column(db.Integer, db.ForeignKey('users_user.id')) def __repr__(self): return '<Site {:d} {}>'.format(self.id, self.base_url) def __str__(self): return self.base_url
class Class(db.Model): __tablename__ = "class" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50)) desc = db.Column(db.String(200)) users = db.relationship("Access", backref="class", lazy="dynamic") students = db.relationship("Class_Student", backref="class", lazy="dynamic") def __repr__(self): return "<Class: {}>".format(self.title)
class Roll(db.Model): __tablename__ = "roll" id = db.Column(db.Integer, primary_key=True) time = db.Column(db.DateTime, default=datetime.now()) class_id = db.Column(db.Integer) roll = db.relationship("Roll_Student", backref="roll", lazy="dynamic") linked_rfid = db.relationship("Station", back_populates="linked_roll_rel") def __repr__(self): return "<Roll object Student: {} is in Class: {}>".format( self.student_id, self.class_id, )
class Student(db.Model): __tablename__ = "student" id = db.Column(db.Integer, primary_key=True) student_id = db.Column(db.Integer) student_name = db.Column(db.String(50)) rfid = db.Column(db.Integer) roll = db.relationship("Roll_Student", backref="student", lazy="dynamic") def __repr__(self): return "<Student, id: {}, name: {}, dbId: {}>".format( self.student_id, self.student_name, self.id, )
class User(UserMixin, CRUDMixin, db.Model): __tablename__ = 'users_user' name = db.Column(db.String(50)) email = db.Column(db.String(120), unique=True) _password = db.Column(db.LargeBinary(120)) _salt = db.Column(db.String(120)) sites = db.relationship('Site', backref='owner', lazy='dynamic') @hybrid_property def password(self): return self._password # In order to ensure that passwords are always stored # hashed and salted in our database we use a descriptor # here which will automatically hash our password # when we provide it (i. e. user.password = "******") @password.setter def password(self, value): # When a user is first created, give them a salt if self._salt is None: self._salt = bytes(SystemRandom().getrandbits(128)) self._password = self._hash_password(value) def is_valid_password(self, password): """Ensure that the provided password is valid. We are using this instead of a ``sqlalchemy.types.TypeDecorator`` (which would let us write ``User.password == password`` and have the incoming ``password`` be automatically hashed in a SQLAlchemy query) because ``compare_digest`` properly compares **all*** the characters of the hash even when they do not match in order to avoid timing oracle side-channel attacks.""" new_hash = self._hash_password(password) return compare_digest(new_hash, self._password) def _hash_password(self, password): pwd = password.encode("utf-8") salt = bytes(self._salt) rounds = current_app.config.get("HASH_ROUNDS", 100000) buff = pbkdf2_hmac("sha512", pwd, salt, iterations=rounds) return bytes(buff) def __repr__(self): return "<User #{:d}>".format(self.id)
class Visit(CRUDMixin, db.Model): __tablename__ = 'tracking_visit' browser = db.Column(db.String) date = db.Column(db.DateTime) event = db.Column(db.String) url = db.Column(db.String) ip_address = db.Column(db.String) location = db.Column(db.String) latitude = db.Column(db.Numeric) longitude = db.Column(db.Numeric) site_id = db.Column(db.Integer, db.ForeignKey('tracking_site.id')) def __repr__(self): r = '<Visit for site ID {:d}: {} - {:%Y-%m-%d %H:%M:%S}>' return r.format(self.site_id, self.url, self.date)
class User(db.Model, UserMixin): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), nullable=True) password_hash = db.Column(db.String(128)) logins = db.Column(db.Integer, default=0) classes = db.relationship("Access", backref="user", lazy="dynamic") def __repr__(self): return '<User {}>'.format(self.username) def set_password(self, password): """Runs the passwords through a hash and appends.""" self.password_hash = generate_password_hash(str(password)) def check_password(self, password): """Checks a password against the hash.""" return check_password_hash(self.password_hash, password)
class Photo(Base,CRUDMixin): __tablename__='photo' uuid = db.Column(db.String(128), nullable=False) filepath = db.Column(db.String(500), nullable=False) analysed = db.Column(db.Boolean,default=False) info = db.Column(db.String(1000),nullable=False) def __init__(self,uuid,filepath,analysed=False,info=""): self.uuid = uuid self.filepath = filepath self.analysed=analysed self.info=info def get_id(self): return self.id def serialize(self): return {'uuid':self.uuid} def serialize_all(self): return {'uuid':self.uuid, 'filepath': self.filepath, 'analysed': self.analysed, 'info': self.info} def __repr__(self): return '<photo uuid=%r filepath=%r>' % (self.uuid,self.filepath)
class Item(db.Model): __tablename__ = "items" id = db.Column(db.Integer, nullable=False) site = db.Column(db.String(length=16), nullable=False) price = db.Column(db.Float) start_time = db.Column(db.DateTime) name = db.Column(db.String(length=128)) description = db.Column(db.String(length=64)) nickname = db.Column(db.String(length=50)) __table_args__ = (db.PrimaryKeyConstraint('site', 'id'), {}) @classmethod def create_item(cls, site, id, db_session=None): item = Item(site=site, id=id) if db_session: db_session.add(item) else: db.session.add(item) if db_session: db_session.commit() else: db.session.commit() return item @classmethod def get_or_create_item(cls, site, id, db_session=None): item = cls.get_item(site, id) if not item: item = cls.create_item(site, id, db_session) return item @classmethod def get_item(cls, site, id): item = cls.query.filter(cls.site == site).filter(cls.id == id) return item.first() def __str__(self): return "{"\ f"'id':'{self.id}'"\ f", 'site':'{self.site}'"\ f", 'price':'{self.price}'"\ f", 'name':'{self.name}'"\ f", 'description':'{self.description}'"\ f", 'nickname':'{self.nickname}'"\ "}"
class Station(db.Model): __tablename__ = "station" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) password_hash = db.Column(db.String(128)) linked_roll = db.Column(db.Integer, db.ForeignKey('roll.id')) linked_roll_rel = db.relationship("Roll", back_populates="linked_rfid") scan = db.Column(db.Integer) active = db.Column(db.Boolean, default=False) def __repr__(self): return '<Station {}>'.format(self.name) def set_password(self, password): """Runs the passwords through a hash and appends.""" self.password_hash = generate_password_hash(str(password)) def check_password(self, password): """Checks a password against the hash.""" return check_password_hash(self.password_hash, password) def get_scan(self): return self.scan if not self.active else None
class Sensor(CRUDMixin, db.Model): id = db.Column(db.Integer, primary_key=True) SENSOR_TYPE = db.Column(db.Text) ACCELEROMETER_X = db.Column(db.Text) ACCELEROMETER_Y = db.Column(db.Text) ACCELEROMETER_Z = db.Column(db.Text) X_AXIS = db.Column(db.Text) Y_AXIS = db.Column(db.Text) Z_AXIS = db.Column(db.Text) LINEAR_ACCELERATION_X = db.Column(db.Text) LINEAR_ACCELERATION_Y = db.Column(db.Text) LINEAR_ACCELERATION_Z = db.Column(db.Text) GYROSCOPE_X = db.Column(db.Text) GYROSCOPE_Y = db.Column(db.Text) GYROSCOPE_Z = db.Column(db.Text) ORIENTATION_Z = db.Column(db.Text) ORIENTATION_X = db.Column(db.Text) ORIENTATION_Y = db.Column(db.Text) Time_since_start = db.Column(db.Text) state = db.Column(db.Text) timestamp = db.Column(db.DateTime) prediction = db.Column(db.Text) experiment_id = db.Column(db.Integer, db.ForeignKey('experiment.id')) def __init__(self, experiment, SENSOR_TYPE=None, ACCELEROMETER_X=None, ACCELEROMETER_Y=None, ACCELEROMETER_Z=None, X_AXIS=None, Y_AXIS=None, Z_AXIS=None, LINEAR_ACCELERATION_X=None, LINEAR_ACCELERATION_Y=None, LINEAR_ACCELERATION_Z=None, GYROSCOPE_X=None, GYROSCOPE_Y=None, GYROSCOPE_Z=None, ORIENTATION_Z=None, ORIENTATION_X=None, ORIENTATION_Y=None, Time_since_start=None, state=None, timestamp=None, prediction=None): self.SENSOR_TYPE = SENSOR_TYPE self.ACCELEROMETER_X = ACCELEROMETER_X self.ACCELEROMETER_Y = ACCELEROMETER_Y self.ACCELEROMETER_Z = ACCELEROMETER_Z self.X_AXIS = X_AXIS self.Y_AXIS = Y_AXIS self.Z_AXIS = Z_AXIS self.LINEAR_ACCELERATION_X = LINEAR_ACCELERATION_X self.LINEAR_ACCELERATION_Y = LINEAR_ACCELERATION_Y self.LINEAR_ACCELERATION_Z = LINEAR_ACCELERATION_Z self.GYROSCOPE_X = GYROSCOPE_X self.GYROSCOPE_Y = GYROSCOPE_Y self.GYROSCOPE_Z = GYROSCOPE_Z self.ORIENTATION_Z = ORIENTATION_Z self.ORIENTATION_X = ORIENTATION_X self.ORIENTATION_Y = ORIENTATION_Y self.Time_since_start = Time_since_start self.state = state self.timestamp = timestamp self.prediction = prediction # not in uploaded files self.experiment_id = experiment.id # not in uploaded files def __repr__(self): return '<Timestamp {:d}>'.format(self.timestamp)
class Base(db.Model): __abstract__ = True id = db.Column(db.Integer, primary_key=True) date_created = db.Column(db.DateTime, default=db.func.current_timestamp())