Exemplo n.º 1
0
 def id_valid(self, _id):
     if isinstance(_id, str):
         raise InvalidArgumentException("错误的id数据类型")
     ret = execute_query_sql(SelectMap.user_valid_by_id, (_id, ),
                             lambda c: c.fetchone())
     if not ret and _id != 0:
         raise InvalidArgumentException("用户不存在")
Exemplo n.º 2
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)
Exemplo n.º 3
0
 def user_id_valid(self, user_id):
     ret = execute_query_sql(SelectMap.user_valid_by_id, (user_id, ),
                             lambda c: c.fetchone())
     if not ret:
         raise InvalidArgumentException("用户不存在")
     if getattr(request, "user")["permission"] != 255 and getattr(
             request, "user")["id"] != user_id:
         raise InvalidArgumentException("此用户权限不足以创建为其他人创建文章")
Exemplo n.º 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("已经点赞过了;数据不存在")
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 def picture_valid(self, picture):
     media_dir = current_app.config["MEDIA_DIR"]
     file_dir = os.path.join(media_dir, picture)
     if os.path.isfile(file_dir):
         setattr(self, "picture", os.path.join(current_app.config["MEDIA_URL"], picture))
         return
     raise InvalidArgumentException("图片不存在!请先上传")
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def content_valid(self, content):
     if len(content) > 1024:
         raise InvalidArgumentException("评论过长")
     description = content.replace("<", "&lt;").replace(">",
                                                        "&gt;").replace(
                                                            " ", "&nbsp;")
     setattr(self, "content", description)
Exemplo n.º 9
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)
Exemplo n.º 10
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)
Exemplo n.º 11
0
 def end_time_valid(self, end_time):
     now = datetime.date.today()
     end_time = datetime.date.fromisoformat(end_time)
     start_time = getattr(self, "start_time")
     if isinstance(start_time, str):
         start_time = datetime.date.fromisoformat(start_time)
     if end_time < start_time or end_time < now:
         raise InvalidArgumentException("错误的end_time")
     setattr(self, "end_time", end_time)
Exemplo n.º 12
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("数据不存在")
Exemplo n.º 13
0
 def picture_valid(self, picture):
     import os
     if picture == '':
         setattr(
             self, "picture",
             os.path.join(current_app.config["MEDIA_URL"],
                          "default_blog.jpg"))
     if is_file_exist(picture):
         setattr(self, "picture",
                 os.path.join(current_app.config["MEDIA_URL"], picture))
         return
     raise InvalidArgumentException("图片不存在 请先上传!")
Exemplo n.º 14
0
 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)
Exemplo n.º 15
0
 def delete(self):
     response = Response()
     try:
         _id = request.json["id"]
         comment_count = execute_sql(DeleteMap.comment_by_id, (_id, ))
         if comment_count == 0:
             raise InvalidArgumentException("删除失败!")
         reply_count = execute_sql(DeleteMap.reply_by_comment_id, (_id, ))
         response.data = {
             "comment_count": comment_count,
             "reply_count": reply_count,
             "msg": "ok"
         }
     except Exception as e:
         init_key_error_handler(response, e, "信息:")
     return jsonify(response.dict_data)
Exemplo n.º 16
0
 def inner(*args, **kwargs):
     response = Response()
     key = current_app.config["AUTH_CODE_SESSION_KEY"]
     try:
         auth_code = session[key].lower()
         code = request.args.get(key).lower()
         if code != auth_code:
             raise InvalidArgumentException()
         ret = func(*args, **kwargs)
         session.clear()
         return ret
     except (KeyError, AttributeError):
         response.code = FORMAT_ERROR
         response.errno = 1
         response.data = {"msg": "验证码已失效 重新输入"}
     except InvalidArgumentException:
         response.code = FORMAT_ERROR
         response.errno = 1
         response.data = {"msg": "验证码校验错误"}
     return jsonify(response.dict_data)
Exemplo n.º 17
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)
Exemplo n.º 18
0
 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)
Exemplo n.º 19
0
 def id_valid(self, _id):
     ret = execute_query_sql(SelectMap.course_by_id, [_id, ], lambda c: c.fetchone())
     if ret == ():
         raise InvalidArgumentException("课程不存在")
Exemplo n.º 20
0
 def content_valid(self, content):
     bs = BeautifulSoup(content, "html.parser")
     if bs.script or bs.findAll(
             text=lambda text: isinstance(text, Comment)):
         raise InvalidArgumentException("错误的content内容")
Exemplo n.º 21
0
 def blog_id_valid(self, blog_id):
     blog = fetchone_dict(SelectMap.blog_by_id, (blog_id, ), GeneralObject)
     if blog is None:
         raise InvalidArgumentException("博客不存在")
Exemplo n.º 22
0
 def id_valid(self, _id):
     if isinstance(_id, str):
         raise InvalidArgumentException("id的格式不能为字符串")
Exemplo n.º 23
0
 def offset_valid(self, offset):
     if isinstance(offset, str):
         raise InvalidArgumentException("offset的格式不能为字符串")
Exemplo n.º 24
0
 def start_time_valid(self, start_time):
     now = datetime.date.today()
     start_time = datetime.date.fromisoformat(start_time)
     if start_time < now:
         raise InvalidArgumentException("开始时间必须大于或等于当前时间(1分钟的误差)")
     setattr(self, "start_time", start_time)
Exemplo n.º 25
0
 def picture_valid(self, picture):
     if not is_file_exist(picture):
         raise InvalidArgumentException("图片文件不存在,请先上传!")
     import os
     setattr(self, "picture", os.path.join(current_app.config["MEDIA_URL"], picture))
Exemplo n.º 26
0
 def sequence_valid(self, sequence):
     ret = execute_query_sql(SelectMap.action_by_course_id, [getattr(self, "id"), ], lambda c: c.fetchall())
     for action in ret:
         if action[2] == sequence:
             raise InvalidArgumentException("课程的顺序重复!")
Exemplo n.º 27
0
 def _page_valid(self, page):
     if page < 0:
         raise InvalidArgumentException("page小于1")