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 update_article(slug, **kwargs): article = Article.query.filter_by(slug=slug, author_id=current_user.profile.id).first() if not article: raise InvalidUsage.article_not_found() article.update(updatedAt=dt.datetime.utcnow(), **kwargs) article.save() return article
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 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 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 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 register_user(username, password, email, **kwargs): 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 get_article(slug): article = Article.query.filter_by(slug=slug).first() if not article: raise InvalidUsage.article_not_found() return article
def get_profile(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() return user.profile