diff --git a/bot/commands/journal.py b/bot/commands/journal.py index 86df783..68b79bb 100644 --- a/bot/commands/journal.py +++ b/bot/commands/journal.py @@ -3,7 +3,7 @@ import os from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.constants import ParseMode - +import models ENTRY_OPTIONS, CONTENT_ENTRY = range(2) BUTTON_COUNT = 5 @@ -12,8 +12,7 @@ BUTTON_COUNT = 5 from .basehandler import BaseHandler class JournalHandler(BaseHandler): - def __init__(self, entry_string, models): - self.models = models + def __init__(self, entry_string): self.entry_string = entry_string self.handler = ConversationHandler( entry_points=[CommandHandler(entry_string, self.entry_point)], @@ -74,12 +73,12 @@ class JournalHandler(BaseHandler): await query.answer() date = datetime.datetime.strptime(query.data, "%d%m%Y").date() - with self.models.db: - self.current_model, new = self.models.JournalEntry.get_or_create( + with models.db: + self.current_model, new = models.JournalEntry.get_or_create( date = date ) if new: - count = self.models.JournalEntry.select().count() + count = models.JournalEntry.select().count() await query.edit_message_text( text=f"Journal entry no. {count}. What happened on {self.current_model.date_pretty}?" ) @@ -128,8 +127,8 @@ class JournalHandler(BaseHandler): return ENTRY_OPTIONS if context.chat_data.get("delete", False): # if not set, delete was not chosen - with self.models.db: - self.current_model = self.models.JournalEntry.get_or_none( + with models.db: + self.current_model = models.JournalEntry.get_or_none( date = date ) if self.current_model: @@ -139,15 +138,15 @@ class JournalHandler(BaseHandler): context.chat_data["delete"] = False return ConversationHandler.END else: - with self.models.db: - self.current_model, new = self.models.JournalEntry.get_or_create( + with models.db: + self.current_model, new = models.JournalEntry.get_or_create( date = date ) if not new: await update.message.reply_text("An entry already exists for this date") return ConversationHandler.END else: - count = self.models.JournalEntry.select().count() + count = models.JournalEntry.select().count() await update.message.reply_text( text=f"Journal entry no. {count}. What happened on {self.current_model.date_pretty}?" ) @@ -155,7 +154,7 @@ class JournalHandler(BaseHandler): async def content_save(self, update, context): - with self.models.db: + with models.db: self.current_model.author_id = update.message.from_user.id if update.message.text: @@ -187,7 +186,7 @@ class JournalHandler(BaseHandler): async def delete_entry(self, update, context): - with self.models.db: + with models.db: self.current_model.delete_instance() context.chat_data["delete"] = False await update.message.reply_text(text="Entry deleted ✅") diff --git a/bot/commands/list/list.py b/bot/commands/list/list.py index c0b0c78..26eab12 100644 --- a/bot/commands/list/list.py +++ b/bot/commands/list/list.py @@ -15,8 +15,7 @@ from ..basehandler import BaseHandler class ListHandler(BaseHandler): """Create and edit lists""" - def __init__(self, entry_string, models): - del models # not needed here, but part of the template + def __init__(self, entry_string): self.entry_string = entry_string set_db(PERSISTENCE_DIR / "lists.sqlite") diff --git a/bot/commands/memory.py b/bot/commands/memory.py index cd25b65..37d47b9 100644 --- a/bot/commands/memory.py +++ b/bot/commands/memory.py @@ -1,15 +1,14 @@ import os from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler, CallbackContext from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, InputMediaPhoto -# ACTION_CHOICE, DATE_ENTRY, ADD_CONTENT = range(3) +import models MEMORY_CHOICE = range(1) from .basehandler import BaseHandler class MemoryHandler(BaseHandler): - def __init__(self, entry_string, models): - self.models = models + def __init__(self, entry_string): self.entry_string = entry_string self.handler = ConversationHandler( entry_points=[CommandHandler(entry_string, self.entry_point, )], @@ -34,13 +33,13 @@ class MemoryHandler(BaseHandler): search_string = " ".join(context.args) if search_string == '~photo': - matching_models = self.models.JournalEntry.select().where(self.models.JournalEntry.media_path != "").order_by(self.models.JournalEntry.date) + matching_models = models.JournalEntry.select().where(models.JournalEntry.media_path != "").order_by(models.JournalEntry.date) else: # searching for text - matching_models = self.models.JournalEntry.select().where( - self.models.JournalEntry.text.contains( + matching_models = models.JournalEntry.select().where( + models.JournalEntry.text.contains( search_string ) - ).order_by(self.models.JournalEntry.date) + ).order_by(models.JournalEntry.date) # exit if no memory matches the string diff --git a/bot/commands/status.py b/bot/commands/status.py index 5ff08d3..82bde41 100644 --- a/bot/commands/status.py +++ b/bot/commands/status.py @@ -6,16 +6,14 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.constants import ParseMode import os - FIRST = 1 from .basehandler import BaseHandler class StatusHandler(BaseHandler): """Shows a short status of the program.""" - def __init__(self, entry_string, models): + def __init__(self, entry_string): self.start_time = datetime.datetime.now() self.entry_string = entry_string - self.models = models self.handler = ConversationHandler( entry_points=[CommandHandler(self.entry_string, self.entry_point)], states={ diff --git a/bot/cronjob/chat_photo.py b/bot/cronjob/chat_photo.py index 4702068..ce41177 100644 --- a/bot/cronjob/chat_photo.py +++ b/bot/cronjob/chat_photo.py @@ -4,13 +4,13 @@ from telegram.error import BadRequest import logging from datetime import time, timedelta, timezone, datetime, date from peewee import fn +import models CHAT_ID = os.getenv("CHAT_ID") class SetChatPhotoJob(): - def __init__(self, models, bot: ExtBot, job_queue): - self.models = models + def __init__(self, bot: ExtBot, job_queue): self.bot = bot self.logger = logging.getLogger(self.__class__.__name__) @@ -26,9 +26,9 @@ class SetChatPhotoJob(): async def callback_photo(self, context): # last_seen of memory must be older than 10 days in past or None - with self.models.db: - possible_photos = self.models.JournalEntry.select().where( - self.models.JournalEntry.media_path != None + with models.db: + possible_photos = models.JournalEntry.select().where( + models.JournalEntry.media_path != None ).order_by(fn.Random()) try: diff --git a/bot/cronjob/random_memory.py b/bot/cronjob/random_memory.py index e5cdf0a..f7000e5 100644 --- a/bot/cronjob/random_memory.py +++ b/bot/cronjob/random_memory.py @@ -2,10 +2,10 @@ from datetime import time, timedelta, timezone, datetime, date import os from peewee import fn import logging +import models class RandomMemoryJob(): - def __init__(self, models, bot, job_queue): - self.models = models + def __init__(self, bot, job_queue): self.bot = bot self.logger = logging.getLogger(self.__class__.__name__) @@ -23,10 +23,10 @@ class RandomMemoryJob(): async def callback_memory(self, context): # last_seen of memory must be older than 10 days in past or None - with self.models.db: - possible_entries = self.models.JournalEntry.select().where( - (self.models.JournalEntry.last_shown <= datetime.today().date() - timedelta(days=self.min_age)) | \ - (self.models.JournalEntry.last_shown == None) + with models.db: + possible_entries = models.JournalEntry.select().where( + (models.JournalEntry.last_shown <= datetime.today().date() - timedelta(days=self.min_age)) | \ + (models.JournalEntry.last_shown == None) ).order_by(fn.Random()) try: diff --git a/bot/main.py b/bot/main.py index a65debd..d450958 100644 --- a/bot/main.py +++ b/bot/main.py @@ -23,14 +23,14 @@ def main() -> None: models.set_db(db_path) application = Application.builder().token(token).build() - application.add_handler(journal.JournalHandler("journal", models).handler) - application.add_handler(list.ListHandler("list", models).handler) - application.add_handler(status.StatusHandler("status", models).handler) + application.add_handler(journal.JournalHandler("journal").handler) + application.add_handler(list.ListHandler("list").handler) + application.add_handler(status.StatusHandler("status").handler) application.add_handler(turtle.TurtleHandler().handler) - application.add_handler(memory.MemoryHandler("memory", models).handler) + application.add_handler(memory.MemoryHandler("memory").handler) - random_memory.RandomMemoryJob(models, application.bot, application.job_queue) - chat_photo.SetChatPhotoJob(models, application.bot, application.job_queue) + random_memory.RandomMemoryJob(application.bot, application.job_queue) + chat_photo.SetChatPhotoJob(application.bot, application.job_queue) # Run the bot until the user presses Ctrl-C application.run_polling()