Ejemplo n.º 1
0
class Category(Model):
    __tablename__ = 'categories'

    id = db.Column(db.Integer, primary_key=True)
    catname = db.Column(db.String(100))

    parents = db.relationship(
        'Category',
        secondary=category_tree,
        primaryjoin=(category_tree.c.parent_id == id),
        secondaryjoin=(category_tree.c.children_id == id),
        backref=db.backref('children_categories', lazy='dynamic'),
        lazy='dynamic')

    def __init__(self, catname):
        db.Model.__init__(self, catname=catname)

    def __repr__(self):
        return self.catname

    def add_children(self, children):
        if children not in self.parents:
            self.parents.append(children)
            return True
        return False
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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', unique=True, 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 __repr__(self):
        return '<UserProfile %r>' % self.user_id

    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
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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())
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
    description = Column(db.Text, nullable=False)
    createdAt = Column(db.DateTime, default=dt.datetime.utcnow)
    moderators = relationship('UserProfile',
                              secondary=moderator_assoc,
                              backref=db.backref('mod_organization'),
                              lazy='dynamic')
    members = relationship('UserProfile',
                           secondary=member_assoc,
                           backref=db.backref('mem_organization'),
                           lazy='dynamic')
    pending_articles = relationship('Article',
                                    secondary=article_assoc,
                                    backref=db.backref('article_organization'),
                                    lazy='dynamic')

    def __init__(self, name, description, slug, **kwargs):
        db.Model.__init__(self,
                          name=name,
                          description=description,
                          slug=slugify(slug),
                          **kwargs)

    def add_moderator(self, profile):
        if profile not in self.moderators:
            self.moderators.append(profile)
            return True
        return False

    def add_member(self, profile):
        if profile not in self.members:
            self.members.append(profile)
            return True
        return False

    def remove_member(self, profile):
        if profile in self.members:
            self.members.remove(profile)
            return True
        return False

    def update_slug(self, slug):
        if slug != self.slug:
            self.slug = slug
            return True
        return False

    def is_member(self, profile):
        if profile in self.members:
            return True
        return False

    def moderator(self, profile):
        if profile in self.moderators:
            return True
        return False

    def promote(self, user_profile):
        if user_profile in self.members:
            self.members.remove(user_profile)
            self.moderators.append(user_profile)
            return True
        return False

    def request_review(self, article):
        if article not in self.pending_articles:
            self.pending_articles.append(article)
            return True
        return False

    def remove_review_status(self, article):
        if article in self.pending_articles:
            self.pending_articles.remove(article)
            return True
        return False
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
class Tags(Model):
    __tablename__ = 'tags'

    id = db.Column(db.Integer, primary_key=True)
    tagname = db.Column(db.String(100))
    description = db.Column(db.Text)
    slug = db.Column(db.String(100))
    icon = db.Column(db.String(50))
    modSetting = db.Column(db.Integer, nullable=False, default=1)

    tagFollowers = db.relationship('UserProfile',
                                   secondary=tag_follower_assoc,
                                   lazy='subquery',
                                   backref=db.backref('followed_tags',
                                                      lazy='dynamic'))
    moderators = db.relationship('UserProfile',
                                 secondary=tag_moderators_assoc,
                                 lazy='subquery',
                                 backref=db.backref('moderated_tags',
                                                    lazy='dynamic'))
    needReviewArticles = db.relationship('Article',
                                         secondary=tag_needReviewArticle_assoc,
                                         lazy='subquery',
                                         backref=db.backref('needReviewTags',
                                                            lazy='dynamic'))

    def __init__(self,
                 tagname,
                 description=None,
                 slug=None,
                 icon=None,
                 **kwargs):
        db.Model.__init__(self,
                          tagname=tagname,
                          description=description,
                          slug=slug or slugify(tagname),
                          icon=icon,
                          **kwargs)

    def __repr__(self):
        return self.tagname

    def follow(self, profile):
        if not self.is_follow(profile):
            self.tagFollowers.append(profile)
            return True
        return False

    def unfollow(self, profile):
        if self.is_follow(profile):
            self.tagFollowers.remove(profile)
            return True
        return False

    def is_follow(self, profile):
        if profile in self.tagFollowers:
            return True
        else:
            return False

    def is_moderator(self, profile):
        if profile in self.moderators:
            return True
        else:
            return False

    def addModerator(self, profile):
        if not self.is_moderator(profile):
            self.moderators.append(profile)
            return True
        return False

    @property
    def following(self):
        if current_user:
            return self.is_follow(current_user.profile)
        return False

    @property
    def moderator(self):
        if current_user:
            return self.is_moderator(current_user.profile)
        else:
            return False
Ejemplo n.º 10
0
# 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"),
Ejemplo n.º 11
0
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(
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
# 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
Ejemplo n.º 14
0
# 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")))

Ejemplo n.º 15
0
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)
    coverImage = Column(db.Text)
    createdAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    updatedAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    needsReview = Column(db.Boolean, nullable=False, default=False)
    isPublished = Column(db.Boolean, nullable=False)
    views = Column(db.Integer, nullable=False, default=0)

    author_id = reference_col('userprofile', nullable=False)
    author = relationship('UserProfile', backref=db.backref('articles'))
    favoriters = relationship('UserProfile',
                              secondary=favoriter_assoc,
                              backref='favorites',
                              lazy='dynamic')
    bookmarkers = relationship('UserProfile',
                               secondary=bookmarker_assoc,
                               backref='bookmarks',
                               lazy='dynamic')

    tagList = relationship('Tags', secondary=tag_assoc, backref='articles')

    comments = relationship('Comment',
                            backref=db.backref('article'),
                            lazy='dynamic')

    org_articles = relationship('Organization',
                                secondary=org_assoc,
                                backref=db.backref('org_article'))

    def __init__(self,
                 author,
                 title,
                 body,
                 description,
                 coverImage,
                 slug=None,
                 **kwargs):
        db.Model.__init__(self,
                          author=author,
                          title=title,
                          description=description,
                          body=body,
                          coverImage=coverImage,
                          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())

    #Function to bookmark an article
    def bookmark(self, profile):
        if not self.is_bookmarked(profile):
            self.bookmarkers.append(profile)
            return True
        return False

    #Function to remove bookmark on an article
    def unbookmark(self, profile):
        if self.is_bookmarked(profile):
            self.bookmarkers.remove(profile)
            return True
        return False

    #Function to check if a current bookmark already exists for a particular article and user
    def is_bookmarked(self, profile):
        return bool(
            self.query.filter(
                db.and_(
                    bookmarker_assoc.c.bookmarker == profile.id,
                    bookmarker_assoc.c.bookmarked_article == self.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

    def add_organization(self, articles):
        self.needsReview = False
        self.org_articles.append(articles)
        return True

    def add_needReviewTag(self, tag):
        self.needReviewTags.append(tag)
        return True

    def remove_needReviewTag(self, tag):
        if tag in self.needReviewTags:
            self.needReviewTags.remove(tag)
            return True
        return False

    def is_allTagReviewed(self):
        return self.needReviewTags.count() == 0

    def set_needsReview(self, val):
        self.needsReview = val
        return self.needsReview

    @property
    def favoritesCount(self):
        return len(self.favoriters.all())

    @property
    def commentsCount(self):
        return len(self.comments.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

    @property
    def bookmarked(self):
        return self.query.filter(Article.id == self.id).join(
            Article.bookmarkers).filter_by(
                id=current_user.profile.id).count() == 1
Ejemplo n.º 16
0
# 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")))