Exemple #1
0
    def query(self, arg_dict):

        # 分页 http://my.oschina.net/ranvane/blog/196906

        # 按照各种条件搜索拼车
        # query_obj = models.Carpool.query

        # 读取条件
        now = timestamp_to_string(int(time.time()))
        # 设置时间条件
        depart_time = arg_dict["depart_time"] or now

        # print(depart_time)

        page_index = arg_dict["page_index"] or 1

        page_size = arg_dict["page_size"] or 10

        page_obj = \
            models.Carpool.query\
                .filter(models.Carpool.departure_time >= depart_time)\
                .order_by(models.Carpool.departure_time.asc())\
                .order_by(models.Carpool.people_count.desc())\
                .paginate(page_index, page_size, False)

        carpools = page_obj.items

        # print(len(carpools))

        return carpools
    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
Exemple #3
0
    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 delete(self, id=None):

        # 先检查方法是否可用
        if self.not_allowed_methods is not None and "delete" in self.not_allowed_methods:
            return {"error": "method not allowed"}, 405

        # if id is None:
        #     return {"error": "bad request"}, 401

        if "delete" in self.parsers:
            args = self.parsers["delete"].parse_args()
            if "delete" in self.accepted_variable_dict:
                helpers.clean_arguments(args, self.accepted_variable_dict["delete"])
            # 进行时间处理
            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 "delete" in self.token_check_callbacks:
                if not self.token_check_callbacks["delete"](args):
                    return {"error": "unauthorized"}, 401 # Unauthorized

            result = common.delete_from_db(db, self.model, args["id"], args["uid"])
            if result == True:
                return {"status": "deleted"}, 200
            else:
                if result[1] == common.ERROR_NOT_FOUND:
                    return {"error": "{} not found".format(self.resource_name)}, 404
                elif result[1] == common.ERROR_COMMIT_FAILED:
                    return {"error": "failed"}, 500 # Internal Server Error
                elif result[1] == common.ERROR_USER_ID_CONFLICT:
                    return {"error": "kidding me?"}, 403
        return {"error": "bad request"}, 400
    def query(self, arg_dict):


        # 分页 http://my.oschina.net/ranvane/blog/196906

        # 按照各种条件搜索拼车
        # query_obj = models.Carpool.query

        # 读取条件
        now = timestamp_to_string(int(time.time()))
        # 设置时间条件
        depart_time = arg_dict["depart_time"] or now

        # print(depart_time)

        page_index = arg_dict["page_index"] or 1

        page_size = arg_dict["page_size"] or 10

        page_obj = \
            models.Carpool.query\
                .filter(models.Carpool.departure_time >= depart_time)\
                .order_by(models.Carpool.departure_time.asc())\
                .order_by(models.Carpool.people_count.desc())\
                .paginate(page_index, page_size, False)

        carpools = page_obj.items

        # print(len(carpools))

        return carpools
    def post(self):
        args = self.post_parser.parse_args(strict=True)
        args["post_type"] = PostResource.Post.POST_TYPE_SCHOOL_ACTIVITY

        # 检查token
        if not common.check_token(args):
            return {"error": "wrong token"}, 401
        del args["token"]

        args["activity_location"] = args["activity_location"] or "未指定"

        # 处理时间
        for key in ("activity_start_time", "activity_end_time"):
            args[key] = helpers.timestamp_to_string(args[key])

        # 检测是否有权限发布
        super_users = models.User.query.with_entities(models.User.id).filter(
            models.User.level >= models.User.LEVEL_CAN_POST_ACTIVITY).all()
        super_ids = [user.id for user in super_users]
        if args["uid"] not in super_ids:
            return {"error": "HAVE NOT THE PRIORITY"}, 403  # 没有权限发布
        # 参数新的数据到数据库
        record_id = common.new_record(db, models.Post, **args)

        if record_id != False:
            return {"id": record_id}, 201  # crated
        else:
            return {"error": "failed"}, 500  # Internal Server Error
    def post(self):
        args = self.post_parser.parse_args(strict=True)
        args["post_type"] = PostResource.Post.POST_TYPE_SCHOOL_ACTIVITY

        # 检查token
        if not common.check_token(args):
            return {"error": "wrong token"}, 401
        del args["token"]

        args["activity_location"] = args["activity_location"] or "未指定"

        # 处理时间
        for key in ("activity_start_time", "activity_end_time"):
            args[key] = helpers.timestamp_to_string(args[key])


        # 检测是否有权限发布
        super_users = models.User.query.with_entities(models.User.id).filter(models.User.level >= models.User.LEVEL_CAN_POST_ACTIVITY).all()
        super_ids = [user.id for user in super_users]
        if args["uid"] not in super_ids:
            return {"error": "HAVE NOT THE PRIORITY"}, 403  # 没有权限发布
        # 参数新的数据到数据库
        record_id = common.new_record(db, models.Post, **args)

        if record_id != False:
            return {"id": record_id}, 201  # crated
        else:
            return {"error": "failed"}, 500 # Internal Server Error
    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 post(self):

        # 先检查方法是否可用
        if self.not_allowed_methods is not None and "post" in self.not_allowed_methods:
            return {"error": "method not allowed"}, 405



        if "post" in self.parsers:
            args = self.parsers["post"].parse_args()
            # print(args)
            if "post" in self.accepted_variable_dict:
                helpers.clean_arguments(args, self.accepted_variable_dict["post"])
            # 进行时间处理
            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 "post" in self.token_check_callbacks:
                print("token checking")
                # print("checking token")
                # print("input token", args["token"])
                if not self.token_check_callbacks["post"](args):
                    return {"error": "unauthorized"}, 401 # Unauthorized

            # 到这里要去掉token, 因为不允许用户写入token
            args.pop("token")

        # 调用回调方法
        if self.extra_callbacks is not None and "post" in self.extra_callbacks:
            print("callback")
            ret_val = self.extra_callbacks["post"](args)
            # 比如说有用户打算重复投票
            # print("called back returned")
            # print(args)
            if ret_val != False:
                return ret_val


        result = common.new_record(db, self.model, **args)
        if result != False:
            return {"id": result}, 201  # crated
        else:
            return {"error": "failed"}, 500 # Internal Server Error
    def query(self, arg_dict):
        # 分页 http://my.oschina.net/ranvane/blog/196906

        # 读取条件

        # 读取类型
        # 默认值为0
        type_ = arg_dict["type"] or 0

        # 设置时间条件
        now = helpers.timestamp_to_string(int(time.time()))
        start_time = arg_dict["activity_start_time"] or now

        # print(depart_time)

        page_index = arg_dict["page_index"] or 1

        page_size = arg_dict["page_size"] or 10

        print("type: {} index: {} size: {}".format(type_, page_index,
                                                   page_size))

        if type_ == 0:
            # 按照活动开始时间排序
            page_obj = \
                models.Post.query\
                    .filter(models.Post.post_type == models.Post.POST_TYPE_SCHOOL_ACTIVITY)\
                    .filter(models.Post.activity_start_time >= start_time)\
                    .filter(models.Post.visibility == models.VISIBILITY_VISIBLE)\
                    .order_by(models.Post.activity_start_time.asc())\
                    .paginate(page_index, page_size, False)
        else:
            # 按照发布时间排序
            page_obj = \
                models.Post.query\
                    .filter(models.Post.post_type == models.Post.POST_TYPE_SCHOOL_ACTIVITY)\
                    .filter(models.Post.visibility == models.VISIBILITY_VISIBLE)\
                    .order_by(models.Post.id.desc())\
                    .paginate(page_index, page_size, False)

        activities = page_obj.items
        # print([a.activity_start_time  for a in activities],"\n=============")

        return activities
