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)
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)
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)
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)
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)
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)
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)
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 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)
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)
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")})))
async def get_my_info(request, user): return success(data=user)