class NewSubmission(db.Model, Base): __tablename__ = 'new_submission' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) editor_in_chief_id = db.Column(db.Integer, db.ForeignKey('users.id')) associate_editor_id = db.Column(db.Integer, db.ForeignKey('users.id')) type_id = db.Column(db.Integer, db.ForeignKey('article_type.id')) title = db.Column(db.String(256), nullable=False) abstract = db.Column(db.Text) keywords = db.Column(db.Text) # classification_id TODO: 此键表还未建 comments = db.Column(db.Text) current_status = db.Column(db.Integer) final_disposition = db.Column(db.Integer) editor_decision = db.Column(db.Integer) uid = db.relationship("User", foreign_keys='NewSubmission.user_id', backref='articles') editor_in_chief = db.relationship( "User", foreign_keys='NewSubmission.editor_in_chief_id', backref='editor_in_chief') associate_editor = db.relationship( "User", foreign_keys='NewSubmission.associate_editor_id', backref='associate_editor') @property def article_id(self): return "CC-{0}-{1}".format(datetime.now().year, self.id) def __repr__(self): return '<NewSubmission id={0}, title={1}, user={2}>'.format( self.article_id, self.title, self.uid)
class Register(Base): id = db.Column(db.Integer, nullable=False, primary_key=True) patient_name = db.Column(db.Text) gender = db.Column(db.Text) phone = db.Column(db.BigInteger) first_level_department_id = db.Column(db.SmallInteger, db.ForeignKey('first_level_department.id')) first_level_department = db.relationship('FirstLevelDepartment', backref='register') second_level_department_id = db.Column(db.SmallInteger, db.ForeignKey('second_level_department.id')) second_level_department = db.relationship('SecondLevelDepartment', backref='register')
class Treatment(Base): id = db.Column(db.Integer, nullable=False, autoincrement=True, primary_key=True) price = db.Column(db.Float) register_id = db.Column(db.Integer, db.ForeignKey('register.id'), primary_key=True) drug_id = db.Column(db.Integer, db.ForeignKey('drugs.id'), primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), primary_key=True) register = db.relationship('Register', backref='treatment') drugs = db.relationship('Drugs', backref='treatment') projects = db.relationship('Projects', backref='treatment')
class Evento(db.Model, CRUD): __tablename__ = "eventos" id = db.Column(db.Integer, primary_key=True) evento = db.Column(db.Enum(Evento), nullable=False) horario = db.Column(db.DateTime, nullable=False) id_usuario = db.Column(db.Integer, db.ForeignKey("usuario.id"), nullable=False) id_sala = db.Column(db.Integer, db.ForeignKey("sala.id"), nullable=False) def __init__(self, evento, horario, id_usuario, id_sala): self.evento = evento self.horario = horario self.id_usuario = id_usuario self.id_sala = id_sala
class Event(db.Model, CRUD): __tablename__ = "events" id = db.Column(db.Integer, primary_key=True) eventType = db.Column(db.Enum(EventTypesEnum), nullable=False) dateTime = db.Column(db.DateTime, nullable=False) userId = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) user = db.relationship("User") roomId = db.Column(db.Integer, db.ForeignKey("rooms.id"), nullable=False) room = db.relationship("Room") def __init__(self): self.lastUpdate = datetime.now() self.active = True
class DireitoAcesso(db.Model, CRUD): __tablename__ = "direito_acesso" id = db.Column(db.Integer, primary_key=True) id_usuario = db.Column(db.Integer, db.ForeignKey("usuario.id"), nullable=False) id_sala = db.Column(db.Integer, db.ForeignKey("sala.id"), nullable=False) nome_sala = db.relationship("Sala", uselist=False) alive = db.Column(db.Boolean, nullable=False) def __init__(self, id_usuario, id_sala): self.id_usuario = id_usuario self.id_sala = id_sala self.alive = True
class RoomUser(db.Model, CRUD): __tablename__ = "roomsUsers" userId = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) roomId = db.Column(db.Integer, db.ForeignKey('rooms.id'), primary_key=True) user = db.relationship("User") room = db.relationship("Room") lastUpdate = db.Column(db.DateTime(), nullable=False) active = db.Column(db.Boolean, nullable=False) description = db.Column(db.String(255), unique=True) def __init__(self): self.lastUpdate = datetime.now() self.active = True
class UserRelatedModel(db.Model): __abstract__ = True user_id = db.Column( db.ForeignKey(f"{User.__tablename__}.id", ondelete="CASCADE", onupdate="CASCADE"))
class Wish(Base): __tablename__ = "wish" id = Column(Integer, primary_key=True) launched = Column( Boolean, default=False) # 表示礼物是否收到,False表示添加到了心愿清单还没有收到,True表示已经收到 uid = Column(Integer, db.ForeignKey("user.id")) user = relationship("User") isbn = Column(String(15), nullable=False) @property def book(self): # 根据当前的isbn获取这本书 yushu_book = YuShuBook() yushu_book.search_by_isbn(self.isbn) return yushu_book.first @classmethod # 获取我的所有想要的礼物 def get_user_gifts(cls, uid): return Wish.query.filter_by(uid=uid, launched=False).order_by( Wish.create_time.desc()).all() @classmethod def get_wish_counts(cls, isbn_list): # 根据传入的一组isbn,到Gift表中检索出要赠送的人,并计算出某个礼物可以赠送的Gift数量 from app.models.gift import Gift gifts = db.session.query(Gift.isbn, func.count(Gift.id)).filter( Gift.launched == False, Gift.status == 1, Gift.isbn.in_(isbn_list)).group_by(Gift.isbn).all() count_list = [{"count": w[1], "isbn": w[0]} for w in gifts] return count_list
class User(Base): id = db.Column(db.Integer, nullable=False, primary_key=True) name = db.Column(db.Text) pwd = db.Column(db.Text) email = db.Column(db.Text) phone = db.Column(db.BigInteger) last_login = db.Column(db.DateTime) user_identity_name = db.Column(db.Integer, db.ForeignKey('user_identity.name'))
class SecondLevelDepartment(Base): id = db.Column(db.SmallInteger, nullable=False, autoincrement=True, primary_key=True) name = db.Column(db.Text, nullable=False) phone = db.Column(db.BigInteger) first_level_department_id = db.Column( db.SmallInteger, db.ForeignKey('first_level_department.id')) first_level_department = db.relationship('FirstLevelDepartment', backref='second_level_department')
class Schedule(db.Model, CRUD): __tablename__ = "schedules" id = db.Column(db.Integer, primary_key=True) roomId = db.Column(db.Integer, db.ForeignKey("rooms.id"), nullable=False) room = db.relationship("Room") userType = db.Column(db.Enum(UserTypesEnum), nullable=False) dayOfWeek = db.Column(db.Enum(DaysOfWeekEnum), nullable=False) beginTime = db.Column(db.Time, nullable=False) endTime = db.Column(db.Time, nullable=False) lastUpdate = db.Column(db.DateTime, nullable=False) active = db.Column(db.Boolean, nullable=False)
class Horario(db.Model, CRUD): __tablename__ = "horario" id = db.Column(db.Integer, primary_key=True) last_update = db.Column(db.DateTime, nullable=False) dia = db.Column(db.Enum(Dia), nullable=False) hora_fim = db.Column(db.Time, nullable=False) hora_inicio = db.Column(db.Time, nullable=False) tipo_user = db.Column(db.Enum(TipoUsuario), nullable=False) id_sala = db.Column(db.Integer, db.ForeignKey("sala.id"), nullable=False) alive = db.Column(db.Boolean, nullable=False) def __init__(self, id_sala, dia, hora_inicio, hora_fim, tipo_user): self.id_sala = id_sala self.dia = dia self.hora_inicio = hora_inicio self.hora_fim = hora_fim self.tipo_user = tipo_user self.last_update = datetime.now() self.alive = True
class Gift(Base): id = Column(db.Integer, primary_key=True) uid = Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = relationship('User') isbn = Column(db.String(13)) launched = Column(db.Boolean, default=False) @property def book(self): yushu_book = YuShuBook() yushu_book.search_by_isbn(self.isbn) return yushu_book.first @classmethod def get_user_gifts(cls, uid): gifts = cls.query.filter_by(uid=uid, launched=False).order_by( desc(cls.create_time)).all() return gifts @classmethod def recent(cls): gift_list = cls.query.filter_by(launched=False).group_by( cls.isbn).order_by(desc(Gift.create_time)).limit( current_app.config['RECENT_BOOK_PER_PAGE']).distinct().all() return gift_list @classmethod def get_wish_count(cls, isbn_list): from app.models.wish import Wish count_list = db.session.query(Wish.isbn, func.count(Wish.id)).filter( Wish.launched == False, Wish.isbn.in_(isbn_list), Wish.status == 1).group_by(Wish.isbn).all() count_list = [EachGiftWishCount(w[0], w[1]) for w in count_list] return count_list def is_yourself_gift(self, uid): return True if uid == self.uid else False
class InstrumentsPurchase(Base): id = db.Column(db.Integer, nullable=False, autoincrement=True, primary_key=True) instruments_id = db.Column(db.Integer, db.ForeignKey('instruments.id')) supplier_id = db.Column(db.Integer, db.ForeignKey('supplier.id')) drugs = db.relationship('Instruments', backref='instruments_purchase') supplier = db.relationship('Supplier', backref='drugs_purchase')
class User(db.Model, Base, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) _password = db.Column(db.String(128), nullable=False) email = db.Column(db.String(64), nullable=False, unique=True) first_name = db.Column(db.String(32), nullable=False) middle_name = db.Column(db.String(32)) last_name = db.Column(db.String(32), nullable=False) title = db.Column(db.String(16), nullable=False) degree = db.Column(db.String(16)) # nick_name = db.Column(db.String(32)) phone_number = db.Column(db.String(16)) fax_number = db.Column(db.String(16)) secondary_email = db.Column(db.String(64)) institution_phone_number = db.Column(db.String(16)) institution = db.Column(db.String(32)) department = db.Column(db.String(32)) street = db.Column(db.String(64)) city = db.Column(db.String(16), nullable=False) country_id = db.Column(db.Integer, db.ForeignKey("countries.id")) state_or_province = db.Column(db.String(16)) zip = db.Column(db.String(32)) # person_classifications todo:这个键作为单独的一张表 person_keywords = db.Column(db.Text, nullable=False) roles = db.relationship('Role', secondary=user_roles, backref=db.backref('users', lazy='dynamic')) # article = db.relationship('NewSubmission', back_populates='uid') # editor_in_chief = db.relationship('NewSubmission', back_populates='editor_in_chief') # associate_editor = db.relationship('NewSubmission', back_populates='associate_editor') def __repr__(self): return '<User id={0}, email={1}, roles={2}>'.format( self.id, self.email, self.roles) def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if not self.roles: if self.email in current_app.config['ADMIN_EMAIL']: self.roles.append( Role.query.filter_by(permissions=Permission.ADMIN).first()) if not self.roles: self.roles.append(Role.query.filter_by(default=True).first()) @property def password(self): return AttributeError('password is not a readable attribute') @password.setter def password(self, raw): self._password = generate_password_hash(raw) def check_password(self, raw): return check_password_hash(self._password, raw) def can(self, perm): role = Role.query.filter_by(permissions=perm).first() if role in self.roles: return True else: return False def is_admin(self): return self.can(Permission.ADMIN)
from app import login_manager from app.models.base import Base, db _Author_ = 'BUPPT' class Permission: AUTHOR = 2 REVIEWER = 4 EDITOR = 8 ADMIN = 16 user_roles = db.Table( 'user_roles', db.Column('user_id', db.Integer, db.ForeignKey("users.id")), db.Column('role_id', db.Integer, db.ForeignKey("roles.id"))) class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) def __repr__(self): return '<Role id={0}, name={1}>'.format(self.id, self.name) def __init__(self, **kwargs): super(Role, self).__init__(**kwargs)
class Gift(Base): __tablename__ = "gift" id = Column(Integer, primary_key=True) launched = Column( Boolean, default=False) # 表示礼物是否已经送出去,False表示添加到了赠送清单还没有赠送,True表示已经赠送过 uid = Column(Integer, db.ForeignKey("user.id")) user = relationship("User") isbn = Column(String(15), nullable=False) # book = db.relationship("Book") # bid = db.Column(Integer, db.ForeignKey("book.id")) # 此案例没有保存书的数据 @property def book(self): # 根据当前的isbn获取这本书 yushu_book = YuShuBook() yushu_book.search_by_isbn(self.isbn) return yushu_book.first # 首页:显示最近书籍,最近的礼物,!,只显示一定数量(30) # 按照时间倒序排序,最新的排在前面 # 去重,同一本书籍的礼物不重复 (用分组) # 链式调用---主体是Query,下面的子函数返回的都是query主体,触发语句:first(),all() 最终生成sql语句去数据库中触发查询 @classmethod #对象代表一个礼物,具体。类代表礼物这个事物,它是抽象,不是具体的"一个" def recent(cls): # rencent_gift = Gift.query.filter_by( # launched=False).group_by( # Gift.isbn).order_by( # Gift.create_time).limit(5).distinct().all() # 课程讲的distinct 要配合group_by使用 rencent_gift = Gift.query.filter_by(launched=False).order_by( Gift.create_time.desc()).limit( current_app.config["RENCENT_BOOK_COUNT"]).distinct( Gift.isbn).all() # rencent_gift = Gift.query.filter_by( # launched=False).group_by(Gift.isbn).order_by(Gift.create_time.desc()).limit( # current_app.config["RENCENT_BOOK_COUNT"]).all() print(len(rencent_gift)) # print (db.session.query(db.distinct(Gift.isbn)).all()) return rencent_gift @classmethod #获取我的所有想赠送的礼物 def get_user_gifts(cls, uid): return Gift.query.filter_by(uid=uid, launched=False).order_by( Gift.create_time.desc()).all() @classmethod # filter中要使用表达式 def get_wish_counts(cls, isbn_list): # 根据传入的一组isbn,到wish表中检索出相应的礼物,并计算出某个礼物的wish心愿数量 from app.models.wish import Wish wishes = db.session.query(Wish.isbn, func.count(Wish.id)).filter( Wish.launched == False, Wish.status == 1, Wish.isbn.in_(isbn_list)).group_by(Wish.isbn).all() count_list = [{"count": w[1], "isbn": w[0]} for w in wishes] return count_list def send_drift_by_myself(self, uid): return True if self.user.id == uid else False