Exemple #11
0
    def put(self):

        # 先检查方法是否可用
        if self.not_allowed_methods is not None and "put" in self.not_allowed_methods:
            return {"error": "method not allowed"}, 405

        if "put" in self.parsers:
            args = self.parsers["put"].parse_args()
            if "put" in self.accepted_variable_dict:
                helpers.clean_arguments(args,
                                        self.accepted_variable_dict["put"])
            # 进行时间处理
            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 "put" in self.token_check_callbacks:
                # print("need to check token")
                if not self.token_check_callbacks["put"](args):
                    # print(args["uid"], args["id"], args["token"])
                    return {"error": "unauthorized"}, 401  # Unauthorized

            # 因为不允许更新id
            id = args.pop("id")

            # 到这里要去掉token, 因为不允许用户写入token
            args.pop("token")

        # result = post_operation.update_post_by_id(id, **args)
        uid = args.pop("uid")
        result = common.update_model_by_id(self.model, db, id, uid, **args)
        if result == True:
            return {"status": "updated"}, 200
        else:
            if result[1] == common.ERROR_NOT_FOUND:
                return {
                    "error": "{} not found".format(self.resource_name)
                }, 404
            elif result[1] == common.ERROR_COMMIT_FAILED:
                return {"error": "failed"}, 500  # Internal Server Error
            elif result[1] == common.ERROR_USER_ID_CONFLICT:
                return {"error": "kidding me?"}, 401
