def get_menu_template_context(buff): """ return the template name and correct context for menu """ if len(buff) == 1: day = Helper.get_day_of_week() if len(buff) > 1: day = buff[1].lower() if day in ["monday", "tuesday", "wednesday", "thursday", "friday"]: sql = CustomSQL() week = Helper.get_week_number() variables = (day, week) query_string = "SELECT food, meal, option FROM menu_table WHERE day = (%s) AND week = (%s)" menu = sql.query(query_string, variables) if menu: menu_dict = Helper.convert_menu_list_to_dict(menu) return {"template": "menu_response", "context": {"menu": menu_dict, "day": day}} elif day in ["saturday", "sunday"]: return {"template": "weekend_meal_error", "context": {}} else: return {"template": "invalid_day_error", "context": {}}
def get_menu_template_context(buff): """ return the template name and correct context for menu """ if len(buff) == 1: day = Helper.get_day_of_week() if len(buff) > 1: day = buff[1].lower() if day in ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']: sql = CustomSQL() week = Helper.get_week_number() variables = (day, week,) query_string = 'SELECT food, meal, option FROM menu_table WHERE day = (%s) AND week = (%s)' menu = sql.query(query_string, variables) if menu: menu_dict = Helper.convert_menu_list_to_dict(menu) return {'template': 'menu_response', 'context': {'menu': menu_dict, 'day': day}} elif day in ['saturday', 'sunday']: return {'template': 'weekend_meal_error', 'context': {}} else: return {'template': 'invalid_day_error', 'context': {}}
def check_multiple_rating(user_id, meal): sql = CustomSQL() variables = (user_id, meal) query = "SELECT count(rating.id) FROM rating INNER JOIN menu_table ON menu_table.id = menu_id WHERE rating.user_id = (%s) AND menu_table.meal = (%s) AND rating.created_at::date = now()::date" result = sql.query(query, variables) if int(result[0][0]) > 0: return True else: return False
def check_option_selected(option, day, week, meal): variables = (meal, day, week) sql = CustomSQL() query_string = "SELECT count(option) FROM menu_table WHERE meal = (%s) AND day = (%s) AND week = (%s)" option_count_sql = sql.query(query_string, variables) option_count = int(option_count_sql[0][0]) try: option_int = int(option) except ValueError: option_int = 0 if option_int not in range(1, option_count + 1): return {"bool": False, "option": option_count} else: return {"bool": True}
def get_rate_template_context(buff, user_id): """ return the template name and correct context for rating and comment """ day = Helper.get_day_of_week() week = Helper.get_week_number() if day in ["saturday", "sunday"]: return {"template": "weekend_rate_error", "context": {}} else: meal = buff[1] option = buff[2] rating = buff[3] comment = " ".join(buff[4:]) or "no comment" check_option = Helper.check_option_selected(option, day, week, meal) if not Helper.check_meal_selected(meal): return {"template": "invalid_meal", "context": {}} if not check_option["bool"]: return {"template": "invalid_option", "context": {"option_count": check_option["option"]}} if not Helper.check_rating(rating): return {"template": "invalid_rating", "context": {}} if Helper.check_multiple_rating(user_id, meal): return {"template": "multiple_rating", "context": {}} variables = (meal, day, week, option) sql = CustomSQL() query_string = ( "SELECT id FROM menu_table WHERE meal = (%s) AND day = (%s) AND week = (%s) AND option = (%s)" ) result = sql.query(query_string, variables) food_menu_id = int(result[0][0]) get_date = Helper.get_date() variables = (user_id, food_menu_id, rating, comment, get_date) query_string = ( "INSERT INTO rating (user_id, menu_id, rate, comment, created_at) VALUES (%s, %s, %s, %s, %s)" ) sql.command(query_string, variables) return {"template": "rating_response", "context": {}}
def get_rate_template_context(buff, user_id): """ return the template name and correct context for rating and comment """ day = Helper.get_day_of_week() week = Helper.get_week_number() if day in ['saturday', 'sunday']: return {'template': 'weekend_rate_error', 'context': {}} else: meal = buff[1] option = buff[2] rating = buff[3] comment = ' '.join(buff[4:]) or 'no comment' check_option = Helper.check_option_selected(option, day, week, meal) if Helper.check_meal_selected(meal) is False: return {'template': 'invalid_meal', 'context': {}} if check_option['bool'] is False: return {'template': 'invalid_option', 'context': {'option_count': check_option['option']}} if Helper.check_rating(rating) is False: return {'template': 'invalid_rating', 'context': {}} variables = (meal, day, week, option,) sql = CustomSQL() query_string = 'SELECT id FROM menu_table WHERE meal = (%s) AND day = (%s) AND week = (%s) AND option = (%s)' result = sql.query(query_string, variables) food_menu_id = int(result[0][0]) variables = (user_id, food_menu_id, rating, comment) query_string = "INSERT INTO rating (user_id, menu_id, rate, comment) VALUES (%s, %s, %s, %s)" sql.command(query_string, variables) return {'template': 'rating_response', 'context': {}}