from project import db, bcrypt from flask_login import UserMixin LikersLikee = db.Table( 'likes', db.Column('id', db.Integer, primary_key=True), db.Column('likee_id', db.Integer, db.ForeignKey('users.id', ondelete="cascade")), db.Column('liker_id', db.Integer, db.ForeignKey('users.id', ondelete="cascade")), db.CheckConstraint('liker_id != likee_id', name="no_self_like")) # TeamUsers = db.Table('teams_users', # db.Column('id', # db.Integer, # primary_key=True), # db.Column('team_id', # db.Integer, # db.ForeignKey('teams.id', ondelete='cascade')), # db.Column('user_id', # db.Integer, # db.ForeignKey('users.id', ondelete='cascade')), # db.Column('status', # db.Integer)) class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.Text, unique=True)
from project import db, bcrypt from flask_login import UserMixin from project.messages.models import Message FollowersFollowee = db.Table( 'follows', db.Column('id', db.Integer, primary_key=True), db.Column('followee_id', db.Integer, db.ForeignKey('users.id', ondelete="cascade")), db.Column('follower_id', db.Integer, db.ForeignKey('users.id', ondelete="cascade")), db.CheckConstraint('follower_id != followee_id', name="no_self_follow")) class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.Text, unique=True) username = db.Column(db.Text, unique=True) image_url = db.Column(db.Text) header_image_url = db.Column(db.Text) bio = db.Column(db.Text) location = db.Column(db.Text) password = db.Column(db.Text) full_name = db.Column(db.Text) messages = db.relationship('Message', backref='user', lazy='dynamic') followers = db.relationship( "User", secondary=FollowersFollowee,
class Friendship(db.Model): """Provide a mutual friendship for two people.""" __tablename__ = 'friendships' __table_args__ = (db.CheckConstraint('left_user_id <> right_user_id'),) left_user_id = db.Column( db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), primary_key=True ) right_user_id = db.Column( db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), primary_key=True ) action_user_id = db.Column( db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), nullable=False ) created_at = db.Column(db.DateTime, server_default=utcnow()) updated_at = db.Column( db.DateTime, server_default=utcnow(), onupdate=utcnow() ) state = db.Column(SAFriendshipState, nullable=False) left_user = db.relationship('User', foreign_keys=left_user_id) right_user = db.relationship('User', foreign_keys=right_user_id) action_user = db.relationship('User', foreign_keys=action_user_id) def __repr__(self): try: return '<{} {}, {}>'.format( self.__class__.__name__, self.left_user.first_name, self.right_user.first_name ) except AttributeError: return super().__repr__() @classmethod def get(cls, id_1, id_2): """Given the composite primary key, return the query which finds the relationship of two people. """ return cls.query.filter(db.or_( db.and_(cls.left_user_id == id_1, cls.right_user_id == id_2), db.and_(cls.left_user_id == id_2, cls.right_user_id == id_1) )) @classmethod def build(cls, id_1, id_2, id_3, state): """Build a mutual relationship.""" return ( cls(left_user_id=id_1, right_user_id=id_2, action_user_id=id_3, state=state), cls(left_user_id=id_2, right_user_id=id_1, action_user_id=id_3, state=state), ) @staticmethod def update(iter, action_user_id, state): """ :param iter: Iterable :class:`.Friendship` model instances. """ for i in range(2): iter[i].action_user_id = action_user_id iter[i].state = state return iter @classmethod def delete(cls, id_1, id_2): """Remove the mutual relationship.""" return cls.get(id_1, id_2).delete(synchronize_session=False)