Пример #1
0
class Post(db.Model):
    post_id = db.Column(db.Integer, primary_key=True)
    post_content = db.Column(db.String(65535))
    timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True)
    post_marked = db.Column(db.Integer)
    flag_top = db.Column(db.Boolean, default=False)
    flag_star = db.Column(db.Boolean, default=False)

    tag_id = db.Column(db.String, db.ForeignKey('tag.id'))
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    tag = db.relationship('Tag', back_populates='posts')
    author = db.relationship('User', back_populates='posts')
    replies = db.relationship('Reply', backref='post')
Пример #2
0
class User(db.Model):
    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))
    bio = db.Column(db.String(120), default='')
    location = db.Column(db.String(50))
    member_since = db.Column(db.DateTime, default=datetime.utcnow)
    last_ip = db.Column(db.String(30))

    avatar = db.Column(db.String(64))

    confirmed = db.Column(db.Boolean, default=False)
    locked = db.Column(db.Boolean, default=False)
    active = db.Column(db.Boolean, default=True)
    power = db.Column(db.Integer, default=0)
    online = db.Column(db.Integer, default=0)

    public_profile = db.Column(db.Boolean, default=True)

    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))

    role = db.relationship('Role', back_populates='users')
    post = db.relationship('Post', back_populates='author', cascade='all')
    reply = db.relationship('Reply', back_populates='author', cascade='all')
    notifications = db.relationship('Notification',
                                    back_populates='receiver',
                                    cascade='all')

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        self.generate_avatar()
        self.set_role()

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

    def set_role(self):
        if self.role is None:
            if self.email == current_app.config['ALBUMY_ADMIN_EMAIL']:
                self.role = Role.query.filter_by(name='Administrator').first()
            else:
                self.role = Role.query.filter_by(name='User').first()
            db.session.commit()

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)
Пример #3
0
class Reply(db.Model):
    reply_id = db.Column(db.Integer, primary_key=True)
    reply_content = db.Column(db.String(65535))
    timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True)
    show = db.Column(db.Boolean, default=True)

    replied_id = db.Column(db.Integer, db.ForeignKey('reply.reply_id'))
    author_id = db.Column(db.String, db.ForeignKey('user.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('post.post_id'))

    post = db.relationship('Post', back_populates='replies')
    author = db.relationship('User', back_populates='comments')
    replies = db.relationship('Reply', back_populates='replied', cascade='all')
    replied = db.relationship('Reply',
                              back_populates='replies',
                              remote_side=[reply_id])
Пример #4
0
class Forum(db.Model):
    forum_id = db.Column(db.Integer, primary_key=True)
    forum_name = db.Column(db.String(20))
    board = db.relationship('Board', back_populates='forum')

    # get reply of today under this forum
    def get_reply_count(self, date):
        pass
Пример #5
0
class Board(db.Model):
    board_id = db.Column(db.Integer, primary_key=True)
    board_name = db.Column(db.String(20))
    forum = db.relationship('Forum', back_populates='boards')

    # get reply of today under this board
    def get_reply_count(self, date):
        # TODO
        pass

    def get_last_reply(self):
        # TODO
        pass
Пример #6
0
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': ['VIEW', 'MARK'],
            'User': ['POST', 'REPLY', 'VIEW', 'MARK', 'GRADE'],
            'A1Mod': ['POST', 'REPLY', 'VIEW', 'MARK', 'GRADE', 'A1MODERATE'],
            'AMod': ['POST', 'REPLY', 'VIEW', 'MARK', 'GRADE', 'AMODERATE'],
            'BMod': ['POST', 'REPLY', 'VIEW', 'MARK', 'GRADE', 'BMODERATE'],
            'B1Mod': ['POST', 'REPLY', 'VIEW', 'MARK', 'GRADE', 'B1MODERATE'],
            'AllModerator':
            ['POST', 'REPLY', 'VIEW', 'MARK', 'GRADE', 'MODERATE'],
            'Administrator': [
                'POST', 'REPLY', 'VIEW', 'MARK', 'GRADE', '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()
Пример #7
0
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')
Пример #8
0
class Tag(db.Model):
    tag_id = db.Column(db.Integer, primary_key=True)
    tag_name = db.Column(db.String(20))

    posts = db.relationship('Post', backref='tag')