class Services(db.Model): id = db.Column(db.Integer, primary_key=True) service = db.Column(db.String(50)) form = db.Column(db.JSON) availability = db.Column(db.Integer, default=3) # 1 for AM, 2 for PM, 3 for BOTH # timestamps created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) # relationship appointment_service = db.relationship('Appointments', backref='service', lazy=True) @hybrid_property def form_date(self): return json.loads(self.form) def serialize(self): return { 'id': self.id, 'service': self.service, 'form': self.form, 'availability': self.availability, 'created_at': self.created_at, 'updated_at': self.updated_at }
class Inventory(db.Model): id = db.Column(db.Integer, primary_key=True) item_code = db.Column(db.String(100)) item = db.Column(db.String(100)) min_quantity = db.Column(db.Integer) max_quantity = db.Column(db.Integer) reorder_level = db.Column(db.Integer, default=10) # timestamps created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) # relationship items = db.relationship('Items', backref='inventory', lazy=True) @hybrid_property def is_sufficient(self): return self.reorder_level < len( [data for data in self.items if data.current_status == 5]) @hybrid_property def all_items(self): return self.items @hybrid_property def available_items(self): return [data for data in self.items if data.current_status == 5] @hybrid_property def used_items(self): return [data for data in self.items if data.current_status == 6] @hybrid_property def expired_items(self): return [data for data in self.items if data.current_status == 7] # @hybrid_method # def get_item(self, id): # return [data for data in self.items if data.id == id][0] def serialize(self): return { 'id': self.id, 'item_code': self.item_code, 'item': self.item, 'min_quantity': self.min_quantity, 'max_quantity': self.max_quantity, 'reorder_level': self.reorder_level }
class Tag(AuditMixin, BaseMixin, db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), nullable=False, default="Untitled Tag") description = db.Column(db.String(140)) user_id = db.Column( db.Integer, db.ForeignKey('user.id', ondelete='cascade') ) documents = db.relationship( 'Document', backref=db.backref('tags'), secondary=tags )
class employees(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) email = db.Column(db.String(100)) employees_logs = db.relationship('logs', backref='employee', lazy=True) def __init__(self, name, email): self.name = name self.email = email def __str__(self): return {'id': self.id, 'name': self.name, 'email': self.email}
class User(BaseMixin, db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String()) last_name = db.Column(db.String()) username = db.Column(db.String(), unique=True) email = db.Column(db.String(), unique=True) _admin = db.Column(db.Boolean, default=False) _active = db.Column(db.Boolean, default=True) _password = db.Column(db.String(), nullable=False) full_name = column_property(first_name + ' ' + last_name) tags = db.relationship('Tag', backref='user', lazy='dynamic') @property def password(self): return self._password @password.setter def password(self, password): self._password = generate_password_hash(password) def authenticate(self, password): return check_password_hash(self._password, password) @property def is_admin(self): return self._admin @property def is_active(self): return self._active def owns_document(self, document): return document in self.documents documents = db.relationship('Document', backref='user', lazy='dynamic')
class Status(db.Model): id = db.Column(db.Integer, primary_key=True) status = db.Column(db.String(50)) # timestamps created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) # relationship appointment_status = db.relationship('Appointments', backref='status', lazy=True) item_status = db.relationship('Items', backref='status', lazy=True) def serialize(self): return { 'id': self.id, 'status': self.status, 'created_at': self.created_at, 'updated_at': self.updated_at }
class CheckIn(db.Model): __tablename__ = 'checkin' id = db.Column(db.Integer, primary_key=True) date = db.Column(db.String(100), nullable=False) time = db.Column(db.String(100), nullable=False) stuid = db.Column(db.Integer, db.ForeignKey("student.id")) student = db.relationship("Student", backref=db.backref("checkins")) def __init__(self, date, time, stu): self.date = date self.time = time self.student = stu def __str__(self): return 'id' % (self.id)
class Roles(db.Model): id = db.Column(db.Integer, primary_key=True) role = db.Column(db.String(20)) # timestamps created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) # relationship accounts_roles = db.relationship('Accounts', backref='role', lazy=True) def serialize(self): return { 'id': self.id, 'role': self.role, 'created_at': self.created_at, 'updated_at': self.updated_at }
class Absent(db.Model): __tablename__ = 'absent' id = db.Column(db.Integer, primary_key=True) period = db.Column(db.Integer, nullable=False) date = db.Column(db.String(100), nullable=False) isCompleted = db.Column(db.Boolean, nullable=False) className = db.Column(db.String(100), nullable=False) stuid = db.Column(db.Integer,db.ForeignKey("student.id")) Student = db.relationship("Student",backref=db.backref("absents")) def __init__(self, period, date, stu, clsname): self.period = period self.date = date self.Student = stu self.isCompleted = False self.className = clsname def __str__(self): return '名称:%s' % (self.d1) def getDic(self): absent = {"period": self.period, "date": self.date, }
class Video(db.Model): __tablename__ = 'videos' id = db.Column(db.Integer(), primary_key=True, nullable=False) author_id = db.Column(db.String(), db.ForeignKey('users.username'), nullable=False) root = db.Column(db.String(), default='', nullable=False) filename = db.Column(db.String(), default='', nullable=False) duration = db.Column(db.String(), nullable=True) public = db.Column(db.Boolean(), nullable=True, default=False) title = db.Column(db.String(128), nullable=False) text = db.Column(db.Text(), nullable=True) views = db.Column(db.Integer(), nullable=False, default=0) thumbnail = db.Column(db.String(), nullable=True) hashtags = db.Column(db.String(), nullable=True) original_author = db.Column(db.String(), nullable=True) original_views = db.Column(db.Integer(), nullable=True) timestamp = db.Column(db.DateTime(timezone=True), server_default=func.now(), nullable=False) comments = db.relationship('Comment', backref='user', cascade='all,delete', lazy=True) voting = db.relationship('Like', backref='votes', cascade='all,delete', lazy=True) def __init__(self, author_id, title, text, duration=None, root=None, filename=None): self.author_id = author_id self.root = root self.title = title self.text = text self.duration = duration self.filename = filename def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def update(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) db.session.commit() @staticmethod def get_all(): return Video.query.all() @staticmethod def get_all_public(): return Video.query.filter_by(public=True).all() @staticmethod def get_all_private(): return Video.query.filter_by(public=False).all() def __repr__(self): return 'Video: {}'.format(self.title)
class Student(db.Model): __tablename__ = 'student' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) stuNo = db.Column(db.String(100), nullable=False) gender = db.Column(db.Integer, nullable=False) daysLate = db.Column(db.Integer, nullable=False) daysAbsent = db.Column(db.Integer, nullable=False) memo = db.Column(db.String(100), nullable=True) avatar = db.Column(db.String(5000), nullable=True) engName = db.Column(db.String(100), nullable=False) year = db.Column(db.Integer, nullable=False) pinyin = db.Column(db.String(100), nullable=False) #外键 classId = db.Column(db.Integer, db.ForeignKey("class.id")) classs = db.relationship("Classs", backref=db.backref("students")) scheduleId = db.Column(db.Integer, db.ForeignKey("schedule.id")) schedule = db.relationship("Schedule", backref=db.backref("students")) def __init__(self, name, stuNo, year, schedule, classs, gender, daysLate, daysAbsent, memo, avatar, engName, pinyin): self.name = name self.classs = classs self.stuNo = stuNo self.gender = gender self.daysLate = daysLate self.daysAbsent = daysAbsent self.memo = memo self.schedule = schedule self.avatar = avatar self.engName = engName self.year = year self.pinyin = pinyin def __str__(self): return '姓名:%s' % (self.name) def getSchedule(self): type = self.scheduleId return type def getName(self): return self.name def getstuNo(self): return self.stuNo def getDic(self): stuinfos = { "name": self.name, "code": self.stuNo, "dayslate": self.daysLate, "daysabsent": self.daysAbsent, "avatar": self.avatar, "memo": self.memo, "class": self.classs.getDic(), "Schedule": self.schedule.getDic(), "engName": self.engName } return stuinfos
class Accounts(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100)) middle_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) gender = db.Column(db.String(10)) civil = db.Column(db.String(10)) phone = db.Column(db.String(15)) birth_date = db.Column(db.DateTime) address = db.Column(db.String(100)) occupation = db.Column(db.String(100)) # login email = db.Column(db.String(100)) password_hash = db.Column(db.String(128)) # timestamps login_date = db.Column(db.DateTime, default=db.func.current_timestamp()) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) # relationship role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), nullable=True) account_notifications = db.relationship('Notifications', backref='account', lazy=True) account_appointments = db.relationship('Appointments', backref='account', lazy=True) @property def password(self): """ Prevent pasword from being accessed """ raise AttributeError('password is not a readable attribute.') @password.setter def password(self, password): """ Set password to a hashed password """ self.password_hash = generate_password_hash(password) def verify_password(self, password): """ Check if hashed password matches actual password """ return check_password_hash(self.password_hash, password) def serialize(self): return { 'id': self.id, 'first_name': self.first_name, 'middle_name': self.middle_name, 'last_name': self.last_name, 'gender': self.gender, 'civil': self.civil, 'phone': self.phone, 'birth_date': self.birth_date.strftime('%m/%d/%Y') if self.birth_date is not None else None, 'email': self.email, 'created_at': self.created_at, 'updated_at': self.updated_at, 'address': self.address, 'occupation': self.occupation, 'role_id': self.role_id }
class ScheduleTemplate(db.Model): __tablename__ = 'scheduletemplate' id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(100), nullable=False) mon1 = db.Column(db.String(100), nullable=False) mon2 = db.Column(db.String(100), nullable=False) mon3 = db.Column(db.String(100), nullable=False) mon4 = db.Column(db.String(100), nullable=False) mon5 = db.Column(db.String(100), nullable=False) mon6 = db.Column(db.String(100), nullable=False) mon7 = db.Column(db.String(100), nullable=False) mon8 = db.Column(db.String(100), nullable=False) mon9 = db.Column(db.String(100), nullable=False) tue1 = db.Column(db.String(100), nullable=False) tue2 = db.Column(db.String(100), nullable=False) tue3 = db.Column(db.String(100), nullable=False) tue4 = db.Column(db.String(100), nullable=False) tue5 = db.Column(db.String(100), nullable=False) tue6 = db.Column(db.String(100), nullable=False) tue7 = db.Column(db.String(100), nullable=False) tue8 = db.Column(db.String(100), nullable=False) tue9 = db.Column(db.String(100), nullable=False) wed1 = db.Column(db.String(100), nullable=False) wed2 = db.Column(db.String(100), nullable=False) wed3 = db.Column(db.String(100), nullable=False) wed4 = db.Column(db.String(100), nullable=False) wed5 = db.Column(db.String(100), nullable=False) wed6 = db.Column(db.String(100), nullable=False) wed7 = db.Column(db.String(100), nullable=False) wed8 = db.Column(db.String(100), nullable=False) wed9 = db.Column(db.String(100), nullable=False) thu1 = db.Column(db.String(100), nullable=False) thu2 = db.Column(db.String(100), nullable=False) thu3 = db.Column(db.String(100), nullable=False) thu4 = db.Column(db.String(100), nullable=False) thu5 = db.Column(db.String(100), nullable=False) thu6 = db.Column(db.String(100), nullable=False) thu7 = db.Column(db.String(100), nullable=False) thu8 = db.Column(db.String(100), nullable=False) thu9 = db.Column(db.String(100), nullable=False) fri1 = db.Column(db.String(100), nullable=False) fri2 = db.Column(db.String(100), nullable=False) fri3 = db.Column(db.String(100), nullable=False) fri4 = db.Column(db.String(100), nullable=False) fri5 = db.Column(db.String(100), nullable=False) fri6 = db.Column(db.String(100), nullable=False) fri7 = db.Column(db.String(100), nullable=False) fri8 = db.Column(db.String(100), nullable=False) fri9 = db.Column(db.String(100), nullable=False) classId = db.Column(db.Integer, db.ForeignKey("class.id")) classs = db.relationship("Classs", backref=db.backref("templates")) def construct(self): monday = [ self.mon1, self.mon2, self.mon3, self.mon4, self.mon5, self.mon6, self.mon7, self.mon8, self.mon9 ] tuesday = [ self.tue1, self.tue2, self.tue3, self.tue4, self.tue5, self.tue6, self.tue7, self.tue8, self.tue9 ] wednesday = [ self.wed1, self.wed2, self.wed3, self.wed4, self.wed5, self.wed6, self.wed7, self.wed8, self.wed9 ] thursday = [ self.thu1, self.thu2, self.thu3, self.thu4, self.thu5, self.thu6, self.thu7, self.thu8, self.thu9 ] friday = [ self.fri1, self.fri2, self.fri3, self.fri4, self.fri5, self.fri6, self.fri7, self.fri8, self.fri9 ] overall = [monday, tuesday, wednesday, thursday, friday] return overall def makeData(self, datas): if (len(datas) < 45): return False self.mon1 = datas[0] self.mon2 = datas[1] self.mon3 = datas[2] self.mon4 = datas[3] self.mon5 = datas[4] self.mon6 = datas[5] self.mon7 = datas[6] self.mon8 = datas[7] self.mon9 = datas[8] self.tue1 = datas[9] self.tue2 = datas[10] self.tue3 = datas[11] self.tue4 = datas[12] self.tue5 = datas[13] self.tue6 = datas[14] self.tue7 = datas[15] self.tue8 = datas[16] self.tue9 = datas[17] self.wed1 = datas[18] self.wed2 = datas[19] self.wed3 = datas[20] self.wed4 = datas[21] self.wed5 = datas[22] self.wed6 = datas[23] self.wed7 = datas[24] self.wed8 = datas[25] self.wed9 = datas[26] self.thu1 = datas[27] self.thu2 = datas[28] self.thu3 = datas[29] self.thu4 = datas[30] self.thu5 = datas[31] self.thu6 = datas[32] self.thu7 = datas[33] self.thu8 = datas[34] self.thu9 = datas[35] self.fri1 = datas[36] self.fri2 = datas[37] self.fri3 = datas[38] self.fri4 = datas[39] self.fri5 = datas[40] self.fri6 = datas[41] self.fri7 = datas[42] self.fri8 = datas[43] self.fri9 = datas[44] return True def __init__(self, name, cls, *overall): self.code = name self.classs = cls self.makeData(overall) def __str__(self): return ':%s' % (self.construct())
class User(db.Model): __tablename__ = 'users' username = db.Column(db.String(32), primary_key=True, unique=True, nullable=False) email = db.Column(db.String(128), unique=False, nullable=False) password = db.Column(db.String(), nullable=False) authenticated = db.Column(db.Boolean(), default=None) profile_picure = db.Column( db.String(), default=os.getcwd() + '/data/database/files/default/default_pic_a.jpg', nullable=False) token = db.relationship('Token', backref='access_token', cascade='all,delete', lazy=True) comments = db.relationship('Comment', backref='author', cascade='all,delete', lazy=True) voting = db.relationship('Like', backref='voter', cascade='all,delete', lazy=True) videos = db.relationship('Video', backref='owner', cascade='all,delete', lazy=True) def __init__(self, username, email, password, authenticated=False): self.username = username self.email = email self.password = password self.authenticated = authenticated def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def update(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) db.session.commit() def is_active(self): return True def get_id(self): return self.username def is_authenticated(self): self.authenticated def is_anonymous(self): return False def __repr__(self): return '{}'.format(self.username) @staticmethod def get_all(): return User.query.all() @staticmethod def username_exists(username): if User.query.get(username): return True else: return False