Esempio n. 1
0
async def create_site(request, body):
    try:
        validate_schema(data=body, schema=new_site_schema)
        if model.get_place_by_id(body["place_id"]) is None:
            return fail(message=message.NOTFOUND, status=404)
        site = model.create_site(site=body)
        return success(data=json.loads(dumps(site)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 2
0
async def get_sites_by_place_id(request):
    try:
        place_id = request.match_info.get("place_id")
        place = model.get_place_by_id(place_id)
        if place is not None and place["vip"]:
            return fail(message=message.UNAUTHORIZED, status=401)
        sites = model.get_sites_by_place_id(place_id)
        return success(data=json.loads(dumps(sites)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 3
0
async def delete_site(request):
    try:
        site_id = request.match_info.get("site_id")
        site = model.get_site_by_id(site_id)
        if site is None:
            return fail(message=message.NOTFOUND, status=404)
        model.delete_site(site_id)
        return success(data=message.SUCCESS)
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
async def update_place(request, body):
    try:
        validate_schema(data=body, schema=update_place_schema)
        place_id = request.match_info.get("place_id")
        place = model.get_place_by_id(place_id)
        if place is None:
            return fail(message=message.NOTFOUND, status=404)
        place = model.update_place(data=body, place=place)
        return success(data=json.loads(dumps(place)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 5
0
 async def wrapper(*agrs, **kwargs):
     access_token = agrs[0].headers.get(AUTHORIZATION)
     try:
         token = jwt.decode(access_token, JWT_KEY, algorithms=["HS256"])
     except jwt.ExpiredSignatureError:
         return fail(EXPIRED_TOKEN, 422)
     except jwt.InvalidTokenError:
         return fail(INVALID_TOKEN, 422)
     user = model.get_user_by_email(token["email"])
     if user is None or user["role"] != "admin":
         return fail(UNAUTHORIZED, 401)
     return await f(*agrs, **kwargs)
async def update_user(request, body, user):
    try:
        validate_schema(body, user_schema)
        if "email" in body and model.get_user_by_email(
                body["email"]) is not None:
            return fail(message=message.EXISTING_EMAIL)
        user_id = user["_id"]["$oid"]
        result = model.update_user(loads(dumps(body)), user_id)
        if result is not None:
            return success(json.loads(dumps(result)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 7
0
 async def wrapper(*agrs, **kwargs):
     access_token = agrs[0].headers.get(AUTHORIZATION)
     try:
         token = jwt.decode(access_token, JWT_KEY, algorithms=["HS256"])
     except jwt.ExpiredSignatureError:
         return fail(EXPIRED_TOKEN, 422)
     except jwt.InvalidTokenError:
         return fail(INVALID_TOKEN, 422)
     user = model.get_user_by_email(token["email"])
     if user is None:
         return fail(UNAUTHORIZED, 401)
     kwargs.update({"user": json.loads(dumps(user))})
     return await f(*agrs, **kwargs)
Esempio n. 8
0
async def update_site(request, body):
    try:
        validate_schema(data=body, schema=update_site_schema)
        site_id = request.match_info.get("site_id")
        site = model.get_site_by_id(site_id)
        if site is None:
            return fail(message=message.NOTFOUND, status=404)
        if "place_id" in body and model.get_place_by_id(
                body["place_id"]) is None:
            return fail(message=message.NOTFOUND, status=404)
        site = model.update_site(data=body, site=site)
        return success(data=json.loads(dumps(site)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
async def get_places(request):
    try:
        places = model.get_places()
        return success(data=json.loads(dumps(places)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 10
0
async def create_place(request, body):
    try:
        validate_schema(data=body, schema=place_schema)
        place = model.create_place(place=body)
        return success(data=json.loads(dumps(place)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 11
0
async def get_sites_by_place_id_vip(request, user):
    try:
        place_id = request.match_info.get("place_id")
        sites = model.get_sites_by_place_id(place_id)
        return success(data=json.loads(dumps(sites)))
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 12
0
async def upload_file(request):
    try:
        data = await request.post()
        filename = data["file"].filename
        file = data["file"].file
        data_excel = pd.read_excel(file, engine='openpyxl')
        headers = []
        for item in data_excel:
            headers.insert(len(headers), item)
        data_excel.to_excel(filename)
        response = {
            "filename": filename,
            "headers": headers
        }
        return success(response)
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 13
0
async def import_file(request, body):
    try:
        data_excel = pd.read_excel(body["filename"], engine='openpyxl')
        site = dict()
        valid = data_excel[body["name"]].isna()
        for i in range(0, len(data_excel[body["name"]])):
            site = {
                "name": data_excel[body["name"]][i],
                "description": data_excel[body["description"]][i],
                "latitude": data_excel[body["latitude"]][i],
                "longitude": data_excel[body["longitude"]][i],
                "place_id": body["place_id"]
            }
            if valid[i]:
                site["name"] = "--- ? ---"
            model.create_site(site)
        return success(message.SUCCESS)
    except Exception as e:
        logging.error(e)
    return fail(message=message.FAIL, status=400)
Esempio n. 14
0
async def login_google(request, body):
    token = body["token"]
    try:
        id_info = id_token.verify_oauth2_token(token, requests.Request())
    except ValueError:
        return fail(message=message.INVALID_TOKEN, status=422)
    email = id_info["email"]
    user = model.get_user_by_email(email)
    if user is None:
        user = {
            "email": email,
            "active": True,
            "name": id_info["name"],
            "avatar": id_info["picture"],
            "role": "user"
        }
        user = model.create_user(user)
    data = {"email": user["email"]}
    access_token = jwt.encode(key=JWT_KEY,
                              payload=json.loads(dumps(data)),
                              algorithm="HS256")
    return success(json.loads(dumps({"token": str(access_token, "utf-8")})))