From 5bdacf94965a7cdb24da3affeb40b4b72258209c Mon Sep 17 00:00:00 2001 From: Fabio De Simone Date: Tue, 29 Jan 2019 14:46:33 +0100 Subject: [PATCH] fixed /cerca to search literal string --- telegrambot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/telegrambot.py b/telegrambot.py index d6389858..91c7b0ae 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -300,13 +300,13 @@ def cmd_cerca(bot: Bot, update: Update): session = db.Session() try: try: - text = update.message.text.split(" ", 1)[1] + queryText = update.message.text.split(" ", 1)[1] except IndexError: bot.send_message(update.message.chat.id, s(strings.DIARIO_SEARCH.ERRORS.INVALID_SYNTAX)) return - text = text.replace('%', '\\%').replace('_', '\\_') - entries = session.query(db.Diario).filter(db.Diario.text.ilike('%'+text+'%')).order_by(db.Diario.id).all() - msg = f"Risultati della ricerca di {text}:\n" + queryText = queryText.replace('%', '\\%').replace('_', '\\_') + entries = session.query(db.Diario).filter(f"text ~* '(?:[^\w\d]+{queryText}[^\w\d]+|^{queryText}[^\w\d]+|^{queryText}$|[^\w\d]+{queryText}$)'").order_by(db.Diario.id).all() + msg = f"Risultati della ricerca di {queryText}:\n" for entry in entries[:5]: msg += f'#{entry.id} di {entry.author or "Anonimo"}\n{entry.text}\n\n' if len(entries) > 5: