예제 #1
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)
class User(SurrogatePK, Model):

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(100), unique=True, nullable=False)
    password = Column(db.Binary(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    updated_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    bio = Column(db.String(300), nullable=True)
    image = Column(db.String(120), nullable=True)
    token = Column(db.String(300), nullable=True, default="")

    def __init__(self, username, email, password=None, **kwargs):
        """Create instance."""
        db.Model.__init__(self, username=username, email=email, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<User({username!r})>'.format(username=self.username)
예제 #3
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())
예제 #4
0
파일: models.py 프로젝트: bz22/bit-next
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)
예제 #5
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
예제 #6
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
예제 #7
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