コード例 #1
0
    def get(self):
        self.get_parser.add_argument("uid",
                                     type=int,
                                     required=True,
                                     location="args")
        self.get_parser.add_argument("token", required=True, location="args")
        # 用于分页
        self.get_parser.add_argument("page_index", type=int, location="args")
        # 用于分页
        self.get_parser.add_argument("page_size", type=int, location="args")
        args = self.get_parser.parse_args()
        # 检查token
        if common.check_token(args):
            # print(args)
            try:
                ret = personal_comments_query(args)

                if ret[0]:
                    return ret[1], 200
                return {"error": ret[1]}, 404
            except Exception as e:
                print(traceback.print_exc())
                return {"error": repr(e)}, 500
        else:
            return {"error": "unauthorized"}, 401
コード例 #2
0
    def delete(self):
        """
        测试:
        参数: uid, pid(post_id, 报头中貌似使用下划线会有一些问题, 所以改为pid), token
        curl "localhost:8080/interaction/api/v2/unread" -X DELETE -H "token: 000000" -H "uid: 2" -H "pid: 2"
        # 注意这里是pid, header 中貌似使用 _ 会有一些遗留问题
        http://stackoverflow.com/questions/22856136/why-underscores-are-forbidden-in-http-header-names
        :return:
        """
        self.DELETE_PARSER.add_argument("token", required=True, location="headers")
        self.DELETE_PARSER.add_argument("uid", type=int, required=True, location="headers")
        # 貌似头部的key不能有下划线
        self.DELETE_PARSER.add_argument("pid", type=int, required=True, location="headers")

        args = self.DELETE_PARSER.parse_args()

        # 检查token
        if common.check_token(args):
            # print(args)
            unread_message = UnRead.query.filter_by(uid=args["uid"]).filter_by(post_id=args["pid"]).all()
            # print("len of message", len(unread_message))
            for message in unread_message:
                # common.delete_from_db(db, UnRead, )
                # print(message)
                db.session.delete(message)
            try:
                db.session.commit()
                return {"status": "deleted"}, 200
            except Exception as e:
                print("error when remove unreads:", repr(e))
                db.session.rollback()
                return {"error": repr(e)}, 500
        else:
            return {"error": "unauthorized"}, 401
コード例 #3
0
    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
コード例 #4
0
    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_filed(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}
コード例 #5
0
    def delete(self):
        """
        删除拼车信息
        API请求地址:
        /interaction/api/v2/carpool
        方法: DELETE
        参数: 所有参数位于请求头部
        必选参数:
            id 拼车信息的id
            uid 发布拼车信息的用户id(发布者才有权删除)
            token 用户的token
        """

        self.DELETE_PARSER.add_argument("id", type=int, required=True, location="headers")
        self.DELETE_PARSER.add_argument("uid", type=int, required=True, location="headers")
        self.DELETE_PARSER.add_argument("token", required=True, location="headers")

        args = self.DELETE_PARSER.parse_args()

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

        status = common.delete_from_db(db, models.Carpool, args["id"], args["uid"])
        if status == True:
            return {"status": "deleted"}
        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
コード例 #6
0
    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_filed(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}
コード例 #7
0
    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
