示例#1
0
class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))

    users = db.relationship('UserCommon', back_populates='role')

    permissions = db.relationship('Permission',
                                  secondary='role_permission',
                                  back_populates='roles')

    @staticmethod
    def init_role():
        roles_permissions_map = {
            'Locked': ['VISIT'],
            'User': ['VISIT', 'ASK', 'ANSWER', 'REPLY'],
            'Moderator': ['VISIT', 'ASK', 'ANSWER', 'REPLY', 'MODERATE'],
            'Administrator':
            ['VISIT', 'ASK', 'ANSWER', 'REPLY', '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.permission = []
            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)
                #important
                role.permissions.append(permission)
        db.session.commit()
示例#2
0
class UserCommon(db.Model, UserMixin):
    #__abstract__=True
    __tablename__ = "user_common"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    username = db.Column(db.String(30), unique=True)
    email = db.Column(db.String(50), unique=True)
    password_hash = db.Column(db.String(128))
    about = db.Column(db.Text)
    joinDate = db.Column(db.DateTime, default=datetime.utcnow, index=True)
    is_confirmed = db.Column(db.Boolean, default=False)

    avatar_s = db.Column(db.String(64))
    avatar_m = db.Column(db.String(64))
    avatar_l = db.Column(db.String(64))

    collections = db.relationship('Question',
                                  secondary='collections_questions',
                                  back_populates='collectors')
    questions = db.relationship('Question',
                                back_populates='author',
                                cascade='all, delete-orphan')
    answers = db.relationship('Answer', back_populates='author')
    comments = db.relationship('Comment', back_populates='author')

    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    role = db.relationship('Role', back_populates='users')

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

    def set_role(self):
        if self.role is None:
            self.role = Role.query.filter_by(name='User').first()
            db.session.commit()

    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 can(self, permission_name):
        all = Permission.query.all()
        print("-----all: ", all)
        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

    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()
示例#3
0
class KnownRegion(db.Model):
    __tablename__ = 'user_known_regions'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    region_id = db.Column(db.Integer,
                          db.ForeignKey('regions.id'),
                          nullable=False)

    user = db.relationship('User',
                           backref=db.backref('known_regions', lazy=True))
    bcluster = db.relationship('Region',
                               backref=db.backref('known_by_users', lazy=True))
示例#4
0
class Permission(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))

    roles = db.relationship('Role',
                            secondary='role_permission',
                            back_populates='permissions')
示例#5
0
class Tag(db.Model):
    __tablename__ = 'tag'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)

    questions = db.relationship('Question',
                                secondary='tags_questions',
                                back_populates='tags')

    #FIXME: may add this later
    #created_by_id = db.Column(db.Integer, db.ForeignKey('Admin.id'))
    #created_by = db.relationship('Admin', back_populates='tags')

    def delete(self):
        default_tag = Tag.query.get(1)
        questions = self.questions[:]

        for q in questions:
            q.tags.remove(self)
            if len(q.tags == 0):
                q.tags.append(default_tag)

        db.session.delete(self)
        db.session.commit()
示例#6
0
class Answer(db.Model):
    __tablename__ = 'answer'
    id = db.Column(db.Integer, primary_key=True)
    answer = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    #FIXME: do this later
    #last_edit_timestamp = db.Column(db.DateTime)

    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
    question = db.relationship('Question', back_populates='answers')

    author_id = db.Column(db.Integer, db.ForeignKey('user_common.id'))
    author = db.relationship('UserCommon', back_populates='answers')

    comments = db.relationship('Comment',
                               back_populates='answer',
                               cascade='all, delete-orphan')
示例#7
0
class SkippedRanking(db.Model):
    __tablename__ = 'skipped_rankings'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    r1 = db.Column(db.Integer, db.ForeignKey('regions.id'), nullable=False)
    r2 = db.Column(db.Integer, db.ForeignKey('regions.id'), nullable=False)

    user = db.relationship('User',
                           backref=db.backref('skipped_rankings', lazy=True))
示例#8
0
class Comment(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key=True)
    comment = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True)

    author_id = db.Column(db.Integer, db.ForeignKey('user_common.id'))
    author = db.relationship('UserCommon', back_populates='comments')

    answer_id = db.Column(db.Integer, db.ForeignKey('answer.id'))
    answer = db.relationship('Answer', back_populates='comments')

    replied_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
    replied = db.relationship('Comment',
                              back_populates='replies',
                              remote_side=[id])

    replies = db.relationship('Comment',
                              back_populates='replied',
                              cascade='all, delete-orphan')
示例#9
0
class Region(db.Model):
    __tablename__ = 'regions'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    shapefile_id = db.Column(db.String(255), nullable=False)
    cluster_id = db.Column(db.Integer,
                           db.ForeignKey('clusters.id'),
                           nullable=False)

    cluster = db.relationship('Cluster',
                              backref=db.backref('regions', lazy=True))
示例#10
0
class Question(db.Model):
    __tablename__ = "question"
    id = db.Column(db.Integer, primary_key=True)
    question = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True)
    #FIXME: do this later
    #last_edit_timestamp = db.Column(db.DateTime, index=True)

    collectors = db.relationship('UserCommon',
                                 secondary='collections_questions',
                                 back_populates='collections')

    author_id = db.Column(db.Integer, db.ForeignKey('user_common.id'))
    author = db.relationship('UserCommon', back_populates='questions')

    answers = db.relationship('Answer',
                              back_populates='question',
                              cascade='all, delete-orphan')

    tags = db.relationship('Tag',
                           secondary='tags_questions',
                           back_populates='questions')
示例#11
0
class Comment(db.Model):
    __tablename__ = config['TABLE_COMMENTS']

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    story = db.Column(db.String(10), index=True, nullable=False)
    author = db.Column(db.String(100), index=True, nullable=False)
    parent_comment = db.Column(db.Integer,
                               db.ForeignKey(__tablename__ + '.id'))
    comment = db.Column(db.Text, nullable=False)
    children = db.relationship('Comment')

    def __repr__(self):
        return '<Story %r>' % self.url

    @classmethod
    def by_url(cls, url):
        comments = cls.query.filter_by(story=url)
        return comments
示例#12
0
class Ranking(db.Model):
    __tablename__ = 'comparisons'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    poorest = db.Column(db.Integer,
                        db.ForeignKey('regions.id'),
                        nullable=False)
    richest = db.Column(db.Integer,
                        db.ForeignKey('regions.id'),
                        nullable=False)
    rejudged = db.Column(db.Boolean, default=False)

    user = db.relationship('User', backref=db.backref('rankings', lazy=True))

    def __repr__(self):
        return '<Ranking {} {} Poorest {} Richest {}>'.format(
            self.date, self.user_id, self.poorest, self.richest)