def invite_moderator(slug, username): tag = Tags.query.filter_by(slug=slug).first() profile = current_user.profile if not current_user.isAdmin and not tag.isModerator(profile): raise InvalidUsage.not_admin_or_moderator() toBeAddedUser = User.query.filter_by(username=username).first() if not toBeAddedUser: raise InvalidUsage.user_not_found() tag.addModerator(toBeAddedUser.profile) tag.save() return toBeAddedUser
def submit_article_for_review(org_slug, slug): organization = Organization.query.filter_by(slug=org_slug).first() if not organization: raise InvalidUsage.organization_not_found() article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() article.needsReview = True article.save() organization.request_review(article) organization.save() return article
def update_article(slug, **kwargs): article = Article.query.filter_by(slug=slug, author_id=current_identity.profile.id).first() if not article: raise InvalidUsage.article_not_found() article.update(updatedAt=dt.datetime.utcnow, **kwargs) article.save() return article
def follow_user(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage(**USER_NOT_FOUND) current_identity.profile.follow(user.profile) print(user.profile.following) return user.profile
def get_article(slug): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() article.update(views=article.views+1) article.save() return article
def update_tag(slug, **kwargs): tag = Tags.query.filter_by(slug=slug).first() if not tag: raise InvalidUsage.tag_not_found() tag.update(**kwargs) tag.save() return tag
def make_comment_on_article(slug, body, **kwargs): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage(**ARTICLE_NOT_FOUND) comment = Comment(article, current_identity.profile, body, **kwargs) comment.save() return comment
def unfollow_user(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() current_user.profile.unfollow(user.profile) current_user.profile.save() return user.profile
def make_comment_on_article(slug, body, **kwargs): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() comment = Comment(article, current_user.profile, body, **kwargs) comment.save() return comment
def login_user(email, password, **kwargs): user = User.query.filter_by(email=email).first() if user is not None and user.check_password(password): user.token = create_access_token(identity=user, fresh=True) return user else: raise InvalidUsage.user_not_found()
def login_user(email, password, **kwargs): user = User.query.filter_by(email=email).first() if user is not None and user.check_password(password): print(user.token) return user else: raise InvalidUsage(**USER_NOT_FOUND)
def register_user(username, password, email, **kwargs): try: userprofile = UserProfile( User(username, email, password=password, **kwargs).save()).save() except IntegrityError: db.session.rollback() raise InvalidUsage(**USER_ALREADY_REGISTERED) return userprofile.user
def unbookmark_an_article(slug): profile = current_user.profile article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() article.unbookmark(profile) article.save() return article
def claim_tag(slug): profile = current_user.profile tag = Tags.query.filter_by(slug=slug).first() if not tag: raise InvalidUsage.tag_not_found() tag.addModerator(profile) tag.save() return tag
def unfollow_a_tag(slug): profile = current_user.profile tag = Tags.query.filter_by(slug=slug).first() if not tag: raise InvalidUsage.tag_not_found() tag.unfollow(profile) tag.save() return tag
def review_article(slug, articleSlug): profile = current_user.profile tag = Tags.query.filter_by(slug=slug).first() if not tag: raise InvalidUsage.tag_not_found() if tag not in profile.moderated_tags: raise InvalidUsage.not_moderator() article = Article.query.filter_by(slug=articleSlug).first() if not article: raise InvalidUsage.article_not_found() if article.needsReview: article.remove_needReviewTag(tag) if article.is_allTagReviewed(): article.set_needsReview(False) article.save() return article
def like_comment_on_article(commentId): profile = current_user.profile comment = Comment.query.get(commentId) if not comment: raise InvalidUsage.comment_not_found() comment.like_comment(profile) comment.save() return comment
def delete_comment_on_article(slug, cid): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() comment = article.comments.filter_by(id=cid, author=current_user.profile).first() comment.delete() return '', 200
def favorite_an_article(slug): profile = current_user.profile article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() article.favourite(profile) article.save() return article
def reviewed_article(slug, org_slug, **kwargs): profile = current_user.profile organization = Organization.query.filter_by(slug=org_slug).first() article = Article.query.filter_by(slug=slug).first() if not organization.moderator(profile): raise InvalidUsage.not_admin() if article not in organization.pending_articles: raise InvalidUsage.article_not_found() organization.pending_articles.remove(article) organization.save() article.add_organization(organization) article.save() return '', 200
def unfavorite_an_article(slug): profile = current_identity.profile article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage(**ARTICLE_NOT_FOUND) article.unfavourite(profile) article.save() return article
def register_user(username, password, email, **kwargs): try: userprofile = UserProfile( User(username, email, password=password, **kwargs).save()).save() except IntegrityError: db.session.rollback() raise InvalidUsage.user_already_registered() return userprofile.user
def delete_comment_on_article(slug, cid): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage(**ARTICLE_NOT_FOUND) comment = article.comments.filter_by( id=cid, author=current_identity.profile).first() comment.delete() return '', 200
def unfollow_organization(slug): profile = current_user.profile organization = Organization.query.filter_by(slug=slug).first() if not organization: raise InvalidUsage.organization_not_found() organization.remove_member(profile) organization.save() return organization
def update_organization(slug, old_slug, **kwargs): organization = Organization.query.filter_by(slug=old_slug).first() if not organization: raise InvalidUsage.organization_not_found() organization.update_slug(slug) organization.update(**kwargs) organization.save() return organization
def register_user(username, password, email, **kwargs): print('In register_user,', username) try: userprofile = UserProfile( User(username, email, password=password, **kwargs).save()).save() userprofile.user.token = create_access_token(identity=userprofile.user) except IntegrityError: db.session.rollback() raise InvalidUsage.user_already_registered() return userprofile.user
def make_organization(name, description, slug, **kwargs): try: organization = Organization(name=name, description=description, slug=slug) profile = current_user.profile organization.add_moderator(profile) organization.save() except IntegrityError: db.session.rollback() raise InvalidUsage.slug_already_exists() return organization
def make_comment_on_article(slug, body, comment_id=None, **kwargs): article = Article.query.filter_by(slug=slug).first() if not article and not comment_id: raise InvalidUsage.article_not_found() if comment_id: comment = Comment(None, current_user.profile, body, comment_id, **kwargs) comment.comment_id = comment_id else: comment = Comment(article, current_user.profile, body, comment_id, **kwargs) comment.save() return comment
def get_user_articles(type=None): res = Article.query if type != "all": if type == "drafts": res = res.filter_by(isPublished=False) elif type == "published": res = res.filter_by(isPublished=True) else: raise InvalidUsage.article_not_found() res = res.join( Article.author).join(User).filter_by(username=current_user.username) return res.all()
def make_article(body, title, description, tagList=None): try: article = Article(title=title, description=description, body=body, author=current_identity.profile) except IntegrityError: db.session.rollback() raise InvalidUsage(**UNKONW_ERROR) if tagList is not None: for tag in tagList: mtag = Tags.query.filter_by(tagname=tag).first() if not mtag: mtag = Tags(tag) mtag.save() article.add_tag(mtag) article.save() return article