add day rating feature
This commit is contained in:
		| @@ -5,7 +5,7 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup | ||||
| from telegram.constants import ParseMode | ||||
| import models | ||||
|  | ||||
| ENTRY_OPTIONS, CONTENT_ENTRY = range(2) | ||||
| ENTRY_OPTIONS, CONTENT_ENTRY, DAY_RATING = range(3) | ||||
| BUTTON_COUNT = 5 | ||||
|  | ||||
|  | ||||
| @@ -26,6 +26,9 @@ class JournalHandler(BaseHandler): | ||||
|                 CONTENT_ENTRY: [ | ||||
|                     MessageHandler(filters.ALL, self.content_save), | ||||
|                     ], | ||||
|                 DAY_RATING: [ | ||||
|                     CallbackQueryHandler(self.day_rating_save), | ||||
|                     ], | ||||
|             }, | ||||
|             fallbacks=[], | ||||
|         ) | ||||
| @@ -41,21 +44,12 @@ class JournalHandler(BaseHandler): | ||||
|  | ||||
|         dates = [(datetime.datetime.now() - datetime.timedelta(days = i)).date() for i in range(BUTTON_COUNT + 2)][::-1] | ||||
|         # since there are two buttons additional buttons, we need to have two more days | ||||
|         names = [d.strftime("%d.%m.") for d in dates] | ||||
|         names = get_names(dates) | ||||
|         callbacks = [d.strftime("%d%m%Y") for d in dates] | ||||
|         names[-1] = "Today" | ||||
|         names[-2] = "Yesterday" | ||||
|  | ||||
|         options = [ | ||||
|             [ | ||||
|                 InlineKeyboardButton(names[-1], callback_data=callbacks[-1]) | ||||
|             ], | ||||
|             [ | ||||
|                 InlineKeyboardButton(names[-2], callback_data=callbacks[-2]) | ||||
|             ], | ||||
|             [ | ||||
|                 InlineKeyboardButton(n, callback_data=c) for n,c in zip(names[:-2], callbacks[:-2]) | ||||
|             ], | ||||
|                 [InlineKeyboardButton(n, callback_data=c)] for n,c in zip(names[::-1], callbacks[::-1]) | ||||
|         ] + [ | ||||
|             [ | ||||
|                 InlineKeyboardButton("<<", callback_data=BUTTON_COUNT + 2) | ||||
|             ], | ||||
| @@ -95,16 +89,16 @@ class JournalHandler(BaseHandler): | ||||
|         delta = int(query.data) | ||||
|  | ||||
|         dates = [(datetime.datetime.now() - datetime.timedelta(days = i + delta)).date() for i in range(BUTTON_COUNT)][::-1] | ||||
|         names = [d.strftime("%d.%m.") for d in dates] | ||||
|         names = get_names(dates) | ||||
|         callbacks = [d.strftime("%d%m%Y") for d in dates] | ||||
|  | ||||
|         options = [ | ||||
|             [ | ||||
|                 InlineKeyboardButton(">>", callback_data=delta - BUTTON_COUNT) | ||||
|             ], | ||||
|             [ | ||||
|                 InlineKeyboardButton(n, callback_data=c) for n,c in zip(names, callbacks) | ||||
|             ], | ||||
|             ] | ||||
|         ] + [ | ||||
|             [InlineKeyboardButton(n, callback_data=c)] for n,c in zip(names[::-1], callbacks[::-1]) | ||||
|         ] + [ | ||||
|             [ | ||||
|                 InlineKeyboardButton("<<", callback_data=delta + BUTTON_COUNT) | ||||
|             ], | ||||
| @@ -173,7 +167,23 @@ class JournalHandler(BaseHandler): | ||||
|  | ||||
|             self.current_model.save() | ||||
|  | ||||
|         await update.message.reply_text(f"Saved entry ✅") | ||||
|         options = [ | ||||
|             [InlineKeyboardButton(models.RATING_MAPPING[idx], callback_data=idx) for idx in [1,2,3,4,5]] | ||||
|         ] | ||||
|  | ||||
|         await update.message.reply_text(f"Saved entry ✅. How was the day?", reply_markup=InlineKeyboardMarkup(options)) | ||||
|  | ||||
|         return DAY_RATING | ||||
|  | ||||
|  | ||||
|     async def day_rating_save(self, update, context): | ||||
|         query = update.callback_query | ||||
|         await query.answer() | ||||
|         rating = int(query.data) | ||||
|         with models.db: | ||||
|             self.current_model.rating = rating | ||||
|             self.current_model.save() | ||||
|         await query.edit_message_text(text="Rating saved ✅") | ||||
|         return ConversationHandler.END | ||||
|  | ||||
|  | ||||
| @@ -190,3 +200,22 @@ class JournalHandler(BaseHandler): | ||||
|             self.current_model.delete_instance() | ||||
|         context.chat_data["delete"] = False | ||||
|         await update.message.reply_text(text="Entry deleted ✅") | ||||
|  | ||||
|  | ||||
|  | ||||
| ### HELPERS | ||||
|  | ||||
| def get_names(dates: list): | ||||
|     names = [] | ||||
|     for d in dates: | ||||
|         suffix = "" | ||||
|         if models.JournalEntry.get_or_none(date = d): | ||||
|             suffix = " ✅" | ||||
|          | ||||
|         if d == datetime.datetime.now().date(): | ||||
|             names.append("Today" + suffix) | ||||
|         elif d == datetime.datetime.now().date() - datetime.timedelta(days = 1): | ||||
|             names.append("Yesterday" + suffix) | ||||
|         else: | ||||
|             names.append(d.strftime("%d.%m.") + suffix) | ||||
|     return names | ||||
|   | ||||
| @@ -69,21 +69,22 @@ class MemoryHandler(BaseHandler): | ||||
|         matching_models = context.chat_data["kept_matches"] | ||||
|         chosen_match = matching_models[ind] | ||||
|  | ||||
|         rating_string = f" ({models.RATING_MAPPING[chosen_match.rating]})" if chosen_match.rating else "" | ||||
|  | ||||
|         message_text = f"On {chosen_match.date_pretty}{rating_string}, " \ | ||||
|                 f"{chosen_match.author} wrote: \n" \ | ||||
|                 f"{chosen_match.spoiler_text}" | ||||
|  | ||||
|         if chosen_match.media_path: | ||||
|             # context.bot.sendPhoto() | ||||
|             await update.effective_message.reply_photo( | ||||
|                 photo = chosen_match.media_path, | ||||
|                 caption= | ||||
|                     f"On {chosen_match.date_pretty}, " | ||||
|                     f"{chosen_match.author} wrote: \n" | ||||
|                     f"{chosen_match.spoiler_text}", | ||||
|                 caption = message_text, | ||||
|                 parse_mode=ParseMode.HTML | ||||
|                 ) | ||||
|         else: | ||||
|             await query.edit_message_text( | ||||
|                 f"On {chosen_match.date_pretty}, " | ||||
|                 f"{chosen_match.author} wrote: \n" | ||||
|                 f"{chosen_match.spoiler_text}", | ||||
|                 message_text, | ||||
|                 parse_mode=ParseMode.HTML | ||||
|             ) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user