def put(self): """ 修改课程的接口 :return: """ response = Response() try: _id = request.json["id"] course = CourseTemplate() response = post(CourseValid, parse, course) if response: return jsonify(response.dict_data) ret = execute_sql(UpdateMap.update_course_by_id, [ course.name, course.type, course.level, course.burning, course.id ]) if ret == 0: raise UserDoesNotExistException("课程不存在") response.data = { "id": course.id, "name": course.name, "type": course.type, "level": course.level, "burning": course.burning } except KeyError as e: init_key_error_handler(response, e, "缺少入参:") except UserDoesNotExistException as e: init_key_error_handler(response, e) return jsonify(response.dict_data)
def post(self): """ 添加帖子的接口 帖子的添加需要携带 user_id content title picture 等键值对 图片需要先上传 content需要不存在注释以及script标签 :return: """ response = Response() blog = BlogTemplate() resp = post(BlogValid, parser, blog) if resp is not None: return resp try: from datetime import datetime now = datetime.now() row_id = execute_sql( InsertMap.blog, (blog.user_id, blog.content, blog.title, blog.picture, now)) if row_id == 0: raise InvalidArgumentException("插入记录失败") response.data = { "id": row_id, "user_id": blog.user_id, "content": blog.content, "title": blog.title, "picture": blog.picture, "create_time": now } except Exception as e: init_key_error_handler(response, e, "信息:") return jsonify(response.dict_data)
def post(self): """ 添加挑战的接口 此接口的content是一个重点 是有格式要求的 要求就是 其中的html不能包含script标签 也不能含有任何的注释 添加挑战的另一个约定就是 start_time和end_time是有格式要求的 必须是2019-11-03这种格式 无法配置 :return: """ challenge = ChallengeTemplate() response = post(ChallengeValid, parse, challenge) if response is None: response = Response() try: number = generate_number(20) ret = execute_sql(InsertMap.challenge, [ challenge.picture, challenge.content, challenge.start_time, challenge.end_time, datetime.datetime.now(), challenge.pageviews, number ]) if ret == 0: raise InvalidArgumentException("数据写入失败") challenge = fetchone_dict(SelectMap.challenge_by_number, [number, ], ChallengeTemplate) response.data = { "id": challenge.id, "picture": challenge.picture, "content": challenge.content, "start_time": challenge.start_time, "end_time": challenge.end_time, "create_time": challenge.create_time, "pageviews": challenge.pageviews } except Exception as e: init_key_error_handler(response, e, "信息:") return jsonify(response.dict_data)
def post(self): comment = CommentTemplate() response = post(CommentValid, parse, comment) if response: return jsonify(response.dict_data) try: response = Response() now = datetime.datetime.now() user_id = getattr(request, "user")["id"] user = fetchone_dict(SelectMap.user_info_by_user_id, (user_id, ), GeneralObject) ret = execute_sql(InsertMap.comment, (comment.content, now, comment.blog_id, user_id, user.nick_name), True) if ret == 0: raise InvalidArgumentException("评论失败") response.data = { "id": ret, "create_time": now, "content": comment.content, "blog_id": comment.blog_id, "user_id": user_id, "nick_name": user.nick_name } except Exception as e: init_key_error_handler(response, e, "信息:") return jsonify(response.dict_data)
def delete(self): upper = UpperTemplate() response = post(UpperValid, parse, upper) if response: return jsonify(response) try: user_id = getattr(request, "user")["id"] response = Response() ret = execute_sql(DeleteMap.upper_by_id, (upper.blog_id, user_id)) if ret == 0: raise InvalidArgumentException("取消失败") execute_sql(UpdateMap.blog_upper_dev, (upper.blog_id, )) response.data = {"msg": "ok"} except Exception as e: init_key_error_handler(response, e, "信息:") return jsonify(response.dict_data)
def post(self): insert_upper = UpperTemplate() response = post(UpperValid, parse, insert_upper) if response: return jsonify(response.dict_data) try: user_id = getattr(request, "user")["id"] old_upper = fetchone_dict(SelectMap.upper_without_delete_flag, (insert_upper.blog_id, user_id), UpperTemplate) if old_upper is not None: response = self._rollback(old_upper) else: response = self._real_insert(insert_upper) execute_sql(UpdateMap.update_blog_upper_by_id, (insert_upper.blog_id, )) except Exception as e: init_key_error_handler(response, e, "信息:") return jsonify(response.dict_data)
def post(self): """ 此方法用来添加一个对应课程的动作 但是此方法不会校验sequence(动作编号),所以请谨慎调用 :return: """ template = ActionTemplate() ret = post(ActionValid, parse, template) response = Response() if ret: return jsonify(ret.dict_data) try: count = execute_sql(InsertMap.action, [ template.id, template.content, template.picture, template.sequence ]) response.data = { "course_id": template.id, "content": template.content, "picture": template.picture, "sequence": template.sequence, "count": count } except Exception as e: init_key_error_handler(response, e) return jsonify(response.dict_data)
def put(self): """ 修改挑战的接口 和post接口相比 需要多传一个id的key 如果id不存在 则无法执行 :return: """ challenge = ChallengeTemplate() response = post(ChallengeValid, parse, template=challenge) if response is None: response = Response() try: _id = challenge.id ret = execute_sql(UpdateMap.update_challenge_by_id, [ challenge.picture, challenge.content, challenge.start_time, challenge.end_time, _id ]) if ret == 0: raise InvalidArgumentException("数据不存在") except InvalidArgumentException as e: response.errno = 1 response.code = 403 response.data = {"msg": str(e)} except Exception as e: init_key_error_handler(response, e, "信息:") return jsonify(response.dict_data)
def _user_list(self, response): req = GeneralObject() resp = post(UserListValid, request.args, req) if resp is not None: response = resp return blog_id = getattr(resp, "blog_id", 0) offset = req.offset page = req.page blog_list = fetchall_dict(SelectMap.blog_list_by_user, (req.id, blog_id, (page - 1) * offset + 1, offset), create_cmp_with_class(_lt)) if blog_list: blog_list.sort() for blog in blog_list: self._set_comment_count(blog) response.data = { "blog_list": [blog.data for blog in blog_list], "count": len(blog_list), "page": page, } else: raise UserDoesNotExistException("数据不存在")
def generic_template(valid_class, parse, template_class): template = template_class() resp = post(valid_class, parse, template) return (False, resp) if resp else (True, template)