def get_user_by_id(user_id): """""" user = "******".format(USER_MICROSERVICE_URL, user_id) response = HttpUtils.make_get_request(user) if response is None: return None user_model = UserModel() user_model.fill_from_json(response) return user_model
def force_reload_rating_all_restaurants(): """ This method call the restaurants api to force the microservice to recalculate the rating for each restaurants :return if the request is ok I rill return the request, otherwise None """ user = "******".format( RESTAURANTS_MICROSERVICE_URL) return HttpUtils.make_get_request(user)
def get_menu_restaurant(restaurant_id: int): """ This method help to retrieve all information inside the """ url = "{}/{}/menu".format(RESTAURANTS_MICROSERVICE_URL, restaurant_id) current_app.logger.debug("URL to microservices is {}".format(url)) response = HttpUtils.make_get_request(url) if response is None: return None return response["menus"]
def get_restaurant_name(restaurant_id): """ Given the id return the name of the restaurant """ response = HttpUtils.make_get_request("{}/{}/name".format( RESTAURANTS_MICROSERVICE_URL, restaurant_id)) if response is None: return "" return response["result"]
def get_count_of_positive_user(): """ This method perform the request to get all positive user inside the database """ url = "{}/report_positive".format(USER_MICROSERVICE_URL) response = HttpUtils.make_get_request(url) if response is None or len(response) == 0: return [] users = response["users"] return len(users)
def get_restaurant_people(restaurant_id: int): """ Given the id of the restaurant return the number of people at lunch and dinner """ response = HttpUtils.make_get_request("{}/stats/{}".format( BOOKING_MICROSERVICE_URL, restaurant_id)) if response is None: return [0, 0, 0] return [response["lunch"], response["dinner"], response["now"]]
def get_opening_hours_restaurant(restaurant_id: int): """ This method help to retreival all information inside the """ url = "{}/{}/openings".format(RESTAURANTS_MICROSERVICE_URL, restaurant_id) response = HttpUtils.make_get_request(url) if response is None: return None return response["openings"]
def get_all_restaurants(): """ Method to return a list of all restaurants inside the database """ url = "{}".format(RESTAURANTS_MICROSERVICE_URL) current_app.logger.debug("URL microservices: {}".format(url)) response = HttpUtils.make_get_request(url) if response is None: current_app.logger.error("Microservices error") return [] return response["restaurants"]
def get_customer_reservation(fromDate: str, toDate: str, customer_id: str): current_app.logger.debug("Filtering by: {}".format( [fromDate, toDate, customer_id])) # bind filter params... url = "{}?user_id={}".format(BOOKING_MICROSERVICE_URL, customer_id) if fromDate: url = HttpUtils.append_query(url, "fromDate", fromDate) if toDate: url = HttpUtils.append_query(url, "toDate", toDate) response = HttpUtils.make_get_request(url) return response
def search_possible_contacts(email: str = None, phone: str = None): """ Search all possible contact for the user with email of phone """ if email is not None and len(email) != 0: url = USER_MICROSERVICE_URL + "/positiveinfo/email/" + str(email) elif phone is not None and len(phone) != 0: url = USER_MICROSERVICE_URL + "/positiveinfo/phone/" + str(phone) else: return None # check if the user exists (ON VIEW) # check if the user is positive (get also date of marking) (API) ???? return HttpUtils.make_get_request(url)
def get_reservation_rest(restaurant_id, from_date, to_date): """ This method contains the logic to find all reservation in the restaurant with the filter on the date """ url = "{}/list/{}".format(BOOKING_MICROSERVICE_URL, restaurant_id) if from_date: url = HttpUtils.append_query(url, "fromDate", from_date) if to_date: url = HttpUtils.append_query(url, "toDate", to_date) response = HttpUtils.make_get_request(url) return response
def get_rest_by_id(id: int): """ This method contains the logic to get an restaurants by id :param id: The restaurants id """ url = "{}/{}".format(RESTAURANTS_MICROSERVICE_URL, id) current_app.logger.debug("URL to microservices is {}".format(url)) restaurant = HttpUtils.make_get_request(url) if restaurant is None: return None current_app.logger.debug(restaurant) restaurant_model = RestaurantModel() restaurant_model.fill_from_json(restaurant) return restaurant_model
def get_list_of_positive_user(): """ This method perform the request to get all positive user inside the database """ url = "{}/report_positive".format(USER_MICROSERVICE_URL) response = HttpUtils.make_get_request(url) if response is None or len(response) == 0: return [] users = response["users"] list_user = [] for user in users: new_user = UserModel() new_user.fill_from_json(user) list_user.append(new_user) return list_user
def get_three_reviews(restaurant_id): """ Given the restaurant_di return three random reviews """ response = HttpUtils.make_get_request("{}/{}/reviews/3".format( RESTAURANTS_MICROSERVICE_URL, restaurant_id)) if response is None: return [] review_list = [] for json in response["Reviews"]: review = ReviewModel() review.fill_from_json(json) review_list.append(review) return review_list
def get_photos_restaurants(restaurant_id: int): """ This method retrieval all information about the restaurants photos """ url = "{}/{}/photos".format(RESTAURANTS_MICROSERVICE_URL, restaurant_id) current_app.logger.debug("URL to microservices is {}".format(url)) response = HttpUtils.make_get_request(url) if response is None: return None photos = [] for json_photo in response["photos"]: current_app.logger.debug("photo {}, url {}".format( json_photo["id"], json_photo["url"])) new_photo = PhotoModel() new_photo.fill_from_json(json_photo) photos.append(new_photo) return photos
def get_dishes_restaurant(restaurant_id: int): """ This method return the restaurants dished """ url = "{}/{}/dishes".format(RESTAURANTS_MICROSERVICE_URL, restaurant_id) current_app.logger.debug("URL to microservices is {}".format(url)) response = HttpUtils.make_get_request(url) if response is None: return None dishes = [] json_dishes = response["dishes"] for json_dish in json_dishes: new_dish = DishModel() new_dish.fill_from_json(json_dish) dishes.append(new_dish) return dishes
def get_restaurants_by_keyword(name: str = None): """ This method contains the logic to perform the search restaurant by keywords The keywords supported are: https://stackoverflow.com/questions/3325467/sqlalchemy-equivalent-to-sql-like-statement :param name: is the name of restaurants """ if name is None: raise Exception("Name is required to make this type of research") response = HttpUtils.make_get_request("{}/search/{}".format( RESTAURANTS_MICROSERVICE_URL, name)) rest_list = [] for json in response["restaurants"]: rest = RestaurantModel() rest.fill_from_json(json) rest_list.append(rest) return rest_list
def mark_positive(email: str = None, phone: str = None): """ This method perform the request to user microservices to make the user positive """ current_app.logger.debug("Asking to mark. Called with: {} , {}".format( email, phone)) if email is not None and len(email) != 0: current_app.logger.debug( "marking with email because email len: {}".format(len(email))) key = "email" value = email elif phone is not None and len(phone) != 0: current_app.logger.debug( "marking with phone because phone len: {}".format(len(phone))) key = "phone" value = phone else: return None url = "{}/mark/{}/{}".format(USER_MICROSERVICE_URL, key, value) return HttpUtils.make_get_request(url)
def get_restaurant_tables(restaurant_id: int): """ This method retrieves all tables of a restaurant :param restaurant_id: id of the restaurant """ url = "{}/{}/tables".format(RESTAURANTS_MICROSERVICE_URL, restaurant_id) response = HttpUtils.make_get_request(url) # if no tables if response is None: return [] # otherwise pick from json all tables # model them into TableModel # and return a list of them all_tables = [] for json_table in response["tables"]: current_app.logger.debug("table {}, seats {}".format( json_table["name"], json_table["max_seats"])) new_table = TableModel() new_table.fill_from_json(json_table) all_tables.append(new_table) return all_tables
def search_contacts_for_email(user_email: str, user_phone: str): if user_email == "" and user_phone == "": return "Insert an email or a phone number" friends = [] contacts = [] past_restaurants = [] future_restaurants = [] if user_email != "": URL = USER_MICROSERVICE_URL + "/positiveinfo/email/" + str( user_email) else: URL = USER_MICROSERVICE_URL + "/positiveinfo/phone/" + str( user_phone) # check if the user exists # check if the user is positive (get also date of marking) (API) response = HttpUtils.make_get_request(URL) if response is None: return "Error, please try again" # now we have the information about the positivity of the user in response date_marking = response["from_date"] user_id = response["user_id"] # start to check contacts # API: get all reservation of the customer between date_marking and date_marking -14 date_marking = datetime.strptime(date_marking, "%Y-%m-%d") to_date = date_marking - timedelta(days=14) reservations_customer = BookingServices.get_reservation_by_constraint( user_id, from_data=to_date, to_data=date_marking) if reservations_customer is not None: # API: get all reservations between date_marking and date_m -14 all_reservations = BookingServices.get_reservation_by_constraint( from_data=to_date, to_data=date_marking) for reservation in reservations_customer: restaurant_id = reservation["table"]["restaurant"]["id"] restaurant = RestaurantServices.get_rest_by_id(restaurant_id) if restaurant is None: continue friends = friends + reservation["people"] start = datetime.strptime(reservation["reservation_date"], "%Y-%m-%dT%H:%M:%SZ") end = datetime.strptime(reservation["reservation_end"], "%Y-%m-%dT%H:%M:%SZ") past_restaurants.append({ "email": restaurant.owner_email, "name": restaurant.name, "date": start.strftime("%Y-%m-%dT%H:%M:%SZ"), }) for one_reservation in all_reservations: restaurant_id_contact = one_reservation["table"][ "restaurant"]["id"] if restaurant_id_contact != restaurant_id: continue start_contact = datetime.strptime( one_reservation["reservation_date"], "%Y-%m-%dT%H:%M:%SZ") end_contact = datetime.strptime( one_reservation["reservation_end"], "%Y-%m-%dT%H:%M:%SZ") # if people are in the same restaurant in the same day if start.date() != start_contact.date(): continue openings = RestaurantServices.get_opening_hours_restaurant( restaurant_id) dayNumber = start.weekday() restaurant_hours = [] for opening in openings: if opening["week_day"] == dayNumber: restaurant_hours.append( datetime.strptime(opening["open_lunch"], "%H:%M")) restaurant_hours.append( datetime.strptime(opening["close_lunch"], "%H:%M")) restaurant_hours.append( datetime.strptime(opening["open_dinner"], "%H:%M")) restaurant_hours.append( datetime.strptime(opening["close_dinner"], "%H:%M")) # if people are in the restaurant at lunch or dinner if (restaurant_hours[0].time() <= start.time() and restaurant_hours[1].time() >= end.time()) or ( restaurant_hours[2].time() <= start.time() and restaurant_hours[3].time() >= end.time()): # people are in the same restaurant at lunch # if they are in the same time if not ((end_contact < start) or (start_contact > end)): # they are contacts! # API: get user email and name of the contact user = UserService.get_user_by_id( one_reservation["customer_id"]) if user is not None: contacts.append({ "email": user.email, "name": user.firstname, "restaurant_name": restaurant.name, "date": start.strftime("%Y-%m-%dT%H:%M:%SZ"), }) friends = friends + one_reservation["people"] if user_email != "": customer_email = user_email else: URL = USER_MICROSERVICE_URL + "/" + str(user_id) user = HttpUtils.make_get_request(URL) customer_email = user["email"] # API booking: get all future booking of the customer future_reservations = BookingServices.get_reservation_by_constraint( user_id, from_data=to_date, to_data=date_marking) for future_reservation in future_reservations: date = datetime.strptime(reservation["reservation_date"], "%Y-%m-%dT%H:%M:%SZ") restaurant_id = future_reservation["table"]["restaurant"]["id"] restaurant = RestaurantServices.get_rest_by_id(restaurant_id) if restaurant is not None: future_restaurants.append({ "email": restaurant.owner_email, "name": restaurant.name, "date": date.strftime("%Y-%m-%dT%H:%M:%SZ"), "customer_email": customer_email, }) return { "friends": friends, "contacts": contacts, "past_restaurants": past_restaurants, "reservation_restaurants": future_restaurants, }
def checkin_reservations(reservation_id: int): response = HttpUtils.make_get_request("{}/{}/checkin".format( BOOKING_MICROSERVICE_URL, reservation_id)) return response