Example #1
0
from . import Base
from app import DB, MA

ROLES = DB.Table(
    'auth_group_role', Base.metadata,
    DB.Column('role_id',
              DB.Integer,
              DB.ForeignKey('auth_role.id'),
              primary_key=True),
    DB.Column('group_id',
              DB.Integer,
              DB.ForeignKey('auth_group.id'),
              primary_key=True))


class Role(Base):

    __tablename__ = 'auth_role'

    name = DB.Column(DB.String(128), nullable=False, unique=True)
    description = DB.Column(DB.String(500), nullable=True)


class RoleSchema(MA.ModelSchema):
    class Meta:
        model = Role
Example #2
0
            ("neutral", self.reactions_num(ReactionsType.neutral)),
            ("smile", self.reactions_num(ReactionsType.smile)),
            ("funny", self.reactions_num(ReactionsType.funny))]
        reactions_data = [item for item in reactions_data if item[1] > 0]
        reactions_data = sorted(reactions_data, key=itemgetter(1), reverse=True)
        return reactions_data

    def add_reaction(self, reaction_type):
        """method called after a user made reaction"""
        new_reaction = JokeReaction(joke_id=self.id, reaction_type=reaction_type)
        DB.session.add(new_reaction)
        DB.session.commit()

# Define models
roles_users = DB.Table('roles_users',
        DB.Column('user_id', DB.Integer(), DB.ForeignKey('user.id')),
        DB.Column('role_id', DB.Integer(), DB.ForeignKey('role.id')))

class Role(DB.Model, RoleMixin):
    id = DB.Column(DB.Integer(), primary_key=True)
    name = DB.Column(DB.String(80), unique=True)
    description = DB.Column(DB.String(255))

class User(DB.Model, UserMixin):
    id = DB.Column(DB.Integer, primary_key=True)
    email = DB.Column(DB.String(255), unique=True)
    password = DB.Column(DB.String(255))
    active = DB.Column(DB.Boolean())
    confirmed_at = DB.Column(DB.DateTime())
    roles = DB.relationship('Role', secondary=roles_users,
                            backref=DB.backref('users', lazy='dynamic'))
Example #3
0
from flask_login import UserMixin
from app import DB


followers = DB.Table('follower',
                     DB.Column('follower_id', DB.Integer, DB.ForeignKey('user.id')),
                     DB.Column('followee_id', DB.Integer, DB.ForeignKey('user.id')))


class User(DB.Model, UserMixin):
    id = DB.Column(DB.Integer, primary_key=True)
    name = DB.Column(DB.String(100))
    username = DB.Column(DB.String(30))
    image = DB.Column(DB.String(100))
    password = DB.Column(DB.String(50))
    join_date = DB.Column(DB.DateTime)

    tweets = DB.relationship('Tweet', backref='user', lazy='dynamic')

    following = DB.relationship('User', secondary=followers,
                                primaryjoin=(followers.c.follower_id == id),
                                secondaryjoin=(followers.c.followee_id == id),
                                backref=DB.backref('followers', lazy='dynamic'),
                                lazy='dynamic')

    followed_by = DB.relationship('User', secondary=followers,
                                primaryjoin=(followers.c.followee_id == id),
                                secondaryjoin=(followers.c.follower_id == id),
                                backref=DB.backref('followees', lazy='dynamic'),
                                lazy='dynamic')