class Place(Object): __tablename__ = "place" id = db.Column(db.Integer, db.ForeignKey("object.object_id"), primary_key=True, name="object_id", nullable=False) name_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id"), name="place_name_id", nullable=False) name = relationship(LocaleString, foreign_keys=[name_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) address_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id"), name="place_address_id", nullable=False) address = relationship( LocaleString, foreign_keys=[address_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) geolocation_id = db.Column( db.Integer, db.ForeignKey("geolocation.geolocation_id"), name="place_geolocation_id", nullable=False, ) geolocation = relationship("Geolocation", cascade="all, delete-orphan", single_parent=True) routes = relationship("RoutePlaceInfo", cascade="all, delete-orphan", single_parent=True) __mapper_args__ = {'polymorphic_identity': EntityType.place} def get_name(self, locale): return self.name.get(locale) def to_json(self, locale): object_json = super().to_json(locale) place_json = { 'address': self.address.get(locale), 'geolocation': self.geolocation, 'description': self.description.get(locale), 'routes': list(map(lambda r: r.route_id, self.routes)), } return {**object_json, **place_json}
class UserMetabase(Base): __tablename__ = 'user_metabase' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, primary_key=True) email = db.Column(db.String(128)) password = db.Column(db.String(128)) user = relationship("User", back_populates="metabase")
class Interface(db.Model): ''' 接口 ''' __tablename__ = 'db_interface' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) interface_id = db.Column(db.String(36), index=True, nullable=False, unique=True) name = db.Column(db.String(64), nullable=False, unique=True) path = db.Column(db.String(255), nullable=False, unique=True) method = db.Column(db.String(36), nullable=False) description = db.Column(db.String(255), nullable=False) identification = db.Column(db.String(255), nullable=False, unique=True) isLock = db.Column(db.Boolean, index=True, default=True) menu_id = db.Column(db.String(36), db.ForeignKey('db_menu.menu_id')) __table_args__ = ({"useexisting": True}) def to_json(self): dict = self.__dict__ if "_sa_instance_state" in dict: del dict["_sa_instance_state"] return dict def __repr__(self): return '<Interface %r>' % self.name
class Menu(db.Model): ''' 菜单 ''' __tablename__ = 'db_menu' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) menu_id = db.Column(db.String(36), index=True, nullable=False, unique=True) pid = db.Column(db.String(36), nullable=False, index=True, default='0') title = db.Column(db.String(64), nullable=False, unique=True) path = db.Column(db.String(255), nullable=False, unique=True) icon = db.Column(db.String(255), nullable=False) mark = db.Column(db.String(255), nullable=False, unique=True) sort = db.Column(db.SmallInteger, index=True, default=1) is_disabled = db.Column(db.Boolean, index=True, default=False) interfaces = db.relationship('Interface', backref='menu') __table_args__ = ({"useexisting": True}) def to_json(self): dict = self.__dict__ if "_sa_instance_state" in dict: del dict["_sa_instance_state"] return dict def __repr__(self): return '<Menu %r>' % self.title
class Role(db.Model): __tablename__ = "role" id = db.Column(db.Integer, primary_key=True, name="role_id", nullable=False) name = db.Column(db.Enum(RoleName), name="role_name", nullable=False, unique=True)
class Reply(db.Model): __tablename__ = 'reply' id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.String(140), nullable=False) create_time = db.Column(db.DateTime, default=datetime.now) # 所属主贴 post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', backref=db.backref('posts')) # 回复用户 author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref=db.backref('replies'))
class OAuth2Token(db.Model, OAuth2TokenMixin): __tablename__ = 'oauth2_token' id = db.Column(db.Integer, primary_key=True) user_id = db.Column( db.Integer, db.ForeignKey('account.id', ondelete='CASCADE')) user = db.relationship('User') def is_refresh_token_expired(self): expires_at = self.issued_at + self.expires_in * 2 return expires_at < time.time()
class InitSql(db.Model): ''' 是否已经初始化数据库 ''' __tablename__ = 'db_init_sql' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) isInit = db.Column(db.Boolean, index=True, default=True) __table_args__ = ({"useexisting": True})
class Word(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) frequency = db.Column(db.Integer) site_id = db.Column(db.Integer, db.ForeignKey('site.id'), nullable=False) site = db.relationship('Site', backref=db.backref('words', lazy='dynamic', cascade='all, delete')) @property def json(self): return to_json(self, self.__class__)
class Counter(db.Model): """ The model of counter """ id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, unique=True) date_time = db.Column(db.DateTime, nullable=False) agency_id = db.Column(db.Integer, db.ForeignKey("agency.id"), nullable=False)
class Image(db.Model): """ The model of images """ id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, unique=True) name = db.Column(db.String(50), nullable=False) physical_name = db.Column(db.String(50), nullable=False) url = db.Column(db.String(250), nullable=False) upload_time = db.Column(db.DateTime, nullable=False) agency_avator = db.relationship("Agency", backref='avator_of')
class LocaleLink(db.Model): __tablename__ = "locale_link" id = db.Column(db.String(36), primary_key=True, name="link_id", nullable=False) locale = db.Column(db.Enum(Language), primary_key=True, name="link_locale", nullable=False) path = db.Column(db.Text, name="link_path", nullable=False) def to_json(self, locale): return self.path
class Entity(db.Model): __metaclass__ = ABCMeta __tablename__ = 'entity' id = db.Column(db.Integer, primary_key=True, name="entity_id", nullable=False) type = db.Column(db.Enum(EntityType), name="entity_type", nullable=False) city_id = db.Column(db.Integer, db.ForeignKey("city.city_id"), name="entity_city_id", nullable=False) city = relationship("City") reviews = relationship("Review", cascade="all, delete-orphan", single_parent=True) __mapper_args__ = { 'polymorphic_identity': EntityType.entity, 'polymorphic_on': type } @abstractmethod def get_name(self, locale): raise NotImplementedError("Must override method get_name") @abstractmethod def get_image(self): raise NotImplementedError("Must override method get_image") def to_entity_json(self, locale): return { 'id': self.id, 'type': self.type.name, 'name': self.get_name(locale), 'image': self.get_image() } def to_json(self, locale): return self.to_entity_json(locale) def avg_rating(self): if len(self.reviews) < 1: return 0 return round(mean(map(lambda r: r.rating, self.reviews)), 2)
class UserCred(db.Model): __tablename__ = 'cred' user_id = db.Column(db.Integer, primary_key=True) login = db.Column(db.String(20), unique=True) pw_hash = db.Column(db.String(180), unique=True) info = db.relationship('UserInfo', uselist=False, back_populates="cred") # NOT USE def __init__(self, login, password): self.login = login self.pw_hash = bcrypt.generate_password_hash(password).decode('utf8') def create_auth_token(self): try: return os.urandom(24).hex() except Exception as e: return e
class LoginLock(db.Model): ''' 登录锁定 ''' __tablename__ = 'db_login_lock' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) lock_id = db.Column(db.String(36), index=True, nullable=False, unique=True) user_id = db.Column(db.String(36), index=True, nullable=False) flag = db.Column(db.Boolean, index=True, default=False) # 是否锁定 number = db.Column(db.Integer, primary_key=True, default=0) ip = db.Column(db.String(36), index=True) lock_time = db.Column(db.DateTime) __table_args__ = ({"useexisting": True}) def to_json(self): dict = self.__dict__ if "_sa_instance_state" in dict: del dict["_sa_instance_state"] if "lock_time" in dict: dict["lock_time"] = dict["lock_time"].strftime('%Y-%m-%d %H:%M:%S') return dict def __repr__(self): return '<LoginLock %r>' % self.lock_id
class Folder(db.Model): ''' 文件夹 ''' __tablename__ = 'db_folder' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) folder_id = db.Column(db.String(36), index=True, nullable=False, unique=True) admin_id = db.Column(db.String(36), index=True) pid = db.Column(db.String(36), nullable=False, index=True, default='0') name = db.Column(db.String(36), index=True, nullable=False) is_sys = db.Column(db.Boolean, index=True, default=True) # True = 系统文件夹 create_time = db.Column(db.DateTime, index=True, default=datetime.datetime.now) documents = db.relationship('Document', backref='folder') __table_args__ = ({"useexisting": True}) def to_json(self): dict = self.__dict__ if "_sa_instance_state" in dict: del dict["_sa_instance_state"] if "create_time" in dict: dict["create_time"] = dict["create_time"].strftime( '%Y-%m-%d %H:%M:%S') return dict def __repr__(self): return '<Folder %r>' % self.name
class City(db.Model): __tablename__ = "city" id = db.Column(db.Integer, primary_key=True, name="city_id", nullable=False) name_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id"), name="city_name_id", nullable=False) name = relationship(LocaleString, foreign_keys=[name_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) image_link = db.Column(db.String(250), name="city_image_link", nullable=False) description_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id"), name="city_description_id", nullable=False) description = relationship( LocaleString, foreign_keys=[description_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) def to_attribute_json(self, locale): return { 'id': self.id, 'name': self.name.get(locale), } def to_json(self, locale): return { 'id': self.id, 'name': self.name.get(locale), 'description': self.description.get(locale), 'image_link': self.image_link, }
class Action(db.Model, ModelMethods): _table_name = 'action' id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey("post.id")) user_id = db.Column(db.Integer, db.ForeignKey("user.id")) time = db.Column(db.DateTime()) def __init__(self, post_id, user_id, like_time=None): self.post_id = post_id self.user_id = user_id if like_time: self.time = like_time else: self.time = datetime.now() def __repr__(self): return "Like: {} {} {}".format(self.post_id, self.user_id, self.time)
class UserDatabase(Base): __tablename__ = 'user_databases' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) drivername = db.Column(db.String(128)) username = db.Column(db.String(128)) password = db.Column(db.String(128)) host = db.Column(db.String(256)) port = db.Column(db.Integer) database = db.Column(db.String(128)) query = db.Column(db.String(256)) user = relationship("User", back_populates="databases") def get_url(self): data = dump_json(self) return get_db_url(loads(data))
class UserInfo(db.Model): __tablename__ = 'info' info_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) surname = db.Column(db.String(20)) age = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('cred.user_id')) # NOT USE cred = db.relationship('UserCred', uselist=False, back_populates="info") # NOT USE def __init__(self, id, name=None, surname=None, age=None): self.name = name self.surname = surname self.age = age self.user_id = id def update_fields(self, fields): keys = ['name', 'surname', 'age'] for key in keys: if fields.get(key) is not None: setattr(self, key, fields[key])
class ProjectModel(Base): __tablename__ = 'projects' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) todos = db.relationship('TodoModel', lazy='dynamic') user_id = db.Column(db.Integer, db.ForeignKey('users.id')) user = db.relationship('UserModel') def __init__(self, name, user_id=None): self.name = name, self.user_id = user_id def json(self): return { 'id': self.id, 'name': self.name, 'user_id': self.user_id, 'todos': [todo.json() for todo in self.todos.all()] }
class Review(db.Model): __tablename__ = "review" user_id = db.Column(db.Integer, db.ForeignKey("user.user_id", ondelete="cascade"), primary_key=True, name="user_id", nullable=False) user = relationship("User") entity_id = db.Column(db.Integer, db.ForeignKey("entity.entity_id", ondelete="cascade"), primary_key=True, name="entity_id", nullable=False) entity = relationship("Entity") rating = db.Column(db.SMALLINT, name="review_rating", nullable=False) time = db.Column(db.DateTime, name="review_last_modified_time", default=datetime.datetime.utcnow, nullable=False) text = db.Column(db.Text, name="review_text", nullable=True) locale = db.Column(db.String(2), name="review_locale", nullable=True) def to_json(self, locale): return { 'user': self.user, 'entity': self.entity_id, 'time': str(self.time), 'rating': self.rating, 'text': str(self.text) if self.text is not None else None, 'locale': self.locale }
class LocaleString(db.Model): __tablename__ = "locale_string" id = db.Column( db.String(36), primary_key=True, name="string_id", nullable=False ) locale = db.Column( db.Enum(Language), primary_key=True, name="string_locale", nullable=False ) text = db.Column( db.Text, name="string_content", nullable=False ) def to_json(self, locale): return self.text
class BookingRequest(db.Model): id = db.Column(db.Integer, primary_key=True) booking_date = db.Column(db.Date) booking_time = db.Column(db.Time) details = db.Column(db.String(240)) status = db.Column(db.String(20)) booking_id = db.Column(db.Integer(), db.ForeignKey(Booking.id)) worker_id = db.Column(db.Integer(), db.ForeignKey(User.id)) booking = db.relationship(Booking, foreign_keys=[booking_id]) worker = db.relationship(User, foreign_keys=[worker_id]) def __str__(self): return self.details
class Timetable(db.Model): __tablename__ = "timetable" id = db.Column( db.Integer, db.ForeignKey("public_place.object_id"), primary_key=True, name="public_place_id", nullable=False ) public_place = relationship( "PublicPlace" ) week_day = db.Column( db.Enum(WeekDay), name="week_day", nullable=False, primary_key=True ) open_time = db.Column( db.Time, name="open_time", nullable=False ) close_time = db.Column( db.Time, name="close_time", nullable=False ) def to_json(self, locale): return { 'day': self.week_day.name, 'openTime': str(self.open_time), 'closeTime': str(self.close_time) }
class User(Base): is_authenticated = True is_active = True is_anonymous = False __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) sub = db.Column(db.String(128)) email = db.Column(db.String(128), nullable=False, unique=True) given_name = db.Column(db.String(128)) family_name = db.Column(db.String(128)) name = db.Column(db.String(128)) picture_url = db.Column(db.String(500)) google_credentials = db.Column(db.LargeBinary) databases = relationship("UserDatabase", back_populates="user") metabase = relationship("UserMetabase", back_populates="user") def get_id(self): return self.id
class Sync(Base): __tablename__ = 'sync' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) status = db.Column(db.String(256)) start = db.Column(db.DateTime, comment='A timestamp of when the sync started') end = db.Column(db.DateTime, comment='A timestamp of when the sync started') message = db.Column(db.Text) tasks = relationship("SyncTask", back_populates="sync") user = relationship("User")
class AuditLog(IdMixin, CreationTimeMixin, db.Model): __tablename__ = 'audit_logs' entity_id = db.Column(db.BigInteger) entity_type = db.Column(db.String, nullable=False) action = db.Column(db.String, nullable=False) user_id = db.Column(db.BigInteger, db.ForeignKey('base_users.id'), nullable=True) old_value = db.Column(db.String) new_value = db.Column(db.String) user = db.relationship('User', lazy=True)
class Document(db.Model): ''' 文档 ''' __tablename__ = 'db_document' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) document_id = db.Column(db.String(36), index=True, nullable=False, unique=True) admin_id = db.Column(db.String(36), index=True, nullable=False) name = db.Column(db.String(64), nullable=False) path = db.Column(db.String(255), nullable=False) type = db.Column(db.SmallInteger, index=True, default=1) ext = db.Column(db.String(64), nullable=False) size = db.Column(db.Integer, nullable=False) deleted = db.Column(db.Integer, default=0) __table_args__ = ({"useexisting": True}) def to_json(self): dict = self.__dict__ if "_sa_instance_state" in dict: del dict["_sa_instance_state"] return dict def __repr__(self): return '<Document %r>' % self.name
class CalendarUser(Base): __tablename__ = 'calendar_user' __bind_key__ = 'user_db' # The internal Google ID id = db.Column(db.String(100), primary_key=True) primary_alias = db.Column(db.String(200)) given_name = db.Column(db.String(200)) family_name = db.Column(db.String(200)) full_name = db.Column(db.String(200)) # Should get updated with every sync current_calendar_timezone = db.Column(db.String(200)) user_aliasses = relationship("CalendarUserAlias", back_populates="calendar_user")