def add_meals(self, update, context): # keyboard assignment self.keyboard = "add_meals" # set status to True and the others to False self.adding_meals = True # message is created according to the list to_write = functions.list_to_text( sorted(self.list.values(), key=lambda x: x[0].category)) # message is send self.send_message(update, context, to_write)
def see_list(self, update, context): # keyboard assignment self.keyboard = None # this makes adding_meals True # and adding_ingridients and removing_ingridients False self.adding_meals = True # then, this makes adding_ingridients False self.adding_meals = False # message is created according to the list to_write = functions.list_to_text( sorted(self.list.values(), key=lambda x: x[0].category)) # message is send self.send_message(update, context, to_write)
def text_message(self, update, context): """ this function handles text messages without commands. Depending on the last command used it might add ingridients from a meal to the list, a particular ingridient, remove an ingridient from the list or do nothing """ # check mode if self.adding_meals: # text from the message is retrieved typed_meal = update.message.text # we get the instance from the meal list. It might be None meal = self.meal_list.get(typed_meal) try: # might produce an AttributeError if ingridients is None # every ingridient in the meal is checked for ingridient in meal.ingridients: # if it's already in self.list the quantity increases if ingridient.name in self.list.keys(): self.list[ingridient.name][1] += 1 else: # the instance is added to the list self.list[ingridient.name] = [ingridient, 1] # the list is transformed to text to_write = functions.list_to_text( sorted(self.list.values(), key=lambda x: x[0].category)) except AttributeError: to_write = MESSAGES["meal_error"] # message is send self.send_message(update, context, to_write) # check mode elif self.adding_ingridients: # text from the message is retrieved typed_ingridient = update.message.text # we get the instance from the ingridients list. It might be None ingridient = self.ingridients.get(typed_ingridient) try: # might produce an AttributeError if ingridients is None # if it's already in self.list the quantity increases if ingridient.name in self.list.keys(): self.list[ingridient.name][1] += 1 else: # the instance is added to the list self.list[ingridient.name] = [ingridient, 1] # the list is transformed to text to_write = functions.list_to_text( sorted(self.list.values(), key=lambda x: x[0].category)) except AttributeError: to_write = MESSAGES["add_ingridient_error"] # message is send self.send_message(update, context, to_write) # check mode elif self.removing_ingridients: # text from the message is retrieved typed_ingridient = update.message.text try: # might produce a KeyError if typed_meal is not in self.list # decreases amounot of the ingridient self.list[typed_ingridient][1] -= 1 # remove igridient from list when the quantity is 0 if self.list[typed_ingridient][1] == 0: del self.list[typed_ingridient] # the list is transformed to text to_write = functions.list_to_text( sorted(self.list.values(), key=lambda x: x[0].category)) except KeyError: to_write = MESSAGES["remove_ingridient_error"] # message is send self.keyboard = "remove_ingridients" self.send_message(update, context, to_write)