Пример #1
0
 def get(self):
     args = request.args
     course_id = args.get("id", 0)
     response = Response()
     offset = request.args.get("offset", current_app.config["PAGE_OFFSET"])
     offset = int(offset)
     default_url = os.path.join(current_app.config["MEDIA_URL"], "default_course.jpg")
     try:
         user_id = getattr(request, "user", {}).get("id")
         ret_list = fetchall_dict(SelectMap.course_list_by_page, (course_id, offset), GeneralObject)
         ret_json = []
         if user_id:
             for course in ret_list:
                 collect = fetchone_dict(SelectMap.collect_by_course_id, (course.id, user_id),
                                         GeneralObject)
                 course.is_collect = True if collect else False
                 course.collect_id = collect.id if collect else None
                 action = fetchone_dict(SelectMap.action_list_by_course_id, (course.id,), GeneralObject)
                 course.picture = action.picture if action else default_url
                 ret_json.append(course.data)
         else:
             for course in ret_list:
                 course.is_collect = False
                 course.collect_id = None
                 ret_json.append(course.data)
         response.data = {
             "course_list": ret_json, "count": len(ret_json),
             "query_id": ret_list[-1].id,
             "last_query_id": course_id, "page_offset": offset
         }
         return jsonify(response.dict_data)
     except Exception as e:
         init_key_error_handler(response, e, "错误:")
         return jsonify(response.dict_data)
Пример #2
0
 def _set_comment_count(self, blog):
     comment = fetchone_dict(SelectMap.comment_and_reply_count_by_blog,
                             (blog.id, ), GeneralObject)
     blog.comment_count = comment.count
     flag = False
     if hasattr(request, "user"):
         upper = fetchone_dict(SelectMap.upper_by_user_and_blog,
                               (blog.id, request.user["id"]), GeneralObject)
         flag = True if upper else False
     blog.isUpper = flag
Пример #3
0
 def id_valid(self, _id):
     challenge = fetchone_dict(SelectMap.challenge_by_id, [_id, ], ChallengeTemplate)
     if challenge:
         return
     else:
         _id = request.json["id"]
         challenge = fetchone_dict(SelectMap.challenge_by_id, [_id, ], ChallengeTemplate)
         if challenge:
             setattr(self, "id", _id)
             return
     raise InvalidArgumentException("数据不存在")
Пример #4
0
 def blog_id_valid(self, blog_id):
     blog = fetchone_dict(SelectMap.blog_by_id, (blog_id, ), BlogTemplate)
     if blog is None:
         raise InvalidArgumentException("博客不存在")
     user_id = getattr(request, "user")["id"]
     upper = fetchone_dict(SelectMap.upper_by_user_and_blog,
                           (blog_id, user_id), GeneralObject)
     if (upper is None
             and request.method == "POST") or request.method == "DELETE":
         return
     raise InvalidArgumentException("已经点赞过了;数据不存在")
Пример #5
0
 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)
Пример #6
0
 def get(self):
     """
     获取一个特定的挑战的接口 根据course id  请求需要携带一个参数 id 由于挑战 不用登陆也可以访问
     所以不加权,此pageviews每请求一次就会增加一次 在多线程环境下会不准确 但是没关系 问题不大
     :return:
     """
     response = Response()
     try:
         _id = request.args["id"]
         challenge = fetchone_dict(SelectMap.challenge_by_id, [_id, ], ChallengeTemplate)
         if challenge:
             ret = execute_sql(UpdateMap.update_challenge_pageviews, [challenge.id, ])
             if ret == 0:
                 raise InvalidArgumentException("数据不存在")
             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 + 1
             }
             return jsonify(response.dict_data)
         raise InvalidArgumentException("数据不存在")
     except Exception as e:
         init_key_error_handler(response, e, "信息:")
     return jsonify(response.dict_data)
Пример #7
0
 def get(self):
     response = Response()
     try:
         _id = request.args["id"]
         course = fetchone_dict(SelectMap.course_by_id, [_id, ], CourseTemplate)
         if course is None:
             raise UserDoesNotExistException("课程不存在")
         response.data = {
             "id": course.id, "name": course.name,
             "level": course.level, "type": course.type,
             "burning": course.burning
         }
     except KeyError:
         response.code = FORMAT_ERROR
         response.errno = 1
         response.data = {"msg": "缺少id"}
     except UserDoesNotExistException:
         response.code = FORMAT_ERROR
         response.errno = 1
         response.data = {"msg": "课程不存在"}
     except Exception as e:
         response.code = 500
         response.errno = 1
         response.data = {"msg": str(e)}
     return jsonify(response.dict_data)
Пример #8
0
 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)
Пример #9
0
 def _save(self, file, path, filename):
     buf = file.stream.read()
     md5_key = md5(buf)
     file_map = fetchone_dict(SelectMap.file_map, (md5_key, ),
                              GeneralObject)
     if file_map:
         return file_map.filename
     file.save(path)
     with open(path, "wb") as new_f:
         new_f.write(buf)
     execute_sql(InsertMap.file_map, (filename, md5_key), True)
     return filename
Пример #10
0
 def get(self):
     response = Response()
     try:
         _id = request.args["id"]
         blog = fetchone_dict(SelectMap.blog_by_id, (_id, ), BlogTemplate)
         if blog is None:
             raise InvalidArgumentException("数据不存在")
         user = fetchone_dict(SelectMap.user_info_by_user_id,
                              (blog.user_id, ), GeneralObject)
         response.data = blog.__dict__
         response.data.update({
             "nick_name": user.nick_name,
             "gender": user.gender,
             "email": user.email,
             "avatar": user.avatar,
             "permission": user.permission
         })
         req = fetchone_dict(SelectMap.comment_and_reply_count_by_blog,
                             (_id, ), GeneralObject)
         response.data.update({"comment_count": req.count})
     except Exception as e:
         init_key_error_handler(response, e, "信息:")
     return jsonify(response.dict_data)
Пример #11
0
 def delete(self):
     response = Response()
     try:
         _id = request.json["id"]
         b = fetchone_dict(SelectMap.blog_by_id, (_id, ), GeneralObject)
         permission = getattr(request, "user")["permission"]
         if getattr(
                 request,
                 "user")["id"] != b.user_id and permission & ADMIN != ADMIN:
             raise InvalidArgumentException("权限不足")
         ret = execute_sql(DeleteMap.blog_by_id, (_id, ))
         if ret == 0:
             raise InvalidArgumentException("删除失败")
         response.data = {"msg": "ok"}
     except Exception as e:
         init_key_error_handler(response, e, "信息:")
     return jsonify(response.dict_data)
Пример #12
0
 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)
Пример #13
0
 def blog_id_valid(self, blog_id):
     blog = fetchone_dict(SelectMap.blog_by_id, (blog_id, ), GeneralObject)
     if blog is None:
         raise InvalidArgumentException("博客不存在")
Пример #14
0
 def _init_info(self, blog_list, user_id):
     user = fetchone_dict(SelectMap.user_info_by_user_id, (user_id, ),
                          GeneralObject)
     for blog in blog_list:
         blog.nick_name = user.nick_name
         self._set_comment_count(blog)