Exemple #12
0
    def post(self):

        # 先检查方法是否可用
        if self.not_allowed_methods is not None and "post" in self.not_allowed_methods:
            return {"error": "method not allowed"}, 405

        if "post" in self.parsers:
            args = self.parsers["post"].parse_args()
            # print(args)
            if "post" in self.accepted_variable_dict:
                helpers.clean_arguments(args,
                                        self.accepted_variable_dict["post"])
            # 进行时间处理
            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 "post" in self.token_check_callbacks:
                print("token checking")
                # print("checking token")
                # print("input token", args["token"])
                if not self.token_check_callbacks["post"](args):
                    return {"error": "unauthorized"}, 401  # Unauthorized

            # 到这里要去掉token, 因为不允许用户写入token
            args.pop("token")

        # 调用回调方法
        if self.extra_callbacks is not None and "post" in self.extra_callbacks:
            print("callback")
            ret_val = self.extra_callbacks["post"](args)
            # 比如说有用户打算重复投票
            # print("called back returned")
            # print(args)
            if ret_val != False:
                return ret_val

        result = common.new_record(db, self.model, **args)
        if result != False:
            return {"id": result}, 201  # crated
        else:
            return {"error": "failed"}, 500  # Internal Server Error
    def query(self, arg_dict):
        # 分页 http://my.oschina.net/ranvane/blog/196906

        # 读取条件

        # 读取类型
        # 默认值为0
        type_ = arg_dict["type"] or 0

        # 设置时间条件
        now = helpers.timestamp_to_string(int(time.time()))
        start_time = arg_dict["activity_start_time"] or now

        # print(depart_time)

        page_index = arg_dict["page_index"] or 1

        page_size = arg_dict["page_size"] or 10

        print("type: {} index: {} size: {}".format(type_, page_index, page_size))

        if type_ == 0:
            # 按照活动开始时间排序
            page_obj = \
                models.Post.query\
                    .filter(models.Post.post_type == models.Post.POST_TYPE_SCHOOL_ACTIVITY)\
                    .filter(models.Post.activity_start_time >= start_time)\
                    .filter(models.Post.visibility == models.VISIBILITY_VISIBLE)\
                    .order_by(models.Post.activity_start_time.asc())\
                    .paginate(page_index, page_size, False)
        else:
            # 按照发布时间排序
            page_obj = \
                models.Post.query\
                    .filter(models.Post.post_type == models.Post.POST_TYPE_SCHOOL_ACTIVITY)\
                    .filter(models.Post.visibility == models.VISIBILITY_VISIBLE)\
                    .order_by(models.Post.id.desc())\
                    .paginate(page_index, page_size, False)

        activities = page_obj.items


        return activities
    def put(self):

        # 先检查方法是否可用
        if self.not_allowed_methods is not None and "put" in self.not_allowed_methods:
            return {"error": "method not allowed"}, 405

        if "put" in self.parsers:
            args = self.parsers["put"].parse_args()
            if "put" in self.accepted_variable_dict:
                helpers.clean_arguments(args, self.accepted_variable_dict["put"])
            # 进行时间处理
            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 "put" in self.token_check_callbacks:
                # print("need to check token")
                if not self.token_check_callbacks["put"](args):
                    # print(args["uid"], args["id"], args["token"])
                    return {"error": "unauthorized"}, 401 # Unauthorized

            # 因为不允许更新id
            id = args.pop("id")

            # 到这里要去掉token, 因为不允许用户写入token
            args.pop("token")

        # result = post_operation.update_post_by_id(id, **args)
        uid = args.pop("uid")
        result = common.update_model_by_id(self.model, db, id, uid, **args)
        if result == True:
            return {"status": "updated"}, 200
        else:
            if result[1] == common.ERROR_NOT_FOUND:
                return {"error": "{} not found".format(self.resource_name)}, 404
            elif result[1] == common.ERROR_COMMIT_FAILED:
                return {"error": "failed"}, 500 # Internal Server Error
            elif result[1] == common.ERROR_USER_ID_CONFLICT:
                return {"error": "kidding me?"}, 401
