def signup(*args, **kwargs): data = request.get_json() try: new_user = User(username=data['username'], email=data['email'], password=data['password']) new_user.save() token = create_access_token(identity=new_user.uuid, expires_delta=timedelta(30)) response = compose_json_response(success=True, data=token, message=None, code=200) except OperationException: response = compose_json_response(success=False, data=None, message=None, code=500) except KeyError: response = compose_json_response(success=False, data=None, message=None, code=400) return response
def login(*args, **kwargs): data = request.get_json() try: username_or_email = data['username_or_email'] password = data['password'] if username_or_email and password: auth_dict = User.validate_credentials(username_or_email, password) if auth_dict['valid']: token = create_access_token( identity=auth_dict['user'].to_json()['uuid'], expires_delta=timedelta(90)) response = compose_json_response(success=True, data=token, message=None, code=200) else: response = compose_json_response(success=False, data=None, message='Unauthorized', code=401) else: response = compose_json_response( success=False, data=None, message='Invalid auth credentials', code=400) except KeyError: response = compose_json_response(success=False, data=None, message=None, code=400) return response
def get_visits(*args, **kwargs): page_number = execute_with_default(int, 1)(request.args.get('page')) visits = Visit.get_visits(page=page_number) def map_visit_to_formatted_json(visit): return {**visit.to_json(), "data": YelpFusion.get_with_id(visit.yelp_id, desired_props=["id", "name"])} formatted_visits = list(map(map_visit_to_formatted_json, visits)) if visits: return compose_json_response(success=True, data=formatted_visits, message=None, code=200) return compose_json_response(success=False, data=None, message=None, code=404)
def get_recommendations(*args, **kwargs): current_user = User.get_current() try: recommendations = [YelpFusion.get_with_id(id=recommendation.yelp_id, desired_props = ["id", "name", "image_url", "is_closed", "location", "url", "price"]) for recommendation in Recommendation.get_latest_5_with_user_id(current_user.id)] response = compose_json_response(success=True, data=recommendations, message=None, code=200) except: response = compose_json_response(success=False, data=None, message=None, code=500) return response
def search_businesses(*args, **kwargs): params = request.args try: q = params.get('q') location = params.get('location') print(q, location) businesses = YelpFusion.search(term=q, location=location) response = compose_json_response(success=True, data=businesses, message=None, code=200) except KeyError: response = compose_json_response(success=True, data=None, message=None, code=400) return response
def get_places_to_discover(*args, **kwargs): current_coords = { 'latitude': request.args.get('latitude'), 'longitude': request.args.get('longitude') } if current_coords: try: places_to_discover = Visit.get_places_to_discover(current_coords) if len(places_to_discover): response = compose_json_response(success=True, data=places_to_discover, message=None, code=200) else: response = compose_json_response(success=False, data=None, message="No places found for current location", code=400) except: response = compose_json_response(success=False, data=None, message=None, code=500) else: response = compose_json_response(success=False, data=None, message=None, code=400) return response
def create_visit(*args, **kwargs): data = request.get_json() try: user_id = User.get_current().id yelp_id = data['yelp_id'] attend_date = data['attend_date'] satisfaction = data['satisfaction'] new_visit = Visit(user_id=user_id, yelp_id=yelp_id, attend_date=attend_date, satisfaction=satisfaction) new_visit.save() response = compose_json_response(success=True, data=None, message=None, code=200) except OperationException: response = compose_json_response(success=False, data=None, message=None, code=500) except KeyError: response = compose_json_response(success=False, data=None, message=None, code=400) return response
def expired_token_loader(_): return compose_json_response(success=False, data=None, message="Expired authorization header", code=401)
def unauthorized_loader(_): return compose_json_response(success=False, data=None, message="Missing authorization header", code=401)
def get_visit_with_uuid(visit_uuid, *args, **kwargs): visit = Visit.get_visit_with_uuid(visit_uuid) if visit: return compose_json_response(success=True, data=visit.to_json(), message=None, code=200) return compose_json_response(success=False, data=None, message=None, code=404)
def get_me(*args, **kwargs): current_user = User.get_current() return compose_json_response(success=True, data=current_user.to_json(), message=None, code=200)