def update_post_by_id(id): post = PostService.get_post_by_id(id) try: data = request.get_data() data = json.loads(data) post.title = data['title'] post.content = data['content'] post.category_id = data['category_id'] tags = data['tags'] post.region = data['region'] post.image_header_id = data['image_header_id'] except Exception: raise ServiceException(ErrorCode.PARAM_ERROR, 'param error') tag_ids = [] if tags is not None and tags != '': tags = tags.split(",") for tag_name in tags: try: tag = GroupService.get_tag_by_name(tag_name) except ServiceException: tag = GroupService.insert_tag(tag_name) tag_ids.append(str(tag.id)) post.tag_ids = ",".join(tag_ids) post = PostService.insert_post(post) return jsonify(json_resp(data=model2dict(post)))
def get(self, param1=None): try: cid = self.get_query_argument("cid") except: self.write(json.dumps({ "status": 0, "message": "course id needed" })) return auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) return data = {} data["username"] = msg[1]["username"] data["course_id"] = cid if param1 is None: ps = PostService() response = ps.get_questions(data) self.write(json.dumps(response)) else: ps = PostService() response = ps.get_post(param1, data) self.write(json.dumps(response))
def put(self, param1=None, param2=None, param3=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return if param1 and not param2 and not param3: ps = PostService() response = ps.edit_post(param1, data) self.write(json.dumps(response)) elif param1 and param2 and param3: ps = PostService() response = ps.edit_reply(param3, data) self.write(json.dumps(response))
def list_drafts(): posts = PostService.list_drafts() posts = models2dict(posts) for post in posts: del post['content'] post['category'] = GroupService.get_category_by_id( post['category_id']).name tags_str = post['tag_ids'] tags = tags_str.split(",") L = [] if tags_str is not None and tags_str != '': for tag_id in tags: tag_id = int(tag_id) tag_name = GroupService.get_tag_by_id(tag_id).name L.append(tag_name) post['tags'] = L return jsonify(json_resp(data=posts))
def get_post_by_id(id): post = PostService.get_post_by_id(id) post = model2dict(post) post['category'] = GroupService.get_category_by_id( post['category_id']).name L = [] if post['tag_ids'] is None or post['tag_ids'] == '': post['tags'] = [] else: tag_ids = post['tag_ids'].split(',') for id in tag_ids: tag_name = GroupService.get_tag_by_id(id).name L.append(tag_name) post['tags'] = L post['image_url'] = ImageService.get_image_by_id( post['image_header_id']).url return jsonify(json_resp(data=post))
def delete(self, param1=None, param2=None, param3=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) if param1 and not param2 and not param3: ps = PostService() response = ps.delete_post(param1) self.write(json.dumps(response)) elif param1 and param2 and param3: ps = PostService() response = ps.delete_reply(param3) self.write(json.dumps(response))
def count_drafts(): cnt = PostService.count_drafts() data = {'num': cnt} return jsonify(json_resp(data=data))
def offline_post_by_id(id): post = PostService.change_post_status(id, Post.Status.DRAFT.value) return jsonify(json_resp(data=model2dict(post)))
def delete_post_by_id(id): post = PostService.change_post_status(id, Post.Status.DELETED.value) return jsonify(json_resp(data=model2dict(post)))
from flask import Blueprint, request from service.post_service import PostService import json from flask import jsonify post_blueprint = Blueprint('post', 'post', url_prefix='/api/post') service = PostService('files/cnn.h5', 'files/vocab.txt') @post_blueprint.route('/analyze', methods=['POST']) def analyze_post(): data = json.loads(request.data) print(data) return jsonify(service.analyze(data))