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=True) comment_id = Column(db.Integer, db.ForeignKey('comment.id'), nullable=True) parentComment = relationship('Comment', backref=db.backref('parent', remote_side=[id]), lazy='dynamic') comment_likers = relationship('UserProfile', secondary=comment_like_assoc, backref='likes', lazy='dynamic') def __init__(self, article, author, body, comment_id=None, **kwargs): db.Model.__init__(self, author=author, body=body, article=article, **kwargs) #Function to like a comment def like_comment(self, profile): if not self.is_liked(profile): self.comment_likers.append(profile) return True return False #Function to check if a current like already exists for a particular comment and user def is_liked(self, profile): return bool( self.query.filter( db.and_( comment_like_assoc.c.profile_liking_comment == profile.id, comment_like_assoc.c.comment_liked == self.id)).count()) @property def likesCount(self): return len(self.comment_likers.all())
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=True) comment_id = Column(db.Integer, db.ForeignKey('comment.id'), nullable=True) parentComment = relationship('Comment', backref=db.backref('parent', remote_side=[id]), lazy='dynamic') def __init__(self, article, author, body, comment_id=None, **kwargs): db.Model.__init__(self, author=author, body=body, article=article, **kwargs)
import datetime as dt from flask_jwt_extended import current_user from slugify import slugify from conduit.database import (Model, SurrogatePK, db, Column, reference_col, relationship) from conduit.profile.models import UserProfile moderator_assoc = db.Table( "moderator_assoc", db.Column("moderator", db.Integer, db.ForeignKey('userprofile.id')), db.Column("organization", db.Integer, db.ForeignKey('organization.id'))) member_assoc = db.Table( "member_assoc", db.Column("member", db.Integer, db.ForeignKey('userprofile.id')), db.Column("organization", db.Integer, db.ForeignKey('organization.id'))) article_assoc = db.Table( "article_assoc", db.Column("article", db.Integer, db.ForeignKey('article.id')), db.Column("organization", db.Integer, db.ForeignKey('organization.id'))) class Organization(Model, SurrogatePK): __tablename__ = 'organization' id = db.Column(db.Integer, primary_key=True) name = Column(db.String(100), nullable=False) slug = Column(db.Text, nullable=False, unique=True)
# coding: utf-8 import datetime as dt from flask_jwt_extended import current_user from slugify import slugify from conduit.database import (Model, SurrogatePK, db, Column, reference_col, relationship) tag_follower_assoc = db.Table( "tag_follower_assoc", db.Column("tag_id", db.Integer, db.ForeignKey("tags.id"), primary_key=True), db.Column("follower_id", db.Integer, db.ForeignKey("userprofile.id"), primary_key=True)) tag_moderators_assoc = db.Table( "tag_moderators_assoc", db.Column("tag_id", db.Integer, db.ForeignKey("tags.id"), primary_key=True), db.Column("moderator_id", db.Integer, db.ForeignKey("userprofile.id"), primary_key=True)) tag_needReviewArticle_assoc = db.Table( "tag_needReviewArticle_assoc", db.Column("tag_id", db.Integer, db.ForeignKey("tags.id"),
from conduit.database import (Model, SurrogatePK, db, reference_col, relationship) from flask_jwt import current_identity 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( self.follows.filter(
# coding: utf-8 import datetime as dt from flask_jwt_extended import current_user from slugify import slugify from conduit.database import (Model, SurrogatePK, db, Column, reference_col, relationship) from conduit.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
# coding: utf-8 import datetime as dt from flask_jwt_extended import current_user from slugify import slugify from conduit.database import (Model, SurrogatePK, db, Column, reference_col, relationship) from conduit.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"))) category_assoc = db.Table( "category_assoc", db.Column("category", db.Integer, db.ForeignKey("categories.id")), db.Column("article", db.Integer, db.ForeignKey("article.id"))) category_tree = db.Table( "category_tree", db.Column("parent_id", db.Integer, db.ForeignKey("categories.id")), db.Column("children_id", db.Integer, db.ForeignKey("categories.id")))
# coding: utf-8 import datetime as dt from flask_jwt_extended import current_user from slugify import slugify from conduit.database import (Model, SurrogatePK, db, Column, reference_col, relationship) from conduit.profile.models import UserProfile from conduit.tags.models import Tags 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"))) comment_like_assoc = db.Table( "comment_like_assoc", db.Column("profile_liking_comment", db.Integer, db.ForeignKey("userprofile.id")), db.Column("comment_liked", db.Integer, db.ForeignKey("comment.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"))) org_assoc = db.Table( "org_assoc", db.Column("organization", db.Integer, db.ForeignKey("organization.id")), db.Column("article", db.Integer, db.ForeignKey("article.id")))