コード例 #1
0
ファイル: models.py プロジェクト: waynerv/memory-cache
class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    permissions = db.relationship('Permission',
                                  secondary=roles_permissions,
                                  back_populates='roles')
    users = db.relationship('User', back_populates='role')

    @staticmethod
    def init_role():
        roles_permissions_map = {
            'Locked': ['FOLLOW', 'COLLECT'],
            'User': ['FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD'],
            'Moderator':
            ['FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD', 'MODERATE'],
            'Administrator': [
                'FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD', 'MODERATE',
                'ADMINISTER'
            ]
        }

        for role_name in roles_permissions_map:
            role = Role.query.filter(Role.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(
                    Permission.name == permission_name).first()
                if permission is None:
                    permission = Permission(name=permission_name)
                    db.session.add(permission)
                role.permissions.append(permission)
        db.session.commit()
コード例 #2
0
ファイル: models.py プロジェクト: waynerv/memory-cache
class Photo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.Text)
    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())
    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, delete')
    tags = db.relationship('Tag', secondary=tagging, back_populates='photos')
    collectors = db.relationship('Collect',
                                 back_populates='collected',
                                 cascade='all')
    flag = db.Column(db.Integer, default=0)
    can_comment = db.Column(db.Boolean, default=True)
コード例 #3
0
ファイル: models.py プロジェクト: waynerv/memory-cache
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    username = db.Column(db.String(20), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(254), unique=True, index=True)
    website = db.Column(db.String(255))
    bio = db.Column(db.Text)
    location = db.Column(db.String(30))
    member_since = db.Column(db.DateTime, default=datetime.utcnow())
    confirmed = db.Column(db.Boolean)
    photos = db.relationship('Photo',
                             back_populates='author',
                             cascade='all, delete')
    notifications = db.relationship('Notification',
                                    back_populates='receiver',
                                    cascade='all, delete')
    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')
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    role = db.relationship('Role', back_populates='users')
    comments = db.relationship('Comment', back_populates='author')
    avatar_raw = db.Column(db.String(64))
    avatar_s = db.Column(db.String(64))
    avatar_m = db.Column(db.String(64))
    avatar_l = db.Column(db.String(64))
    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)
    public_collections = db.Column(db.Boolean, default=True)
    locked = db.Column(db.Boolean, default=False)
    active = db.Column(db.Boolean, default=True)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        self.set_role()
        self.generate_avatar()
        self.follow(self)  # 关注自己

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)

    def set_role(self):
        if self.role is None:
            if self.email == current_app.config['APP_ADMIN_EMAIL']:
                self.role = Role.query.filter(
                    Role.name == 'Administrator').first()
            else:
                self.role = Role.query.filter(Role.name == 'User').first()

    def can(self, permission_name):
        permission = Permission.query.filter(
            Permission.name == permission_name).first()
        return permission is not None and self.role is not None and permission in self.role.permissions

    @property
    def is_admin(self):
        return self.role.name == 'Administrator'

    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]

    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 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:
            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

    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()

    @property
    def is_active(self):
        return self.active

    def block(self):
        self.active = False
        db.session.commit()

    def unblock(self):
        self.active = True
        db.session.commit()
コード例 #4
0
ファイル: models.py プロジェクト: waynerv/memory-cache
class Permission(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    roles = db.relationship('Role',
                            secondary=roles_permissions,
                            back_populates='permissions')
コード例 #5
0
ファイル: models.py プロジェクト: waynerv/memory-cache
class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    photos = db.relationship('Photo', secondary=tagging, back_populates='tags')