def update_admin():
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message':
                        'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401

    admin = keys[0]
    id = keys[1]

    if admin == 0:
        return jsonify({'response': "This is not an admin"}), 403

    if db.session.query(Admin.adminId).filter_by(adminId=id).scalar() is None:
        return jsonify({'response': "Invalid ID found"}), 406

    result = request.get_json()
    if not result:
        return jsonify({"response": "No input data provided"}), 400

    if "username" in result:
        db.session.query(Admin).filter(Admin.adminId == id).update(
            dict(username=result["username"]))
    if "email" in result:
        db.session.query(Admin).filter(Admin.adminId == id).update(
            dict(email=result["email"]))
    db.session.commit()

    return jsonify({'response': "Success"}), 200
def create_brand():
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message':
                        'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]
    id = keys[1]

    if admin == 0:
        return jsonify({'response': "This is not an admin"}), 403

    data = request.get_json()
    if not data:
        return {"response": "No input data provided"}, 400

    try:
        result = BrandSchema().load(data)
    except Exception:
        return jsonify({'response': "Invalid input"}), 403

    brand = Brand(name=result["name"])
    db.session.add(brand)
    db.session.commit()

    return jsonify({'response': "Success"}), 201
Example #3
0
def create_car():
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message': 'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]
    id = keys[1]

    if admin == 0:
        return jsonify({'response': "This is not an admin"}), 403

    data = request.get_json()
    if not data:
        return jsonify({"response": "No input data provided"}), 400

    try:
        result = CarSchema().load(data)
    except Exception:
        return jsonify({'response': "Invalid input"}), 403

    if db.session.query(Brand.brandId).filter_by(brandId=result["brand_id"]).scalar() is None:
        return jsonify({'response': "Invalid brand_id found"}), 403

    car = Car(brand_id=result["brand_id"], model=result["model"], description=result["description"])

    db.session.add(car)
    db.session.commit()

    return jsonify({'response': "Success"}), 201
Example #4
0
def get_rents():
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message':
                        'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]
    id = keys[1]

    if admin == 1:
        return jsonify({'response': "You are not a user"}), 403

    result = db.session.query(Rent).filter(Rent.owner_id == id).order_by(
        Rent.rentId).all()
    schema = RentSchema(many=True)
    dump_data = schema.dump(result)

    return jsonify({'response': dump_data}), 200
def delete_brand(id):
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message':
                        'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]

    if admin == 0:
        return jsonify({'response': "This is not an admin"}), 403

    if db.session.query(Brand.brandId).filter_by(brandId=id).scalar() is None:
        return jsonify({'response': "Invalid brand ID found"}), 406

    db.session.query(Brand).filter(Brand.brandId == id).delete()
    db.session.commit()

    return jsonify({'response': "Success"}), 200
Example #6
0
def create_rent():
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message':
                        'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]
    id = keys[1]

    if admin == 1:
        return jsonify({'response': "You are not a user"}), 403

    data = request.get_json()
    if not data:
        return jsonify({"response": "No input data provided"}), 400

    try:
        result = RentSchema().load(data)
    except Exception:
        return jsonify({'response': "Invalid input"}), 403

    if db.session.query(
            Car.carId).filter_by(carId=result["car_id"]).scalar() is None:
        return jsonify({'response': "Invalid car ID found"}), 404

    rent = Rent(owner_id=id,
                car_id=result["car_id"],
                startT=result["startTime"],
                endT=result["endTime"])
    db.session.add(rent)
    db.session.commit()

    return jsonify({'response': "Success"}), 201
Example #7
0
def delete_rent(rentId):
    try:
        auth_token = request.args.get('access_token')
        keys = bu.decode_auth_token(auth_token)

    except jwt.ExpiredSignatureError:
        return jsonify({'message':
                        'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]
    id = keys[1]

    if admin == 1:
        return jsonify({'response': "You are not a user"}), 403

    if db.session.query(Rent.rentId).filter_by(rentId=rentId,
                                               owner_id=id).scalar() is None:
        return jsonify({'response': "No your`s rents with this ID found"}), 404

    db.session.query(Rent).filter(Rent.rentId == rentId).delete()
    db.session.commit()

    return jsonify({'response': "Success"}), 200
Example #8
0
def update_car(id):
    auth_token = request.args.get('access_token')
    try:
        keys = bu.decode_auth_token(auth_token)
    except jwt.ExpiredSignatureError:
        return jsonify({'message': 'Signature expired. Please log in again.'}), 401
    except jwt.InvalidTokenError:
        return jsonify({'message': 'Invalid token. Please log in again.'}), 401
    admin = keys[0]

    if admin == 0:
        return jsonify({'response': "This is not an admin"}), 403

    if db.session.query(Car.carId).filter_by(carId=id).scalar() is None:
        return jsonify({'response': "Invalid ID found"}), 406

    result = request.get_json()
    if not result:
        return {"response": "No input data provided"}, 400

    if "description" in result:
        db.session.query(Car).filter(Car.carId == id).update(dict(description=str(result["description"])))
        db.session.commit()
    return jsonify({'response': "Success"}), 200