-
Notifications
You must be signed in to change notification settings - Fork 2
Add add_favorites command #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| from util.messages import settings_msg | ||
| from util.kb_mark_up import settings_kb, hidden_cuisine_kb | ||
| from util.kb_mark_up import settings_kb, hidden_cuisine_kb, favorites_kb | ||
| from util.util import parse_callback | ||
| from database.database import get_hidden_cuisines, update_hidden_cuisine | ||
| from util.messages import no_hidden_cuisine_msg, hidden_cuisine_msg | ||
| from database.database import get_hidden_cuisines, update_hidden_cuisine, get_favorite_foods, update_favorite_foods | ||
| from util.messages import (no_hidden_cuisine_msg, hidden_cuisine_msg, add_favorite_no_input_msg, | ||
| add_favorite_already_exists_msg, added_favorites_msg, no_favorites_msg, favorites_msg) | ||
| from util.formatting import normalize | ||
| import logging | ||
|
|
||
|
|
||
| def handle_settings(update, context): | ||
|
|
@@ -19,3 +22,44 @@ def handle_hide_cuisine(update, context): | |
| cuisine_to_hide = parse_callback(update.callback_query.data)[1] | ||
| updated_hidden_cuisine = update_hidden_cuisine(update.effective_chat.id, cuisine_to_hide) | ||
| update.callback_query.edit_message_reply_markup(reply_markup=hidden_cuisine_kb(updated_hidden_cuisine)) | ||
|
|
||
|
|
||
| def handle_add_favorite(update, context): | ||
| food_to_add = ' '.join(context.args) | ||
| if food_to_add == '': | ||
| update.message.reply_text(add_favorite_no_input_msg()) | ||
| return | ||
|
|
||
| food_to_add = normalize(food_to_add) | ||
| favorites = update_favorite_foods(update.effective_chat.id, food_to_add) | ||
|
|
||
| if favorites is None: | ||
| update.message.reply_text(add_favorite_already_exists_msg()) | ||
| return | ||
|
|
||
| update.message.reply_text(added_favorites_msg(favorites)) | ||
|
|
||
|
|
||
| def handle_remove_favorite(update, context): | ||
| favorites = get_favorite_foods(update.effective_chat.id) | ||
| if len(favorites) == 0: | ||
| update.message.reply_text(no_favorites_msg()) | ||
| return | ||
|
|
||
| update.message.reply_text('Select your favorite food to remove:', reply_markup=favorites_kb(favorites)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abstract away the reply text to const.py or message.py? |
||
|
|
||
|
|
||
| def handle_remove_favorite_callback(update, context): | ||
| food_to_remove = parse_callback(update.callback_query.data)[1] | ||
| favorites = update_favorite_foods(update.effective_chat.id, food_to_remove, False) | ||
|
|
||
| if favorites is None: | ||
| update.callback_query.edit_message_text('You already removed that!') | ||
| return | ||
|
|
||
| if len(favorites) == 0: | ||
| context.bot.edit_message_text(text=no_favorites_msg(), chat_id=update.effective_chat.id, | ||
| message_id=update.callback_query.message.message_id, reply_markup=favorites_kb(favorites)) | ||
| else: | ||
| context.bot.edit_message_text(text=favorites_msg(favorites), chat_id=update.effective_chat.id, | ||
| message_id=update.callback_query.message.message_id, reply_markup=favorites_kb(favorites)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above the one-liner? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| import psycopg2.extras | ||
|
|
||
| from database.queries import menu_query, settings_query, settings_insert, settings_update | ||
| from util.const import HIDE_CUISINE | ||
| from util.const import HIDE_CUISINE, FAVORITES | ||
|
|
||
| # global connection | ||
| _connection = None | ||
|
|
@@ -49,7 +49,17 @@ def get_menu(meal, date): | |
| conn = connect() | ||
| cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) | ||
| cursor.execute(menu_query(meal), (date,)) | ||
| return cursor.fetchone() | ||
| menu = cursor.fetchone() | ||
| cursor.close() | ||
| return menu | ||
|
|
||
|
|
||
| def insert_default_user_pref(chat_id): | ||
| conn = connect() | ||
| cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) | ||
| cursor.execute(settings_insert(), (chat_id, '{}', '{}')) | ||
| conn.commit() | ||
| cursor.close() | ||
|
|
||
|
|
||
| def get_hidden_cuisines(chat_id): | ||
|
|
@@ -60,11 +70,13 @@ def get_hidden_cuisines(chat_id): | |
|
|
||
| if data is None: | ||
| # insert default settings | ||
| cursor.execute(settings_insert(), (chat_id, '{}', '{}')) | ||
| conn.commit() | ||
| return [] # return empty hidden food array | ||
| insert_default_user_pref(chat_id) | ||
| hidden = [] | ||
| else: | ||
| hidden = data[HIDE_CUISINE] | ||
|
|
||
| return data[HIDE_CUISINE] # returns hidden cuisines in user_pref | ||
| cursor.close() | ||
| return hidden # returns hidden cuisines in user_pref | ||
|
|
||
|
|
||
| def update_hidden_cuisine(chat_id, cuisine_to_hide): | ||
|
|
@@ -80,7 +92,41 @@ def update_hidden_cuisine(chat_id, cuisine_to_hide): | |
| cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) | ||
| cursor.execute(settings_update(HIDE_CUISINE), (hidden_cuisines, chat_id)) | ||
| conn.commit() | ||
| cursor.execute(settings_query(HIDE_CUISINE), (chat_id,)) | ||
| cursor.close() | ||
|
|
||
| return hidden_cuisines | ||
|
|
||
|
|
||
| def get_favorite_foods(chat_id): | ||
| conn = connect() | ||
| cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) | ||
| cursor.execute(settings_query(FAVORITES), (chat_id,)) | ||
| data = cursor.fetchone() | ||
|
|
||
| return data[HIDE_CUISINE] | ||
| if data is None: | ||
| insert_default_user_pref(chat_id) | ||
| favorites = [] | ||
| else: | ||
| favorites = data[FAVORITES] | ||
|
|
||
| cursor.close() | ||
| return favorites # returns favorites in user_pref | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we practice DRY by taking in the type of user_pref as a function parameter and extract data accordingly? |
||
|
|
||
|
|
||
| def update_favorite_foods(chat_id, favorite_food, is_add=True): | ||
| favorites = get_favorite_foods(chat_id) | ||
|
|
||
| if favorite_food in favorites and is_add or favorite_food not in favorites and not is_add: | ||
| return None | ||
| elif favorite_food in favorites and not is_add: | ||
| favorites.remove(favorite_food) | ||
| else: | ||
| favorites.append(favorite_food) | ||
|
|
||
| conn = connect() | ||
| cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) | ||
| cursor.execute(settings_update(FAVORITES), (favorites, chat_id)) | ||
| conn.commit() | ||
| cursor.close() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise, can we DRY the updating process? |
||
|
|
||
| return favorites | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,23 +5,24 @@ | |
| DINNER = 'dinner' | ||
| MENU = 'menu' | ||
| SETTINGS = 'settings' | ||
| FAVORITE = 'favorite' | ||
| FAVORITES = 'favorites' | ||
| NOTIFICATION = 'notification' | ||
| HOME = 'home' | ||
| HIDE_CUISINE = 'hidden' | ||
| ADD_FAVORITE = 'add_favorite' | ||
| REMOVE_FAVORITE = 'remove_favorite' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be Singapore readable 'favourite' for the users or left Americanized? |
||
| # menu messages | ||
| BREAKFAST_COMMAND = f'/{BREAKFAST}' | ||
| BREAKFAST_DESC = f'{BREAKFAST_COMMAND} - view today\'s breakfast menu\n' | ||
| DINNER_COMMAND = f'DINNER' | ||
| DINNER_COMMAND = f'/{DINNER}' | ||
| DINNER_DESC = f'{DINNER_COMMAND} - view today\'s dinner menu\n' | ||
| # setting messages | ||
| SETTINGS_COMMAND = f'/{SETTINGS}' | ||
| SETTINGS_DESC = f'{SETTINGS_COMMAND} - customize menu visibility and display settings\n' | ||
| ADD_FAVORITE_COMMAND = '/add_favorite' | ||
| ADD_FAVORITE_COMMAND = f'/{ADD_FAVORITE}' | ||
| ADD_FAVORITE_DESC = f'{ADD_FAVORITE_COMMAND} <food> - add favorite food for notifications\n' | ||
| REMOVE_FAVORITE_COMMAND = '/remove_favorite' | ||
| REMOVE_FAVORITE_COMMAND = f'/{REMOVE_FAVORITE}' | ||
| REMOVE_FAVORITE_DESC = f'{REMOVE_FAVORITE_COMMAND} - remove favorite food from notifications\n' | ||
| NO_FAVORITES_MSG = f'You have no favorite foods! Use {ADD_FAVORITE_COMMAND} <food> to add one!' | ||
| HELP_COMMAND = '/help' | ||
| HELP_DESC = f'{HELP_COMMAND} - show the help message\n' | ||
| SET_BREAKFAST_NOTIFICATION_COMMAND = '/set_breakfast_time' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we reduce verbosity with one-liner
update.message.reply_text(added_favorites_msg(favorites) if favorites is None else add_favorite_already_exists_msg())