def new_or_update_user(account, token):
    """
    插入新的用户, 或者是更新旧用户
    :param account:
    :param token:
    :return:
    """
    user = common.query_single_by_filed(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 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
Beispiel #3
0
    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 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_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

        collector = common.query_single_by_filed(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
Beispiel #5
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
Beispiel #6
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
    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
    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
    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
Beispiel #10
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
Beispiel #11
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