Improve deletion by using buku function delete_resultset()

This commit is contained in:
Schimon Jehudah, Adv. 2024-06-03 18:46:55 +03:00
parent e27e87c43b
commit 3387dffeaa

View file

@ -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: