def post(self): """ 发送课表数据到服务器 地址: /interaction/api/v2/syllabus_collection 方法: POST 参数: 位置: form 必选参数: username 用户账号 token 验证令牌 start_year 学年的开始年份 season 某个学期, 和学分制对应 syllabus 课表的JSON数据 :return: """ self.POST_PARSER.add_argument("username", required=True, location="form") self.POST_PARSER.add_argument("token", required=True, location="form") self.POST_PARSER.add_argument("start_year", type=int, required=True, location="form") self.POST_PARSER.add_argument("season", type=int, required=True, location="form") self.POST_PARSER.add_argument("collection_id", required=True, location="form") self.POST_PARSER.add_argument("syllabus", required=True, location="form") args = self.POST_PARSER.parse_args() user = common.query_single_by_field(models.User, "account", args["username"]) if user is None: return {"error": "user doesn't exist"}, 404 if not check_token(user, args["token"]): return {"error": "token is wrong"}, 401 collector = common.query_single_by_field(models.Collector, "collection_id", args["collection_id"]) if collector is None: # 表明用户输入了错误的collection_id return {"error": "wrong collection_id"}, 404 # 检查学期是否正确 if collector.start_year != args["start_year"] or collector.season != args["season"]: return {"error": "semester doesn't match"}, 400 collection = models.SyllabusCollection.query.filter_by(account=user.account).filter_by(collection_id=args["collection_id"]).first() if collection is not None: # 删除原有记录 status = delete_record(db, collection) if status != True: return {"error": repr(status[1])}, 500 collection = models.SyllabusCollection(collection_id=args["collection_id"], syllabus=args["syllabus"], account=args["username"]) result = common.add_to_db(db, collection) if result == True: return {"id": collection.id} else: return {"error": "commit error in mysql"}, 500
def get(self): self.GET_PARSER.add_argument("username", required=True, location="headers") self.GET_PARSER.add_argument("token", required=True, location="headers") args = self.GET_PARSER.parse_args() user = common.query_single_by_field(models.User, "account", args["username"]) if user is None: return {"error": "user doesn't exist"}, 404 token_check = {"uid": user.id, "token": args["token"]} if not common.check_token(token_check): return {"error": "token is wrong"}, 401 collectors = models.Collector.query.filter_by(uid=user.id).all() result = [] for collector in collectors: count = models.SyllabusCollection.query.with_entities( models.SyllabusCollection.collection_id).filter_by( collection_id=collector.collection_id).count() result.append({ "collection_id": collector.collection_id, "start_year": collector.start_year, "season": collector.season, "count": count }) # collectors = [ dict(collection_id=x.collection_id, start_year=x.start_year, season=x.season) for x in collectors ] return {"collection_ids": result}
def new_or_update_user(account, token): """ 插入新的用户, 或者是更新旧用户 :param account: :param token: :return: """ user = common.query_single_by_field(User, "account", account) if user is None: # 新用户 user = User(account=account, token=token) ret_val = common.add_to_db(db, user) if ret_val != True: print(ret_val[1]) return False else: return user # 老用户 user.token = token # 更新token ret_val = common.add_to_db(db, user) if ret_val != True: print(ret_val[1]) return False else: return user
def get(self): """ 申请人获取用户已经上传的课表数据 地址: /interaction/api/v2/syllabus_collection 方法: GET 参数: 位置: headers 必须参数: username 用户账号 token 验证令牌 collectionID 之前申请到的获取id :return: """ self.GET_PARSER.add_argument("username", required=True, location="headers") self.GET_PARSER.add_argument("token", required=True, location="headers") # header里面的键名不能有下划线 self.GET_PARSER.add_argument("collectionID", required=True, location="headers") args = self.GET_PARSER.parse_args() user = common.query_single_by_field(models.User, "account", args["username"]) if user is None: return {"error": "user doesn't exist"}, 404 if not check_token(user, args["token"]): return {"error": "token is wrong"}, 401 collector = common.query_single_by_field(models.Collector, "collection_id", args["collectionID"]) if collector is None: # 表明用户输入了错误的collection_id return {"error": "wrong collection_id"}, 404 # 检查权限 if collector.uid != user.id: return {"error": "have not the permission"}, 403 collections = models.SyllabusCollection.query.filter_by(collection_id=args["collectionID"]).all() collections = [ dict(id=x.id, account=x.account, syllabus=x.syllabus) for x in collections ] return {"collections": collections}
def post(self): args = self.get_parser.parse_args(strict=True) status = auth(args["username"], args["password"]) if status["code"] == CODE_OKAY: user = common.query_single_by_field(models.User, "account", args["username"]) if user is None: return {"error": "account correct but not found in database"}, 404 return {"token": user.token}, 200 elif status["code"] == CODE_FALSE: # 凭证有误 return {"error": "incorrect"}, 401 else: # 内部网络错误 return {"error": "INTERNET FAILURE"}, 500
def delete(self): self.DELETE_PARSER.add_argument("username", required=True, location="headers") self.DELETE_PARSER.add_argument("token", required=True, location="headers") self.DELETE_PARSER.add_argument("id", required=True, location="headers") args = self.DELETE_PARSER.parse_args() # 检查token user = common.query_single_by_field(models.User, "account", args["username"]) if user is None: return {"error": "user doesn't exist"}, 404 if not check_token(user, args["token"]): return {"error": "token is wrong"}, 401 collection = common.query_single_by_id(models.SyllabusCollection, args["id"]) if collection is None: return {"error": "collection not found"}, 404 if collection.account == args["username"]: status = delete_record(db, collection) if status == True: return {"status": "deleted"} else: return {"error": repr(status[1])}, 500 else: collector = common.query_single_by_field(models.Collector, "collection_id", collection.collection_id) if collector is None: return {"error": "collector not found"}, 404 if collector.uid == user.id: status = delete_record(db, collection) if status == True: return {"status": "deleted"} else: return {"error": repr(status[1])}, 500 else: return {"error": "have not the permission"}, 403
def post(self): """ 请求地址: /interaction/api/v2/collector 参数: 必选参数: 位置: form username 用户账号 token 用户验证令牌 start_year 学年的开始年份 season 春夏秋指定一个, 同学分制 :return: """ self.POST_PARSER.add_argument("username", required=True, location="form") self.POST_PARSER.add_argument("token", required=True, location="form") self.POST_PARSER.add_argument("start_year", type=int, required=True, location="form") self.POST_PARSER.add_argument("season", type=int, required=True, location="form") args = self.POST_PARSER.parse_args() user = common.query_single_by_field(models.User, "account", args["username"]) if user is None: return {"error": "user doesn't exist"}, 404 token_check = {"uid": user.id, "token": args["token"]} if not common.check_token(token_check): return {"error": "token is wrong"}, 401 while True: collection_id = generate_collection_id() if not check_existence(collection_id): break collector = models.Collector(collection_id=collection_id, start_year=args["start_year"], season=args["season"], uid=user.id) result = common.add_to_db(db, collector) if result == True: return {"collection_id": collector.collection_id} else: return {"error": "commit error in mysql"}, 500
def check_existence(collection_id): collector = common.query_single_by_field(models.Collector, "collection_id", collection_id) if collector is None: return False return True
def get(self, account=None): if account is None: return {"error": "name required in the query parameter"}, 400 # print(name) # input() user = common.query_single_by_field(User, "account", account) if user is None: return {"error": "no user's account is {}".format(account)}, 404 return marshal(user, SINGLE_USER_STRUCTURE) # class UserResource(GenericResource): # # # curl --header "Content-type: application/json" 127.0.0.1:8080/interaction/api/v2/user -X PUT -d '{"id": 1, "birthday": "819648000", "nickname": "xiaofud", "gender": 1, "profile": "hello world"}' # # date -d "1995-12-23" "+%s" 获取时间戳 # def put(self): # args = put_parser.parse_args() # if args["birthday"] is not None: # birthday = datetime.fromtimestamp(int(args["birthday"])) # birthday = birthday.strftime("%Y-%m-%d %H:%M:%S") # args["birthday"] = birthday # # print(birthday) # user_id = args.pop("id") # # # for arg in args: # # if arg not in UserResource.ACCEPT_VARIABLES: # # args.pop(arg) # # 去除其他参数, 避免用户自己修改token之类的数据 # helpers.clean_arguments(args, PUT_ACCEPT_VARIABLES) # # result = user_operation.update_user_by_id(user_id, **args) # if result == True: # return {"status": "updated"}, 200 # else: # if result[1] == user_operation.common.ERROR_NOT_FOUND: # return {"error": "user not found"}, 404 # else: # return {"error": "failed"}, 500 # Internal Server Error # Argument Locations # By default, the RequestParser tries to parse values from flask.Request.values, and flask.Request.json. # # Use the location argument to add_argument() to specify alternate locations to pull the values from. Any variable on the flask.Request can be used. For example: # # # Look only in the POST body # parser.add_argument('name', type=int, location='form') # # # Look only in the querystring # parser.add_argument('PageSize', type=int, location='args') # # # From the request headers # parser.add_argument('User-Agent', location='headers') # # # From http cookies # parser.add_argument('session_id', location='cookies') # # # From file uploads # parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files') # Note # Only use type=list when location='json'. See this issue for more details