コード例 #8
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
コード例 #9
0
    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_filed(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
コード例 #10
0
    def put(self):
        """
        修改自己的联系方式
        API请求地址:
        /interaction/api/v2/passenger
        方法: PUT
        参数: 参数位置为form
        必选参数:
            id 乘客id
            carpool_id 已经存在的某个拼车id
            uid 用户id
            token 用户token
            contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构
                例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板
        """

        # 用于更新信息, 只允许修改contact信息
        self.PUT_PARSER.add_argument("id",
                                     type=int,
                                     required=True,
                                     location="form")
        # self.PUT_PARSER.add_argument("carpool_id", type=int, required=True, location="form")
        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("contact", required=True, location="form")

        args = self.PUT_PARSER.parse_args()

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

        # passenger = models.Passenger.query.filter_by(uid=args["uid"]).filter_by(carpool_id=args["carpool_id"]).first()
        passenger = models.Passenger.query.filter_by(id=args["id"]).first()
        # 并未上车
        if passenger is None:
            return {"error": "passenger not exists"}, 404

        passenger.contact = args["contact"]

        if common.add_to_db(db, passenger) == True:
            return {"status": "updated"}, 200
        else:
            return {"error": "Internal Server Error"}, 500
コード例 #11
0
    def delete(self):
        """
        退出某个拼车
        API请求地址:
        /interaction/api/v2/passenger
        方法: DELETE
        参数: 位于请求报头
        必选参数:
            id 乘客id
            uid 用户id
            token 用户token
        """
        self.DELETE_PARSER.add_argument("id",
                                        type=int,
                                        required=True,
                                        location="headers")
        self.DELETE_PARSER.add_argument("uid",
                                        type=int,
                                        required=True,
                                        location="headers")
        self.DELETE_PARSER.add_argument("token",
                                        required=True,
                                        location="headers")

        args = self.DELETE_PARSER.parse_args()

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

        # passenger = models.Passenger.query.filter_by(id=args["id"]).first()
        # # 并未上车
        # if passenger is None:
        #     return {"error": "passenger not exists"}, 404
        status = common.delete_from_db(db, models.Passenger, args["id"],
                                       args["uid"])
        if status == True:
            return {"status": "deleted"}
        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
コード例 #12
0
    def delete(self):
        """
        删除拼车信息
        API请求地址:
        /interaction/api/v2/carpool
        方法: DELETE
        参数: 所有参数位于请求头部
        必选参数:
            id 拼车信息的id
            uid 发布拼车信息的用户id(发布者才有权删除)
            token 用户的token
        """

        self.DELETE_PARSER.add_argument("id",
                                        type=int,
                                        required=True,
                                        location="headers")
        self.DELETE_PARSER.add_argument("uid",
                                        type=int,
                                        required=True,
                                        location="headers")
        self.DELETE_PARSER.add_argument("token",
                                        required=True,
                                        location="headers")

        args = self.DELETE_PARSER.parse_args()

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

        status = common.delete_from_db(db, models.Carpool, args["id"],
                                       args["uid"])
        if status == True:
            return {"status": "deleted"}
        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
コード例 #13
0
    def delete(self):
        """
        测试:
        参数: uid, pid(post_id, 报头中貌似使用下划线会有一些问题, 所以改为pid), token
        curl "localhost:8080/interaction/api/v2/unread" -X DELETE -H "token: 000000" -H "uid: 2" -H "pid: 2"
        # 注意这里是pid, header 中貌似使用 _ 会有一些遗留问题
        http://stackoverflow.com/questions/22856136/why-underscores-are-forbidden-in-http-header-names
        :return:
        """
        self.DELETE_PARSER.add_argument("token",
                                        required=True,
                                        location="headers")
        self.DELETE_PARSER.add_argument("uid",
                                        type=int,
                                        required=True,
                                        location="headers")
        # 貌似头部的key不能有下划线
        self.DELETE_PARSER.add_argument("pid",
                                        type=int,
                                        required=True,
                                        location="headers")

        args = self.DELETE_PARSER.parse_args()

        # 检查token
        if common.check_token(args):
            # print(args)
            unread_message = UnRead.query.filter_by(uid=args["uid"]).filter_by(
                post_id=args["pid"]).all()
            # print("len of message", len(unread_message))
            for message in unread_message:
                # common.delete_from_db(db, UnRead, )
                # print(message)
                db.session.delete(message)
            try:
                db.session.commit()
                return {"status": "deleted"}, 200
            except Exception as e:
                print("error when remove unreads:", repr(e))
                db.session.rollback()
                return {"error": repr(e)}, 500
        else:
            return {"error": "unauthorized"}, 401
コード例 #14
0
    def put(self):
        """
        修改自己的联系方式
        API请求地址:
        /interaction/api/v2/passenger
        方法: PUT
        参数: 参数位置为form
        必选参数:
            id 乘客id
            carpool_id 已经存在的某个拼车id
            uid 用户id
            token 用户token
            contact 用户自己的联系信息, 存储JSON字符串, 和iOS端沟通好结构
                例: {"wechat": "xxx", "phone": xxx} 等, 方便用于复制联系信息到剪贴板
        """

        # 用于更新信息, 只允许修改contact信息
        self.PUT_PARSER.add_argument("id", type=int, required=True, location="form")
        # self.PUT_PARSER.add_argument("carpool_id", type=int, required=True, location="form")
        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("contact", required=True, location="form")

        args = self.PUT_PARSER.parse_args()

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

        # passenger = models.Passenger.query.filter_by(uid=args["uid"]).filter_by(carpool_id=args["carpool_id"]).first()
        passenger = models.Passenger.query.filter_by(id=args["id"]).first()
        # 并未上车
        if passenger is None:
            return {"error": "passenger not exists"}, 404

        passenger.contact = args["contact"]

        if common.add_to_db(db, passenger) == True:
            return {"status": "updated"}, 200
        else:
            return {"error": "Internal Server Error"}, 500
コード例 #15
0
    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_filed(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
コード例 #16
0
    def delete(self):
        """
        退出某个拼车
        API请求地址:
        /interaction/api/v2/passenger
        方法: DELETE
        参数: 位于请求报头
        必选参数:
            id 乘客id
            uid 用户id
            token 用户token
        """
        self.DELETE_PARSER.add_argument("id", type=int, required=True, location="headers")
        self.DELETE_PARSER.add_argument("uid", type=int, required=True, location="headers")
        self.DELETE_PARSER.add_argument("token", required=True, location="headers")

        args = self.DELETE_PARSER.parse_args()

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

        # passenger = models.Passenger.query.filter_by(id=args["id"]).first()
        # # 并未上车
        # if passenger is None:
        #     return {"error": "passenger not exists"}, 404
        status = common.delete_from_db(db, models.Passenger, args["id"], args["uid"])
        if status == True:
            return {"status": "deleted"}
        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
コード例 #17
0
def check_token(user, token):
    token_check = {"uid": user.id, "token": token}
    return common.check_token(token_check)
コード例 #18
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
コード例 #19
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
コード例 #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
コード例 #21
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
コード例 #22
0
def check_token(user, token):
    token_check = {
            "uid": user.id,
            "token": token
    }
    return common.check_token(token_check)
コード例 #23
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