Better logging, sql related patches

This commit is contained in:
Remy Moll 2021-10-11 08:28:43 +02:00
parent b5bd9f262a
commit 12602f9f84
10 changed files with 43 additions and 20 deletions

View File

@ -39,6 +39,8 @@ class Help(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
keyboard = [
[
InlineKeyboardButton("All commands", callback_data="all"),

View File

@ -40,6 +40,7 @@ class Lists(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
lists = self.db.lists.select()
sl = [l.name for l in lists]
keyboard = [[InlineKeyboardButton(k, callback_data="list-"+k)] for k in sl] + [[InlineKeyboardButton("New list", callback_data="new")]]
@ -151,8 +152,12 @@ class Lists(BotFunc):
query = update.callback_query
query.answer()
it = self.db.lists.get(self.db.lists.name == self.current_name)
content = it.content.split("<-->")
content = "\n".join(content)
if it:
content = it.content.split("<-->")
content = "\n".join(content)
else:
content = "List empty"
keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text("Content of " + self.current_name + ":\n" + content, reply_markup=reply_markup)
@ -162,7 +167,10 @@ class Lists(BotFunc):
def list_add_item(self, update: Update, context: CallbackContext) -> None:
item = update.message.text
it = self.db.lists.get(self.db.lists.name == self.current_name)
sl = it.content
if it:
sl = it.content
else:
sl = ""
sl += item + "<-->"
self.db.lists.update(content=sl).where(self.db.lists.name == self.current_name).execute()

View File

@ -11,6 +11,7 @@ class Plain(BotFunc):
return h
def add_to_log(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
super().log_activity(
read = True,
send = False,

View File

@ -1,3 +1,4 @@
from re import U
from .template import *
@ -24,7 +25,7 @@ class Joke(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
keyboard = [[InlineKeyboardButton(str(i), callback_data=str(i)) for i in range(1,11)]]
reply_markup = InlineKeyboardMarkup(keyboard)
super().log_activity(read=True, execute=True, send=True) # at this point every step has been fulfilled

View File

@ -25,7 +25,7 @@ class Search(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
update.message.reply_text("What are we searching?")
return SEARCH

View File

@ -33,6 +33,7 @@ class Status(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
keyboard = [
[
InlineKeyboardButton("And the log?", callback_data="full"),

View File

@ -23,14 +23,22 @@ class BotFunc():
def log_activity(self, **kwargs):
# mark that a new command has been executed
data = self.db.chats(
time=datetime.datetime.now(),
**kwargs
)
# kwargs can look like
# receive=True,
# execute=True,
# send=False,
data.save()
try:
data = self.db.chats(
time=datetime.datetime.now(),
**kwargs
)
# kwargs can look like
# receive=True,
# execute=True,
# send=False,
data.save()
except Exception as e:
self.logger.error("sql error: {}".format(e))
def entry_point(self, update: Update, context: CallbackContext) -> None:
if update.message.text:
self.logger.info("Chat said: {}".format(update.message.text))
else:
self.logger.info("Chat said: {}".format(update.message))

View File

@ -30,6 +30,7 @@ class Weather(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
"""Reacts the call of the command. Prints the first buttons"""
keyboard = [
[

View File

@ -27,6 +27,7 @@ class Zvv(BotFunc):
def entry_point(self, update: Update, context: CallbackContext) -> None:
super().entry_point(update, context)
update.message.reply_text("What is the start point?")
return START

View File

@ -39,12 +39,12 @@ class DBModel(Model):
logger.error(e)
# db.atomic().rollback()
def get(self, *query, **filters):
try:
return super().get(*query, **filters)
except:
logger.error("Error while 'getting' from db.")
print(query, filters)
# def get(self, *query, **filters):
# try:
# return super().get(*query, **filters)
# except Exception as e:
# logger.error("Error while executing get: {}".format(e))
# print(query, filters)
class Metric(DBModel):