From 3387dffeaaa06f8fc24ee968818d0982550a1553 Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Mon, 3 Jun 2024 18:46:55 +0300 Subject: [PATCH] Improve deletion by using buku function delete_resultset() --- bukubot/xmpp/chat.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/bukubot/xmpp/chat.py b/bukubot/xmpp/chat.py index b16114b..f8a66b5 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) if result else 'Index{} does not exist.'.format(idx) + 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) @@ -78,27 +78,35 @@ class Chat: # elif message.startswith('regex'): # message_body = bookmark_regexp(message[7:]) case _ if message_lowercase.startswith('del '): - val = message_lowercase[4:] ixs = message_lowercase[4:].split(',') - message_body = '' + ixs = set(ixs) + message_body = '*Deleted bookmarks*\n\n' ixs_accepted = [] ixs_rejected = [] for ix in ixs: try: - ixs_accepted.append(int(ix)) + ix_as_int = int(ix) + if ix_as_int > 0: + ixs_accepted.append(ix_as_int) + else: + ixs_rejected.append(ix) except: ixs_rejected.append(ix) ixs_accepted.sort(reverse=True) + ixs_approrved = [] 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' + if bookmark: + ixs_approrved.append((ix,)) 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)) + ixs_rejected.append(str(ix)) + if ixs_approrved: + for ix in ixs_approrved: + bookmark = bookmarks_db.get_rec_by_id(ix[0]) + message_body += Chat.format_message(bookmark, extended=True) + '\n\n' + result = bookmarks_db.delete_resultset(list(ixs_approrved)) + if ixs_rejected: + message_body += '*Deletion has failed for indexes:* {}'.format(', '.join(ixs_rejected)) case _ if message_lowercase.startswith('mod '): message_lowercase_split = message_lowercase[4:].split(' ') if len(message_lowercase_split) > 2: