class Patients(db.Model): # __tablename__ = 'patients' id = db.Column(db.Integer, primary_key=True) accession_no = db.Column(db.String(20)) err_message = db.Column(db.String(70), default='') task_id = db.Column(db.Integer, db.ForeignKey('tasks.id')) status_id = db.Column(db.Integer, db.ForeignKey('status_dict.id')) status = db.relationship('StatusDict')
class StatusDict(db.Model): id = db.Column(db.Integer, primary_key=True) status_name = db.Column(db.String(20), unique=True) # status_id = db.Column(db.Integer, unique=True) @staticmethod def init_statusdict(): """初始化状态字典""" # StatusDict.__table__.drop() status_dict = { 1: '完成', 2: '被取消', 3: '失败', 4: '部分完成', 5: '未知错误', 6: '待审批', 7: '队列中', 8: '被拒绝' } status_obj = [] for key, val in status_dict.items(): status_obj.append(StatusDict(status_name=val)) db.session.add_all(status_obj) db.session.commit() def __repr__(self): return self.status_name
class Role(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) users = db.relationship('User', back_populates='role') permissions = db.relationship('Permission', secondary=roles_permissions, back_populates='roles') @staticmethod def init_role(): roles_permissions_map = { # 'Locked': ['FOLLOW', 'COLLECT'], '普通用户': ['FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD'], '普通管理员': ['FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD', 'MODERATE'], '超级管理员': [ 'FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD', 'MODERATE', 'ADMINISTER' ] } for role_name in roles_permissions_map: role = Role.query.filter_by(name=role_name).first() if role is None: role = Role(name=role_name) db.session.add(role) role.permissions = [] for permission_name in roles_permissions_map[role_name]: permission = Permission.query.filter_by( name=permission_name).first() if permission is None: permission = Permission(name=permission_name) db.session.add(permission) role.permissions.append(permission) db.session.commit()
class AEDict(db.Model): id = db.Column(db.Integer, primary_key=True) ae_title = db.Column(db.String(20), unique=True) ae_name = db.Column(db.String(20), unique=True) @staticmethod def init_aedict(): """初始化AE列表""" ae_list = [['DOWNLOAD', '下载', 1], ['SMIT_Q', '新网PACS', 2], ['ISDPHILIPS', '飞利浦ISD', 3]] ae_obj = [] for ae_title, ae_name, ae_id in ae_list: ae_obj.append(AEDict(ae_title=ae_title, ae_name=ae_name)) db.session.add_all(ae_obj) db.session.commit() def __str__(self): return self.ae_name
class Photo(db.Model): id = db.Column(db.Integer, primary_key=True) description = db.Column(db.String(500)) filename = db.Column(db.String(64)) filename_s = db.Column(db.String(64)) filename_m = db.Column(db.String(64)) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) can_comment = db.Column(db.Boolean, default=True) flag = db.Column(db.Integer, default=0) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', back_populates='photos') comments = db.relationship('Comment', back_populates='photo', cascade='all') collectors = db.relationship('Collect', back_populates='collected', cascade='all') tags = db.relationship('Tag', secondary=tagging, back_populates='photos')
class Tasks(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), index=True) transport_id = db.Column(db.Integer, db.ForeignKey('ae_dict.id')) transport_to = db.relationship('AEDict') series = db.Column(db.String(100)) time_wait = db.Column(db.Float, default=5) research_plan = db.Column(db.Text) other_file = db.Column(db.String(100)) folder_name = db.Column(db.String(100)) anonymous = db.Column(db.Integer, default=0) patients_count = db.Column(db.Integer, default=0) status_id = db.Column(db.Integer, db.ForeignKey('status_dict.id'), default=6) status = db.relationship('StatusDict') priority = db.Column(db.Integer, default=0) timestamp = db.Column(db.DateTime, default=datetime.now, index=True) patients = db.relationship('Patients') # collection active = db.Column(db.Boolean, default=False) # relationship用于反向引用 db.ForeignKey('user.id') user为表名, researcher_id = db.Column(db.Integer, db.ForeignKey('user.id')) researcher = db.relationship('User', back_populates='tasks')
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index=True, unique=True) photos = db.relationship('Photo', secondary=tagging, back_populates='tags')
class Permission(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) roles = db.relationship('Role', secondary=roles_permissions, back_populates='permissions')
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, index=True) # email = db.Column(db.String(254), unique=True, index=True) password_hash = db.Column(db.String(128)) name = db.Column(db.String(30)) website = db.Column(db.String(255)) bio = db.Column(db.String(120)) location = db.Column(db.String(50)) member_since = db.Column(db.DateTime, default=datetime.utcnow) avatar_s = db.Column(db.String(64)) avatar_m = db.Column(db.String(64)) avatar_l = db.Column(db.String(64)) avatar_raw = db.Column(db.String(64)) confirmed = db.Column(db.Boolean, default=False) locked = db.Column(db.Boolean, default=True) active = db.Column(db.Boolean, default=True) public_collections = db.Column(db.Boolean, default=True) receive_comment_notification = db.Column(db.Boolean, default=True) receive_follow_notification = db.Column(db.Boolean, default=True) receive_collect_notification = db.Column(db.Boolean, default=True) role_id = db.Column(db.Integer, db.ForeignKey('role.id')) role = db.relationship('Role', back_populates='users') photos = db.relationship('Photo', back_populates='author', cascade='all') comments = db.relationship('Comment', back_populates='author', cascade='all') notifications = db.relationship('Notification', back_populates='receiver', cascade='all') collections = db.relationship('Collect', back_populates='collector', cascade='all') following = db.relationship('Follow', foreign_keys=[Follow.follower_id], back_populates='follower', lazy='dynamic', cascade='all') followers = db.relationship('Follow', foreign_keys=[Follow.followed_id], back_populates='followed', lazy='dynamic', cascade='all') tasks = db.relationship('Tasks') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.generate_avatar() # self.follow(self) # follow self self.set_role() def __str__(self): return self.name def set_password(self, password): self.password_hash = generate_password_hash(password) def set_role(self): if self.role is None: if self.username == current_app.config['ADMIN_USERNAME']: self.role = Role.query.filter_by(name='超级管理员').first() else: self.role = Role.query.filter_by(name='普通用户').first() db.session.commit() def validate_password(self, password): return check_password_hash(self.password_hash, password) def follow(self, user): if not self.is_following(user): follow = Follow(follower=self, followed=user) db.session.add(follow) db.session.commit() def unfollow(self, user): follow = self.following.filter_by(followed_id=user.id).first() if follow: db.session.delete(follow) db.session.commit() def is_following(self, user): if user.id is None: # when follow self, user.id will be None return False return self.following.filter_by( followed_id=user.id).first() is not None def is_followed_by(self, user): return self.followers.filter_by( follower_id=user.id).first() is not None @property def followed_photos(self): return Photo.query.join(Follow, Follow.followed_id == Photo.author_id).filter( Follow.follower_id == self.id) def collect(self, photo): if not self.is_collecting(photo): collect = Collect(collector=self, collected=photo) db.session.add(collect) db.session.commit() def uncollect(self, photo): collect = Collect.query.with_parent(self).filter_by( collected_id=photo.id).first() if collect: db.session.delete(collect) db.session.commit() def is_collecting(self, photo): return Collect.query.with_parent(self).filter_by( collected_id=photo.id).first() is not None def lock(self): self.locked = True # self.role = Role.query.filter_by(name='Locked').first() db.session.commit() def unlock(self): self.locked = False # self.role = Role.query.filter_by(name='User').first() db.session.commit() def block(self): self.active = False db.session.commit() def unblock(self): self.active = True db.session.commit() def generate_avatar(self): avatar = Identicon() filenames = avatar.generate(text=self.username) self.avatar_s = filenames[0] self.avatar_m = filenames[1] self.avatar_l = filenames[2] db.session.commit() @property def is_admin(self): return self.role.name == '超级管理员' @property def is_active(self): return self.active def can(self, permission_name): permission = Permission.query.filter_by(name=permission_name).first() return permission is not None and self.role is not None and permission in self.role.permissions
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, index=True) # email = db.Column(db.String(254), unique=True, index=True) password_hash = db.Column(db.String(128)) name = db.Column(db.String(30)) website = db.Column(db.String(255)) bio = db.Column(db.String(120)) location = db.Column(db.String(50)) member_since = db.Column(db.DateTime, default=datetime.utcnow) avatar_s = db.Column(db.String(64)) avatar_m = db.Column(db.String(64)) avatar_l = db.Column(db.String(64)) avatar_raw = db.Column(db.String(64)) locked = db.Column(db.Boolean, default=True) active = db.Column(db.Boolean, default=True) role_id = db.Column(db.Integer, db.ForeignKey('role.id')) role = db.relationship('Role', back_populates='users') tasks = db.relationship('Tasks') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.generate_avatar() # self.follow(self) # follow self self.set_role() def __str__(self): return self.name def set_password(self, password): self.password_hash = generate_password_hash(password) def set_role(self): if self.role is None: if self.username == current_app.config['ADMIN_USERNAME']: self.role = Role.query.filter_by(name='超级管理员').first() else: self.role = Role.query.filter_by(name='普通用户').first() db.session.commit() def validate_password(self, password): return check_password_hash(self.password_hash, password) def lock(self): self.locked = True # self.role = Role.query.filter_by(name='Locked').first() db.session.commit() def unlock(self): self.locked = False # self.role = Role.query.filter_by(name='User').first() db.session.commit() def block(self): self.active = False db.session.commit() def unblock(self): self.active = True db.session.commit() def generate_avatar(self): avatar = Identicon() filenames = avatar.generate(text=self.username) self.avatar_s = filenames[0] self.avatar_m = filenames[1] self.avatar_l = filenames[2] db.session.commit() @property def is_admin(self): return self.role.name == '超级管理员' @property def is_active(self): return self.active def can(self, permission_name): permission = Permission.query.filter_by(name=permission_name).first() return permission is not None and self.role is not None and permission in self.role.permissions