use db contexts for better stability
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		| @@ -51,9 +51,10 @@ class JournalHandler(BaseHandler): | ||||
|         await query.answer() | ||||
|         if query.data == "today" or query.data == "yesterday": | ||||
|             date = datetime.datetime.now().date() if query.data == "today" else datetime.datetime.now().date() - datetime.timedelta(days=1) | ||||
|             self.current_model, new = self.models.JournalEntry.get_or_create( | ||||
|                 date = date | ||||
|             ) | ||||
|             with self.models.db: | ||||
|                 self.current_model, new = self.models.JournalEntry.get_or_create( | ||||
|                     date = date | ||||
|                 ) | ||||
|             if not new: | ||||
|                 await query.edit_message_text(text="An entry already exists for this date") | ||||
|                 return ConversationHandler.END | ||||
| @@ -69,9 +70,10 @@ class JournalHandler(BaseHandler): | ||||
|         date = update.message.text | ||||
|         try: | ||||
|             date = datetime.datetime.strptime(date, "%d%m%Y").date() | ||||
|             self.current_model, new = self.models.JournalEntry.get_or_create( | ||||
|                 date = date | ||||
|             ) | ||||
|             with self.models.db: | ||||
|                 self.current_model, new = self.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 | ||||
|   | ||||
| @@ -3,7 +3,7 @@ from pathlib import Path | ||||
| from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler | ||||
| from telegram import InlineKeyboardButton, InlineKeyboardMarkup | ||||
|  | ||||
| from .models import ListModel, set_db | ||||
| from .models import ListModel, set_db, db | ||||
|  | ||||
| MEDIA_DIR = Path(os.getenv("MEDIA_DIR")) | ||||
| DB_DIR = MEDIA_DIR / "lists_db" | ||||
| @@ -19,7 +19,7 @@ class ListHandler(BaseHandler): | ||||
|     """Create and edit lists""" | ||||
|  | ||||
|     def __init__(self, entry_string, models): | ||||
|         self.models = models | ||||
|         self.journal_models = models # not needed here | ||||
|         self.entry_string = entry_string | ||||
|         self.handler = ConversationHandler( | ||||
|             entry_points=[CommandHandler(entry_string, self.entry_point)], | ||||
| @@ -47,7 +47,8 @@ class ListHandler(BaseHandler): | ||||
|     async def entry_point(self, update, context) -> None: | ||||
|         await super().entry_point(update, context) | ||||
|         set_db(DB_DIR / f"{update.message.chat_id}.db") | ||||
|         lists = ListModel.select() | ||||
|         with db: | ||||
|             lists = ListModel.select() | ||||
|         keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.name}")] for k in lists] + \ | ||||
|             [[InlineKeyboardButton("New list", callback_data="new")]] | ||||
|  | ||||
| @@ -103,7 +104,8 @@ class ListHandler(BaseHandler): | ||||
|     async def new_listname(self, update, context) -> None: | ||||
|         name = update.message.text | ||||
|         try: | ||||
|             ListModel.create(name = name) | ||||
|             with db: | ||||
|                 ListModel.create(name = name) | ||||
|             keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]] | ||||
|             reply_markup = InlineKeyboardMarkup(keyboard) | ||||
|             self.current_name = name | ||||
| @@ -124,7 +126,8 @@ class ListHandler(BaseHandler): | ||||
|     async def list_remove(self, update, context) -> None: | ||||
|         query = update.callback_query | ||||
|         await query.answer() | ||||
|         list_object = ListModel.get(name = self.current_name) | ||||
|         with db: | ||||
|             list_object = ListModel.get(name = self.current_name) | ||||
|  | ||||
|         keyboard = [[InlineKeyboardButton(k, callback_data=i)] for i,k in enumerate(list_object.content_list)] | ||||
|         reply_markup = InlineKeyboardMarkup(keyboard) | ||||
| @@ -136,7 +139,8 @@ class ListHandler(BaseHandler): | ||||
|     async def list_clear(self, update, context) -> None: | ||||
|         query = update.callback_query | ||||
|         await query.answer() | ||||
|         ListModel.get(name = self.current_name).content_list = [] | ||||
|         with db: | ||||
|             ListModel.get(name = self.current_name).content_list = [] | ||||
|         keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]] | ||||
|         reply_markup = InlineKeyboardMarkup(keyboard) | ||||
|         await query.edit_message_text("List " + self.current_name + " cleared", reply_markup=reply_markup) | ||||
| @@ -146,7 +150,8 @@ class ListHandler(BaseHandler): | ||||
|     async def list_delete(self, update, context) -> None: | ||||
|         query = update.callback_query | ||||
|         await query.answer() | ||||
|         ListModel.get(name = self.current_name).delete_instance() | ||||
|         with db: | ||||
|             ListModel.get(name = self.current_name).delete_instance() | ||||
|         await query.edit_message_text("List " + self.current_name + " deleted") | ||||
|         return ConversationHandler.END | ||||
|  | ||||
| @@ -154,7 +159,8 @@ class ListHandler(BaseHandler): | ||||
|     async def list_print(self, update, context) -> None: | ||||
|         query = update.callback_query | ||||
|         await query.answer() | ||||
|         it = ListModel.get(name = self.current_name).content_list | ||||
|         with db: | ||||
|             it = ListModel.get(name = self.current_name).content_list | ||||
|         if it: | ||||
|             content = "·" + "\n· ".join(it) | ||||
|         else: | ||||
| @@ -168,7 +174,8 @@ class ListHandler(BaseHandler): | ||||
|  | ||||
|     async def list_add_item(self, update, context) -> None: | ||||
|         item = update.message.text | ||||
|         ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item] | ||||
|         with db: | ||||
|             ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item] | ||||
|         # TODO test me! | ||||
|         keyboard = [[InlineKeyboardButton("Add some more", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]] | ||||
|         reply_markup = InlineKeyboardMarkup(keyboard) | ||||
| @@ -181,10 +188,11 @@ class ListHandler(BaseHandler): | ||||
|         ind = int(query.data) | ||||
|         await query.answer() | ||||
|  | ||||
|         list_object = ListModel.get(name = self.current_name) | ||||
|         old = list_object.content_list | ||||
|         name = old.pop(ind) | ||||
|         list_object.content_list = old | ||||
|         with db: | ||||
|             list_object = ListModel.get(name = self.current_name) | ||||
|             old = list_object.content_list | ||||
|             name = old.pop(ind) | ||||
|             list_object.content_list = old | ||||
|  | ||||
|         keyboard = [[InlineKeyboardButton("Remove another", callback_data="remove"), InlineKeyboardButton("Back to the menu", callback_data="overview")]] | ||||
|         reply_markup = InlineKeyboardMarkup(keyboard)       | ||||
|   | ||||
| @@ -19,7 +19,8 @@ class ListModel(BaseModel): | ||||
|     @content_list.setter | ||||
|     def content_list(self, list_content): | ||||
|         self.content = json.dumps(list_content) | ||||
|         self.save() | ||||
|         with db: | ||||
|             self.save() | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,9 @@ | ||||
| import datetime | ||||
| import requests | ||||
| import httpx | ||||
| import socket | ||||
| from telegram.ext import ConversationHandler, CommandHandler, CallbackQueryHandler, ParseMode | ||||
| from telegram.ext import ConversationHandler, CommandHandler, CallbackQueryHandler | ||||
| from telegram import InlineKeyboardButton, InlineKeyboardMarkup | ||||
| from telegram.constants import ParseMode | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -27,7 +28,7 @@ class StatusHandler(BaseHandler): | ||||
|  | ||||
|  | ||||
|     async def entry_point(self, update, context) -> None: | ||||
|         super().entry_point(update, context) | ||||
|         await super().entry_point(update, context) | ||||
|         keyboard = [ | ||||
|             [ | ||||
|                 InlineKeyboardButton("And the log?", callback_data="full"), | ||||
| @@ -36,10 +37,10 @@ class StatusHandler(BaseHandler): | ||||
|         reply_markup = InlineKeyboardMarkup(keyboard) | ||||
|  | ||||
|         delta = str(datetime.datetime.now() - self.start_time) | ||||
|         message = "BeebBop, this is Norbit)\n" | ||||
|         message = "BeebBop, this is Norbit\n" | ||||
|  | ||||
|         try: | ||||
|             ip = requests.get('https://api.ipify.org').text | ||||
|             ip = httpx.get('https://api.ipify.org').text | ||||
|             with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | ||||
|                 s.connect(('8.8.8.8', 80)) | ||||
|                 (addr, port) = s.getsockname() | ||||
| @@ -49,15 +50,14 @@ class StatusHandler(BaseHandler): | ||||
|             local_ips = "not fetchable" | ||||
|  | ||||
|         message += "Status: Running 🟢\n" | ||||
|         message += "Uptime: `" + delta[:delta.rfind(".")] + "`\n" | ||||
|         message += "IP (public): `" + ip + "`\n" | ||||
|         message += "IP (private): `" + str(local_ips) + "`\n" | ||||
|         message += f"Uptime: `{delta[:delta.rfind('.')]}`\n" | ||||
|         message += f"IP \(public\): `{ip}`\n" | ||||
|         message += f"IP \(private\): `{local_ips}`\n" | ||||
|  | ||||
|          | ||||
|         if update.message: | ||||
|             await update.message.reply_text(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) | ||||
|             await update.message.reply_text(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN_V2) | ||||
|         else: | ||||
|             await update._effective_chat.send_message(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) | ||||
|             await update._effective_chat.send_message(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN_V2) | ||||
|          | ||||
|         return FIRST | ||||
|  | ||||
| @@ -65,10 +65,7 @@ class StatusHandler(BaseHandler): | ||||
|     async def send_log(self, update, context) -> None: | ||||
|         query = update.callback_query | ||||
|         wanted = query.data.replace("status-","") | ||||
|         query.answer() | ||||
|         # with open("persistence/server.log") as l: | ||||
|         #     query.message.reply_document(l) | ||||
|         await update.message.reply_text("Not implemented yet.") | ||||
|         super().log_activity(read = False, execute = False, send = True) | ||||
|         await query.answer() | ||||
|         await query.edit_message_text("Here you go: https://portainer.kluster.moll.re/#!/1/kubernetes/applications/journal/journal-botte") | ||||
|         return ConversationHandler.END | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user