class Role(db.Model): ''' 权限 ''' __tablename__ = 'db_role' id = db.Column(db.Integer, nullable=False, primary_key=True, index=True, autoincrement=True) role_id = db.Column(db.String(36), index=True, nullable=False, unique=True) name = db.Column(db.String(64), nullable=False, unique=True) mark = db.Column(db.String(64), nullable=False, unique=True) is_disabled = db.Column(db.Boolean, index=True, default=False) admins = db.relationship('Admin', backref='role') menus = db.relationship('Menu', secondary=MenuToRole, backref=db.backref('db_role', lazy='dynamic'), lazy='dynamic') interfaces = db.relationship('Interface', secondary=InterfaceToRole, backref=db.backref('db_interface', lazy='dynamic'), lazy='dynamic') __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 '<Role %r>' % self.name
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 Agency(db.Model): """ The model of agency """ id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, unique=True) name = db.Column(db.String(50), nullable=False) introduction = db.Column(db.Text, nullable=True) city = db.Column(db.String(20), nullable=False) address = db.Column(db.String(200), nullable=False) phone = db.Column(db.String(15), nullable=False) certification = db.Column(db.Boolean, nullable=False, default=False) create_time = db.Column(db.DateTime, nullable=False) last_update = db.Column(db.DateTime, nullable=True) avator = db.Column(db.Integer, db.ForeignKey("image.id"), nullable=False) trust_orders = db.relationship("TrusteeshipOrder", backref='agency') images = db.relationship("Image", secondary=agency_image, lazy='subquery', backref=db.backref('image_of', lazy=True)) managers = db.relationship("User", backref="own_agency") comments = db.relationship("Comment", backref="comment_for") counter = db.relationship("Counter", backref="counter_for")
class TrusteeshipOrder(db.Model): """ The model of trusteeship orders """ id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, unique=True) ord_num = db.Column(db.String(30), nullable=False) species = db.Column(db.String(50), nullable=False) pet_name = db.Column(db.String(50), nullable=False) age = db.Column(db.Integer, nullable=False) weight = db.Column(db.Float, nullable=False) expiration = db.Column(db.Integer, nullable=False) sterilization = db.Column(db.Boolean, nullable=False) naughty = db.Column(db.Boolean) shy = db.Column(db.Boolean) friendly = db.Column(db.Boolean) comment = db.Column(db.Text) price = db.Column(db.Float, nullable=False) status = db.Column(db.Integer, nullable=False, default=0) create_time = db.Column(db.DateTime, nullable=False) open_time = db.Column(db.DateTime) close_time = db.Column(db.DateTime) agency_fee = db.Column(db.Float) agency_id = db.Column(db.Integer, db.ForeignKey('agency.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) images = db.relationship("Image", secondary=trust_order_image, lazy='subquery', backref=db.backref('image_in', lazy=True))
class Post(db.Model): __tablename__ = 'post' 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) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref=db.backref('posts')) image = db.Column(db.String(10), default='/')
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 User(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) username = db.Column(db.String(80), unique=True) password = db.Column(db.String(80)) email = db.Column(db.String(120), unique=True) address = db.Column(db.String(200)) barangay = db.Column(db.String(100)) phone_no = db.Column(db.String(30)) role = db.Column(db.String(20)) registration_id = db.Column(db.String(300)) is_verified = db.Column(db.Boolean) services = db.relationship(Service, secondary=user_services_table, backref=db.backref('users')) def __str__(self): return '{} {}'.format(self.first_name, self.last_name)
class HistoricalPerson(Object): __tablename__ = "historical_person" 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="person_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')) second_name_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id"), name="person_second_name_id", nullable=False) second_name = relationship( LocaleString, foreign_keys=[second_name_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) patronymic_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id", ondelete="SET NULL"), name="person_patronymic_id", nullable=True) patronymic = relationship( LocaleString, foreign_keys=[patronymic_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) birthdate = db.Column(db.Date, name="person_birthdate", nullable=False) deathdate = db.Column(db.Date, name="person_deathdate", nullable=True) related_objects = relationship("Object", secondary=HistoricalPersonRelatedObject, single_parent=True, backref=db.backref('historical_person')) __mapper_args__ = {'polymorphic_identity': EntityType.historical_person} def get_name(self, locale): name = self.name.get(locale) second_name = self.second_name.get(locale) patronymic = self.patronymic.get(locale) if name is None or second_name is None: return None elif patronymic is None: return name.text + " " + second_name.text else: return "%s. %s. %s" % (name.text[0], patronymic.text[0], second_name.text) def to_json(self, locale): name = self.name.get(locale) second_name = self.second_name.get(locale) patronymic = self.patronymic.get(locale) object_json = super().to_json(locale) person_json = { 'name': name, 'secondName': second_name, 'patronymic': patronymic, 'birthdate': str(self.birthdate), 'deathdate': str(self.deathdate) if self.deathdate is not None else None, 'relatedObjects': list(map(lambda o: o.to_object_json(locale), self.related_objects)) } return {**object_json, **person_json}
class User(db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True, name="user_id", nullable=False) name = db.Column(db.String(100), name="user_name", nullable=False) password = db.Column(db.String(100), name="user_password", nullable=False) email = db.Column(db.String(200), name="user_email", nullable=False) is_active = db.Column(db.Boolean, name="is_active", default=True, server_default='1', nullable=False) roles = relationship("Role", secondary="user_role", single_parent=True, backref=db.backref('user')) favorites = relationship("Entity", secondary="user_favorite", single_parent=True, order_by=desc(UserFavorite.c.add_time), backref=db.backref('user_favorites')) image = db.Column(db.String(41), name="user_image", nullable=False) visited_places = relationship("Place", secondary="user_visited_place", single_parent=True, order_by=desc(UserVisitedPlace.c.add_time), backref=db.backref('user_visited_places')) def set_password(self, password): """Set password.""" self.password = bcrypt_init.bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt_init.bcrypt.check_password_hash(self.password, value) def can(self, allowed_roles): user_role_ids = [] for role in self.roles: user_role_ids.append(role.name.value) for allowed_role in allowed_roles: if allowed_role.value in user_role_ids: return True return False def is_moder(self): moder_roles = [RoleName.content_moder.value, RoleName.user_moder.value] for role in self.roles: if role.name.value in moder_roles: return True return False def is_admin(self): for role in self.roles: if role.name.value is RoleName.admin.value: return True return False def to_json(self, locale): return {'id': self.id, 'name': self.name, 'image': self.image}
class Object(Entity): __metaclass__ = ABCMeta __tablename__ = "object" id = db.Column(db.Integer, db.ForeignKey("entity.entity_id"), primary_key=True, name="object_id", nullable=False) image_link = db.Column(db.String(250), name="object_image_link", nullable=False) audioguide_link_id = db.Column(db.String(36), db.ForeignKey("locale_link.link_id", ondelete="SET NULL"), name="object_audioguide_link_id", nullable=True) audioguide_link = relationship( LocaleLink, foreign_keys=[audioguide_link_id], uselist=True, single_parent=True, cascade="all, delete-orphan", collection_class=attribute_mapped_collection('locale')) description_id = db.Column(db.String(36), db.ForeignKey("locale_string.string_id"), name="object_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')) categories = relationship("Category", secondary=CategoryObject, single_parent=True, backref=db.backref('object')) __mapper_args__ = {'polymorphic_identity': EntityType.object} @abstractmethod def get_name(self, locale): raise NotImplementedError("Must override method get_name") def get_image(self): return self.image_link def to_json(self, locale): return self.to_object_json(locale) # we need it because some times we need not to call child's to_json() func def to_object_json(self, locale): return { 'id': self.id, 'name': self.get_name(locale), 'city': self.city.to_attribute_json(locale), 'type': self.type.name, 'image': self.image_link, 'audioguide': self.audioguide_link.get(locale), 'categories': self.categories, 'rating': { 'average': self.avg_rating(), 'count': len(self.reviews) } }