Exemple #15
0
    def delete(self, id=None):

        # 先检查方法是否可用
        if self.not_allowed_methods is not None and "delete" in self.not_allowed_methods:
            return {"error": "method not allowed"}, 405

        # if id is None:
        #     return {"error": "bad request"}, 401

        if "delete" in self.parsers:
            args = self.parsers["delete"].parse_args()
            if "delete" in self.accepted_variable_dict:
                helpers.clean_arguments(args,
                                        self.accepted_variable_dict["delete"])
            # 进行时间处理
            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 "delete" in self.token_check_callbacks:
                if not self.token_check_callbacks["delete"](args):
                    return {"error": "unauthorized"}, 401  # Unauthorized

            result = common.delete_from_db(db, self.model, args["id"],
                                           args["uid"])
            if result == True:
                return {"status": "deleted"}, 200
            else:
                if result[1] == common.ERROR_NOT_FOUND:
                    return {
                        "error": "{} not found".format(self.resource_name)
                    }, 404
                elif result[1] == common.ERROR_COMMIT_FAILED:
                    return {"error": "failed"}, 500  # Internal Server Error
                elif result[1] == common.ERROR_USER_ID_CONFLICT:
                    return {"error": "kidding me?"}, 403
        return {"error": "bad request"}, 400
    def post(self):
        """
        发布拼车信息
        API请求地址:
        /interaction/api/v2/carpool
        方法: POST
        参数: 所有参数位置为form, 即 URL-ENCODED 的字符串
        必选参数:
            uid 发布拼车信息的用户的id
            token 用户的token
            departure_time 发车时间, 为[时间戳]
            driver 司机信息, 字符串
            contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构
                例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板
            source 出发地点
            destination 目的地
            max_people 这辆车最多能坐多少人
        可选参数:
            notice 备注信息, 如哪里集合之类的
        """

        # 验证信息
        self.POST_PARSER.add_argument("uid", type=int, required=True, location="form")
        self.POST_PARSER.add_argument("token", required=True, location="form")
        # 具体数据
        self.POST_PARSER.add_argument("departure_time", type=int, required=True, location="form")
        self.POST_PARSER.add_argument("driver", required=True, location="form")
        self.POST_PARSER.add_argument("contact", required=True, location="form")
        self.POST_PARSER.add_argument("source", required=True, location="form")
        self.POST_PARSER.add_argument("destination", required=True, location="form")

        self.POST_PARSER.add_argument("notice", required=False, location="form")

        self.POST_PARSER.add_argument("max_people", required=True, location="form")
        # self.POST_PARSER.add_argument("people_count")

        args = self.POST_PARSER.parse_args()

        # 检查token
        if not common.check_token(args):
            return {"error": "wrong token"}, 401

        del args["token"]

        args["departure_time"] = timestamp_to_string(args["departure_time"])

        carpool = models.Carpool(**args)

        # print(carpool)
        if common.add_to_db(db, carpool) == True:
            # 这里还要添加一条记录到 Passenger 里
            now = timestamp_to_string(int(time.time()))
            passenger = models.Passenger(join_time=now,
                                         uid=args["uid"],
                                         carpool_id=carpool.id,
                                         contact=args["contact"]
            )
            common.add_to_db(db, passenger)
            return {"id": carpool.id}, 200
        else:
            return {"error": "Internal Server Error"}, 500
    def put(self):

        """
        更新拼车信息
        API请求地址:
        /interaction/api/v2/carpool
        方法: PUT
        参数: 所有参数位置为form, 即 URL-ENCODED 的字符串
        必选参数:
            id 拼车信息的id
            uid 发布拼车信息的用户的id
            token 用户的token
            departure_time 发车时间, 为[时间戳]
            driver 司机信息, 字符串
            contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构
                例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板
            source 出发地点
            destination 目的地
            max_people 这辆车最多能坐多少人
        可选参数:
            notice 备注信息, 如哪里集合之类的
        """

        # 验证信息
        self.PUT_PARSER.add_argument("uid", type=int, required=True, location="form")
        self.PUT_PARSER.add_argument("token", required=True, location="form")
        # 具体数据
        self.PUT_PARSER.add_argument("departure_time", type=int, required=True, location="form")
        self.PUT_PARSER.add_argument("driver", required=True, location="form")
        self.PUT_PARSER.add_argument("contact", required=True, location="form")
        self.PUT_PARSER.add_argument("source", required=True, location="form")
        self.PUT_PARSER.add_argument("destination", required=True, location="form")

        self.PUT_PARSER.add_argument("notice", required=False, location="form")

        self.PUT_PARSER.add_argument("max_people", required=True, location="form")

        self.PUT_PARSER.add_argument("id", type=int, required=True, location="form")

        args = self.PUT_PARSER.parse_args(strict=True)
        # print(args)
        # 检查token
        if not common.check_token(args):
            return {"error": "wrong token"}, 401

        # 去掉其他辅助信息
        del args["token"]

        id_ = args["id"]
        del args["id"]

        uid = args["uid"]
        del args["uid"]

        args["departure_time"] = timestamp_to_string(args["departure_time"])

        status = common.update_model_by_id(models.Carpool, db, id_, uid, **args)
        if status == True:
            return {"status": "updated"}
        else:
            code = status[1]
            if code == common.ERROR_NOT_FOUND:
                return {"error": "not found"}, 404
            elif code == common.ERROR_USER_ID_CONFLICT:
                return {"error": "forbidden"}, 403
            elif code == common.ERROR_COMMIT_FAILED:
                return {"error": "Internal Server Error"}, 500
