class Tags(Model): __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) tagname = db.Column(db.String(100)) def __init__(self, tagname): db.Model.__init__(self, tagname=tagname) def __repr__(self): return self.tagname
class UserProfile(Model, SurrogatePK): __tablename__ = 'userprofile' # id is needed for primary join, it does work with SurrogatePK class id = db.Column(db.Integer, primary_key=True) user_id = reference_col('users', nullable=False) user = relationship('User', backref=db.backref('profile', uselist=False)) follows = relationship('UserProfile', secondary=followers_assoc, primaryjoin=id == followers_assoc.c.follower, secondaryjoin=id == followers_assoc.c.followed_by, backref='followed_by', lazy='dynamic') def __init__(self, user, **kwargs): db.Model.__init__(self, user=user, **kwargs) def is_following(self, profile): return bool( self.follows.filter( followers_assoc.c.followed_by == profile.id).count()) def follow(self, profile): if self is not profile and not self.is_following(profile): self.follows.append(profile) return True return False def unfollow(self, profile): if self is not profile and self.is_following(profile): self.follows.remove(profile) return True return False @property def following(self): if current_user: return current_user.profile.is_following(self) return False @property def username(self): return self.user.username @property def bio(self): return self.user.bio @property def image(self): return self.user.image @property def email(self): return self.user.email
class Comment(Model, SurrogatePK): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True) body = Column(db.Text) createdAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updatedAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) author_id = reference_col('userprofile', nullable=False) author = relationship('UserProfile', backref=db.backref('comments')) article_id = reference_col('article', nullable=False) def __init__(self, article, author, body, **kwargs): db.Model.__init__(self, author=author, body=body, article=article, **kwargs)
class Article(SurrogatePK, Model): __tablename__ = 'article' id = db.Column(db.Integer, primary_key=True) slug = Column(db.Text, unique=True) title = Column(db.String(100), nullable=False) description = Column(db.Text, nullable=False) body = Column(db.Text) createdAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updatedAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) author_id = reference_col('userprofile', nullable=False) author = relationship('UserProfile', backref=db.backref('articles')) favoriters = relationship( 'UserProfile', secondary=favoriter_assoc, backref='favorites', lazy='dynamic') tagList = relationship( 'Tags', secondary=tag_assoc, backref='articles') comments = relationship('Comment', backref=db.backref('article'), lazy='dynamic') def __init__(self, author, title, body, description, slug=None, **kwargs): db.Model.__init__(self, author=author, title=title, description=description, body=body, slug=slug or slugify(title), **kwargs) def favourite(self, profile): if not self.is_favourite(profile): self.favoriters.append(profile) return True return False def unfavourite(self, profile): if self.is_favourite(profile): self.favoriters.remove(profile) return True return False def is_favourite(self, profile): return bool(self.query.filter(favoriter_assoc.c.favoriter == profile.id).count()) def add_tag(self, tag): if tag not in self.tagList: self.tagList.append(tag) return True return False def remove_tag(self, tag): if tag in self.tagList: self.tagList.remove(tag) return True return False @property def favoritesCount(self): return len(self.favoriters.all()) @property def favorited(self): if current_user: profile = current_user.profile return self.query.join(Article.favoriters).filter(UserProfile.id == profile.id).count() == 1 return False
# coding: utf-8 import datetime as dt from flask_jwt_extended import current_user from slugify import slugify from sayso.database import (Model, SurrogatePK, db, Column, reference_col, relationship) from sayso.profile.models import UserProfile favoriter_assoc = db.Table("favoritor_assoc", db.Column("favoriter", db.Integer, db.ForeignKey("userprofile.id")), db.Column("favorited_article", db.Integer, db.ForeignKey("article.id"))) tag_assoc = db.Table("tag_assoc", db.Column("tag", db.Integer, db.ForeignKey("tags.id")), db.Column("article", db.Integer, db.ForeignKey("article.id"))) class Tags(Model): __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) tagname = db.Column(db.String(100)) def __init__(self, tagname): db.Model.__init__(self, tagname=tagname) def __repr__(self): return self.tagname
from flask_jwt_extended import current_user from sayso.database import (Model, SurrogatePK, db, reference_col, relationship) followers_assoc = db.Table( "followers_assoc", db.Column("follower", db.Integer, db.ForeignKey("userprofile.user_id")), db.Column("followed_by", db.Integer, db.ForeignKey("userprofile.user_id"))) class UserProfile(Model, SurrogatePK): __tablename__ = 'userprofile' # id is needed for primary join, it does work with SurrogatePK class id = db.Column(db.Integer, primary_key=True) user_id = reference_col('users', nullable=False) user = relationship('User', backref=db.backref('profile', uselist=False)) follows = relationship('UserProfile', secondary=followers_assoc, primaryjoin=id == followers_assoc.c.follower, secondaryjoin=id == followers_assoc.c.followed_by, backref='followed_by', lazy='dynamic') def __init__(self, user, **kwargs): db.Model.__init__(self, user=user, **kwargs) def is_following(self, profile): return bool(