From e27e87c43b7b8306aad407c3d3f96bd76e1552b5 Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Sun, 2 Jun 2024 18:51:00 +0300 Subject: [PATCH] Fix error upon an attempt to display information from an id which does not exist; Allow a deletion of multiple bookmarks. --- bukubot/xmpp/chat.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/bukubot/xmpp/chat.py b/bukubot/xmpp/chat.py index 2fdefe9..b16114b 100644 --- a/bukubot/xmpp/chat.py +++ b/bukubot/xmpp/chat.py @@ -48,7 +48,7 @@ class Chat: case _ if message_lowercase.startswith('id'): idx = message_lowercase[2:] result = bookmarks_db.get_rec_by_id(idx) - message_body = Chat.format_message(result, extended=True) + message_body = Chat.format_message(result, extended=True) if result else 'Index{} does not exist.'.format(idx) case 'last': idx = bookmarks_db.get_max_id() result = bookmarks_db.get_rec_by_id(idx) @@ -79,17 +79,26 @@ class Chat: # message_body = bookmark_regexp(message[7:]) case _ if message_lowercase.startswith('del '): val = message_lowercase[4:] - try: - idx = int(val) - except: - idx = bookmarks_db.get_rec_id(val) - bookmark = bookmarks_db.get_rec_by_id(idx) - message_body = Chat.format_message(bookmark, extended=True) if bookmark else '' - result = bookmarks_db.delete_rec(idx) - if result: - message_body += '\n*Bookmark has been deleted.*' - else: - message_body += '\n*No action has been taken for index {}*'.format(idx) + ixs = message_lowercase[4:].split(',') + message_body = '' + ixs_accepted = [] + ixs_rejected = [] + for ix in ixs: + try: + ixs_accepted.append(int(ix)) + except: + ixs_rejected.append(ix) + ixs_accepted.sort(reverse=True) + for ix in ixs_accepted: + bookmark = bookmarks_db.get_rec_by_id(ix) + if bookmark: message_body += Chat.format_message(bookmark, extended=True) + result = bookmarks_db.delete_rec(ix) + if result: + message_body += '\n*Bookmark has been deleted.*\n' + else: + message_body += '*Index {} does not exist.*\n'.format(ix) + message_body += '\n' + message_body += '*Rejected indexes:* {} (ID must be a number)'.format(', '.join(ixs_rejected)) case _ if message_lowercase.startswith('mod '): message_lowercase_split = message_lowercase[4:].split(' ') if len(message_lowercase_split) > 2: @@ -126,14 +135,9 @@ class Chat: message_lowercase.startswith('tag -')): message_lowercase_split = message_lowercase[4:].split(' ') try: - int(message_lowercase_split[1]) if len(message_lowercase_split) > 2: arg = message_lowercase_split[0] - val = message_lowercase_split[1] - try: - idx = int(val) - except: - idx = bookmarks_db.get_rec_id(val) + idx = int(message_lowercase_split[1]) # tag = ',' + ' '.join(message_lowercase_split[2:]) + ',' # tag = ' '.join(message_lowercase_split[2:]) tag = arg + ',' + ' '.join(message_lowercase_split[2:])