Exemple #18
0
    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
Exemple #19
0
    def put(self):
        """
        更新拼车信息
        API请求地址:
        /interaction/api/v2/carpool
        方法: PUT
        参数: 所有参数位置为form, 即 URL-ENCODED 的字符串
        必选参数:
            id 拼车信息的id
            uid 发布拼车信息的用户的id
            token 用户的token
            departure_time 发车时间, 为[时间戳]
            driver 司机信息, 字符串
            contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构
                例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板
            source 出发地点
            destination 目的地
            max_people 这辆车最多能坐多少人
        可选参数:
            notice 备注信息, 如哪里集合之类的
        """

        # 验证信息
        self.PUT_PARSER.add_argument("uid",
                                     type=int,
                                     required=True,
                                     location="form")
        self.PUT_PARSER.add_argument("token", required=True, location="form")
        # 具体数据
        self.PUT_PARSER.add_argument("departure_time",
                                     type=int,
                                     required=True,
                                     location="form")
        self.PUT_PARSER.add_argument("driver", required=True, location="form")
        self.PUT_PARSER.add_argument("contact", required=True, location="form")
        self.PUT_PARSER.add_argument("source", required=True, location="form")
        self.PUT_PARSER.add_argument("destination",
                                     required=True,
                                     location="form")

        self.PUT_PARSER.add_argument("notice", required=False, location="form")

        self.PUT_PARSER.add_argument("max_people",
                                     required=True,
                                     location="form")

        self.PUT_PARSER.add_argument("id",
                                     type=int,
                                     required=True,
                                     location="form")

        args = self.PUT_PARSER.parse_args(strict=True)
        # print(args)
        # 检查token
        if not common.check_token(args):
            return {"error": "wrong token"}, 401

        # 去掉其他辅助信息
        del args["token"]

        id_ = args["id"]
        del args["id"]

        uid = args["uid"]
        del args["uid"]

        args["departure_time"] = timestamp_to_string(args["departure_time"])

        status = common.update_model_by_id(models.Carpool, db, id_, uid,
                                           **args)
        if status == True:
            return {"status": "updated"}
        else:
            code = status[1]
            if code == common.ERROR_NOT_FOUND:
                return {"error": "not found"}, 404
            elif code == common.ERROR_USER_ID_CONFLICT:
                return {"error": "forbidden"}, 403
            elif code == common.ERROR_COMMIT_FAILED:
                return {"error": "Internal Server Error"}, 500
