def get(self): """ 获取拼车信息 API请求地址: /interaction/api/v2/carpool 方法: GET 参数: type 可选, 默认为0, 位置: query参数, 0 表示按照拼车id获取拼车信息(即返回指定的单个拼车信息) id 位置: query参数, 值意为用户的uid, 或者拼车信息的id(由参数type决定) """ # 决定搜索类型 self.GET_PARSER.add_argument("type", type=int, location="args") self.GET_PARSER.add_argument("id", required=True, type=int, location="args") args = self.GET_PARSER.parse_args() # 默认按照拼车id寻找 type_ = args["type"] or self.QUERY_BY_ID id_ = args['id'] if type_ == self.QUERY_BY_ID: carpool = common.query_single_by_id(models.Carpool, id_) if carpool is not None: return marshal(carpool, CARPOOL_STRUCTURE) else: return {"error": "not found"}, 404 else: carpools = models.Carpool.query\ .join(models.Passenger, models.Carpool.id == models.Passenger.carpool_id)\ .filter(models.Passenger.uid == id_)\ .all() if len(carpools) == 0: return {"error": "not found"}, 404 else: return marshal(carpools, CARPOOL_STRUCTURE)
def get(self, id=None): # 先检查方法是否可用 if self.not_allowed_methods is not None and "get" in self.not_allowed_methods: return {"error": "method not allowed"}, 405 if id is None: return {"error": "bad request"}, 400 if "get" in self.parsers: args = self.parsers["get"].parse_args() if "get" in self.accepted_variable_dict: helpers.clean_arguments(args, self.accepted_variable_dict["get"]) # 进行时间处理 if self.time_to_string_list is not None: for key in self.time_to_string_list: if key in args: args[key] = helpers.timestamp_to_string(args[key]) # 看看是否需要检查token if self.token_check_callbacks is not None and "get" in self.token_check_callbacks: if not self.token_check_callbacks["get"](args): return {"error": "unauthorized"}, 401 # Unauthorized else: # 到这里要去掉token, 因为不允许用户写入token args.pop("token") thing = common.query_single_by_id(self.model, id) if thing is None: return {"error": "invalid id {} for {}".format(id, self.resource_name)}, 404 return marshal(thing, self.marshal_structure), 200
def get(self, id=None): # 先检查方法是否可用 if self.not_allowed_methods is not None and "get" in self.not_allowed_methods: return {"error": "method not allowed"}, 405 if id is None: return {"error": "bad request"}, 400 if "get" in self.parsers: args = self.parsers["get"].parse_args() if "get" in self.accepted_variable_dict: helpers.clean_arguments(args, self.accepted_variable_dict["get"]) # 进行时间处理 if self.time_to_string_list is not None: for key in self.time_to_string_list: if key in args: args[key] = helpers.timestamp_to_string(args[key]) # 看看是否需要检查token if self.token_check_callbacks is not None and "get" in self.token_check_callbacks: if not self.token_check_callbacks["get"](args): return {"error": "unauthorized"}, 401 # Unauthorized else: # 到这里要去掉token, 因为不允许用户写入token args.pop("token") thing = common.query_single_by_id(self.model, id) if thing is None: return { "error": "invalid id {} for {}".format(id, self.resource_name) }, 404 return marshal(thing, self.marshal_structure), 200
def post(self): """ 加入某个拼车 API请求地址: /interaction/api/v2/passenger 方法: POST 参数: 参数位置为form 必选参数: carpool_id 已经存在的某个拼车id uid 用户id token 用户token contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构 例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板 """ self.POST_PARSER.add_argument("contact", required=True, location="form") # self.POST_PARSER.add_argument("id", type=int, required=True, location="form") self.POST_PARSER.add_argument("carpool_id", type=int, required=True, location="form") self.POST_PARSER.add_argument("uid", type=int, required=True, location="form") self.POST_PARSER.add_argument("token", required=True, location="form") args = self.POST_PARSER.parse_args() # 检查token if not common.check_token(args): return {"error": "wrong token"}, 401 del args["token"] # 检查carpool存不存在 carpool = common.query_single_by_id(models.Carpool, args["carpool_id"]) if carpool is None: return {"error": "carpool not exists"}, 404 # 不允许加入几次拼车 passenger = models.Passenger.query.filter_by(uid=args["uid"]).filter_by(carpool_id=carpool.id).first() if passenger is not None: return {"error": "already in this carpool"}, 400 # 加入时间戳 args["join_time"] = helpers.timestamp_to_string(int(time.time())) passenger = models.Passenger(**args) count = carpool.people_count + 1 if count > carpool.max_people: return {"error": "people overflows"}, 400 carpool.people_count = count if common.add_to_db(db, passenger) == True and common.add_to_db(db, carpool) == True: return {"id": common.get_last_inserted_id(models.Passenger)}, 200 else: return {"error": "Internal Server Error"}, 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_filed(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_filed(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 get(self): """ 获取拼车的人的信息 API请求地址: /interaction/api/v2/passenger 方法: GET 参数: 必选参数: id 乘客的id """ self.GET_PARSER.add_argument("id", required=True, type=int, location="args") args = self.GET_PARSER.parse_args() id_ = args["id"] passenger = common.query_single_by_id(models.Passenger, id_) if passenger is None: return {"error": "not found"}, 404 return marshal(passenger, PASSENGER_STRUCTURE)
def get(self): """ 获取拼车的人的信息 API请求地址: /interaction/api/v2/passenger 方法: GET 参数: 必选参数: id 乘客的id """ self.GET_PARSER.add_argument("id", required=True, type=int, location="args") args = self.GET_PARSER.parse_args() id_ = args["id"] passenger = common.query_single_by_id(models.Passenger, id_) if passenger is None: return {"error": "not found"}, 404 return marshal(passenger, PASSENGER_STRUCTURE)
def get(self): """ 获取拼车信息 API请求地址: /interaction/api/v2/carpool 方法: GET 参数: type 可选, 默认为0, 位置: query参数, 0 表示按照拼车id获取拼车信息(即返回指定的单个拼车信息) id 位置: query参数, 值意为用户的uid, 或者拼车信息的id(由参数type决定) """ # 决定搜索类型 self.GET_PARSER.add_argument("type", type=int, location="args") self.GET_PARSER.add_argument("id", required=True, type=int, location="args") args = self.GET_PARSER.parse_args() # 默认按照拼车id寻找 type_ = args["type"] or self.QUERY_BY_ID id_ = args['id'] if type_ == self.QUERY_BY_ID: carpool = common.query_single_by_id(models.Carpool, id_) if carpool is not None: return marshal(carpool, CARPOOL_STRUCTURE) else: return {"error": "not found"}, 404 else: carpools = models.Carpool.query\ .join(models.Passenger, models.Carpool.id == models.Passenger.carpool_id)\ .filter(models.Passenger.uid == id_)\ .all() if len(carpools) == 0: return {"error": "not found"}, 404 else: return marshal(carpools, CARPOOL_STRUCTURE)
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_filed(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_filed(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 get_post_by_id(id): post = common.query_single_by_id(Post, id) if post is None: print("post of id {} is None".format(id)) return common.query_single_by_id(Post, id)
def post(self): """ 加入某个拼车 API请求地址: /interaction/api/v2/passenger 方法: POST 参数: 参数位置为form 必选参数: carpool_id 已经存在的某个拼车id uid 用户id token 用户token contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构 例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板 """ self.POST_PARSER.add_argument("contact", required=True, location="form") # self.POST_PARSER.add_argument("id", type=int, required=True, location="form") self.POST_PARSER.add_argument("carpool_id", type=int, required=True, location="form") self.POST_PARSER.add_argument("uid", type=int, required=True, location="form") self.POST_PARSER.add_argument("token", required=True, location="form") args = self.POST_PARSER.parse_args() # 检查token if not common.check_token(args): return {"error": "wrong token"}, 401 del args["token"] # 检查carpool存不存在 carpool = common.query_single_by_id(models.Carpool, args["carpool_id"]) if carpool is None: return {"error": "carpool not exists"}, 404 # 不允许加入几次拼车 passenger = models.Passenger.query.filter_by( uid=args["uid"]).filter_by(carpool_id=carpool.id).first() if passenger is not None: return {"error": "already in this carpool"}, 400 # 加入时间戳 args["join_time"] = helpers.timestamp_to_string(int(time.time())) passenger = models.Passenger(**args) count = carpool.people_count + 1 if count > carpool.max_people: return {"error": "people overflows"}, 400 carpool.people_count = count if common.add_to_db(db, passenger) == True and common.add_to_db( db, carpool) == True: return {"id": common.get_last_inserted_id(models.Passenger)}, 200 else: return {"error": "Internal Server Error"}, 500
def get_user_by_id(id): user = common.query_single_by_id(User, id) if user is None: print("user of id {} is None".format(id)) return common.query_single_by_id(User, id)
def get_post_by_id(id): post = common.query_single_by_id(Post, id) if post is None: print("post of id {} is None".format(id)) return common.query_single_by_id(Post, id)