def delete(self, article_id): """删除""" user_id = get_jwt_identity() user = get_object(User, user_id) article = get_object(ArticleDoc, article_id) ok = False if article: ok = user_delete_article(user, article) result = {"status": 1 if ok else 0, "msg": "ok" if ok else "error"} return result, 200
def post(self): data = request.get_json() schema = ArticleUrlSchema() try: validated_data = schema.load(data) except marshmallow.exceptions.ValidationError as error: message = "请求错误" for msg in error.messages.values(): message = msg[0] resp = {"status": 0, "msg": message, "errors": error.messages} return resp, 500 url = validated_data.get("url") article = create_article({"url": url}) # add article to user user_id = get_jwt_identity() if user_id: user = get_object(User, user_id) user_add_article(user, article) if article.status == 0: task_fetch_url.delay(str(article.id)) return { "status": 1, "msg": "ok", "data": { "id": str(article.id) }, }, 200
def get(self, article_id): article = get_object(ArticleDoc, article_id) if article: status = 1 data = article.json() else: status = 0 data = {} result = {"status": status, "msg": "ok", "data": {"article": data}} return result, 200
def post(self, article_id): """like and unlike""" data = request.get_json() schema = ArticleActionSchema(unknown="EXCLUDE") try: validated_data = schema.load(data) except marshmallow.exceptions.ValidationError as error: message = "请求错误" for msg in error.messages.values(): message = msg[0] resp = {"status": 0, "msg": message, "errors": error.messages} return resp, 200 article = get_object(ArticleDoc, article_id) if article: user_id = get_jwt_identity() user = get_object(User, user_id) action = validated_data.get("action") if action == "unlike": article.update(pull__likes=user) elif action == "like": article.update(add_to_set__likes=[user]) elif action == "add": user_add_article(user, article) article = get_object(ArticleDoc, article_id) result = { "status": 1, "msg": "ok", "data": { "likes_count": len(article.likes) } } return result, 200 else: result = {"status": 0, "msg": "error"} return result, 404
def get(self): user_id = get_jwt_identity() user = get_object(User, user_id) page = int(request.args.get("page", 0)) per_page = int(request.args.get("per_page", 10)) user_articles = UserArticleDoc.objects( user=user).order_by('-created_at') articles = [ ua.article.json(user) for ua in user_articles[page * per_page:(page + 1) * per_page] ] return { "status": 1, "msg": "ok", "data": { "articles": articles }, }, 200
def get(self, article_id): article = get_object(ArticleDoc, article_id) if article: return { "status": 1, "msg": "ok", "data": { "audios": [ f"/media/{article.id}/{part}.mp3" for part in range(len(article.chuncks)) ] }, }, 200 else: return { "status": 0, "msg": "article not found", "data": {}, }, 400