Exemple #20
0
    def post(self):
        """
        发布拼车信息
        API请求地址:
        /interaction/api/v2/carpool
        方法: POST
        参数: 所有参数位置为form, 即 URL-ENCODED 的字符串
        必选参数:
            uid 发布拼车信息的用户的id
            token 用户的token
            departure_time 发车时间, 为[时间戳]
            driver 司机信息, 字符串
            contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构
                例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板
            source 出发地点
            destination 目的地
            max_people 这辆车最多能坐多少人
        可选参数:
            notice 备注信息, 如哪里集合之类的
        """

        # 验证信息
        self.POST_PARSER.add_argument("uid",
                                      type=int,
                                      required=True,
                                      location="form")
        self.POST_PARSER.add_argument("token", required=True, location="form")
        # 具体数据
        self.POST_PARSER.add_argument("departure_time",
                                      type=int,
                                      required=True,
                                      location="form")
        self.POST_PARSER.add_argument("driver", required=True, location="form")
        self.POST_PARSER.add_argument("contact",
                                      required=True,
                                      location="form")
        self.POST_PARSER.add_argument("source", required=True, location="form")
        self.POST_PARSER.add_argument("destination",
                                      required=True,
                                      location="form")

        self.POST_PARSER.add_argument("notice",
                                      required=False,
                                      location="form")

        self.POST_PARSER.add_argument("max_people",
                                      required=True,
                                      location="form")
        # self.POST_PARSER.add_argument("people_count")

        args = self.POST_PARSER.parse_args()

        # 检查token
        if not common.check_token(args):
            return {"error": "wrong token"}, 401

        del args["token"]

        args["departure_time"] = timestamp_to_string(args["departure_time"])

        carpool = models.Carpool(**args)

        # print(carpool)
        if common.add_to_db(db, carpool) == True:
            # 这里还要添加一条记录到 Passenger 里
            now = timestamp_to_string(int(time.time()))
            passenger = models.Passenger(join_time=now,
                                         uid=args["uid"],
                                         carpool_id=carpool.id,
                                         contact=args["contact"])
            common.add_to_db(db, passenger)
            return {"id": carpool.id}, 200
        else:
            return {"error": "Internal Server Error"}, 500