def put(cls, slug): """ Assign image to specific article instance. """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article does not exist"}, 404 data = image_schema.load(request.files) folder = "article_images" try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) previous_article_image = article.image_url article.image_url = basename article.save_to_db() if previous_article_image: image_helper.delete_image(previous_article_image, folder) return {"message": "Image {} uploaded".format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": "Extension not allowed"}, 400 except Exception: traceback.print_exc() return {"message": "Internal server error"}, 500
def delete(cls, slug: str): """ Delete specific data instance """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 article.delete_from_db() return {"message": "Article deleted"}, 200
def post(cls, slug): """ Unpublish article by unsetting published_date field. """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 article.unpublish() return {"message": "Article has been unpublished"}, 200
def get(cls, slug: str): """ Return specific article data. Only published articles are available via this endpoint """ article = ArticleModel.find_by_slug(slug) if not article or not article.published_date: return {"message": "Article not found"}, 404 return article_schema.dump(article), 200
def get(cls, slug: str): """ Return specific article data. Only published articles are available via this endpoint """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article does not exist"}, 404 if article.published_date: return {"message": "Article already published, it's not in draft list"}, 400 return article_schema.dump(article), 200
def put(cls, slug: str): """ Update specific article instance """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 article_json = request.get_json() article.description = article_json["description"] article.content = article_json["content"] article.save_to_db() return article_schema.dump(article), 200
def post(cls, slug): """Update article title field.""" new_title = request.get_json()["title"] article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 if new_title == article.title: return {"message": "You're trying to change to existing title"} if ArticleModel.find_by_title(new_title): return {"message": "This title is taken"} article.title = new_title article.save_to_db() return {"message": "Title has been changed"}, 200
def post(cls, slug): """Revoke like from article by logged in user.""" article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 user_id = get_jwt_identity() user = UserModel.find_by_id(user_id) if not user in article.likes: return {"message": "You have to like this article before you can revoke like"}, 403 article.likes.remove(user) article.save_to_db() return {"message": "Article with id={} has been disliked by {}".format(article.id, user.username)}, 200
def post(cls, slug): """Like specific article instance by current logged in user.""" article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 user_id = get_jwt_identity() user = UserModel.find_by_id(user_id) if user == article.author: return {"message": "You can't like your own article"}, 403 if user in article.likes: return {"message": "You can't like this article twice"}, 403 article.likes.append(user) article.save_to_db() return {"message": "Article with id={} has been liked by {}".format(article.id, user.username)}, 200
def post(cls, slug): """ Publish article instance by setting it's published_date field. """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article not found"}, 404 if not article.image_url: return {"message": "Article must contain image before publishing"}, 400 if article.tags == []: return {"message": "Article must have at least one tag assigned"}, 400 if article.published_date: return {"message": "Article has been already published"}, 400 article.publish() return {"message": "Article has been published"}, 200
def put(cls, slug): """ Assign tags to specific article instance. """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article does not exist"}, 404 tags = request.get_json()["tags"] article.tags = [] for tag in tags: tag_object = TagModel.get_or_create(name=tag) if not tag_object in article.tags: article.tags.append(tag_object) article.save_to_db() return article_schema.dump(article), 200