From 288e5157fa25a529888bab6dd946bdf5a583ed67 Mon Sep 17 00:00:00 2001 From: Schimon Jehudah Date: Fri, 16 Feb 2024 02:59:01 +0000 Subject: [PATCH] Fix submission of filters --- slixfeed/sqlite.py | 41 +++++++++++++++++++++++++++++++++++++---- slixfeed/xmpp/client.py | 10 +++------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/slixfeed/sqlite.py b/slixfeed/sqlite.py index b4c9a0f..8f8af23 100644 --- a/slixfeed/sqlite.py +++ b/slixfeed/sqlite.py @@ -1968,6 +1968,41 @@ async def update_filters_value(db_file, key_value): cur.execute(sql, par) +async def is_filter_key(db_file, key): + """ + Get filters key. + + Parameters + ---------- + db_file : str + Path to database file. + key : str + Key: allow, deny. + + Returns + ------- + key : str + Key. + """ + with create_connection(db_file) as conn: + cur = conn.cursor() + try: + sql = ( + """ + SELECT key + FROM filters + WHERE key = ? + """ + ) + par = (key,) + key = cur.execute(sql, par) + key = True + except: + key = False + logging.debug("No key {}.".format(key)) + return key + + async def get_filters_value(db_file, key): """ Get filters value. @@ -1981,7 +2016,7 @@ async def get_filters_value(db_file, key): Returns ------- - val : str + value : str List of strings. """ with create_connection(db_file) as conn: @@ -1999,9 +2034,7 @@ async def get_filters_value(db_file, key): value = str(value) except: value = None - logging.debug( - "No specific value set for key {}.".format(key) - ) + logging.debug("No specific value set for key {}.".format(key)) return value diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index 36e5251..98de357 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -548,7 +548,7 @@ class Slixfeed(slixmpp.ClientXMPP): jid_file = jid db_file = config.get_pathname_to_database(jid_file) form = self['xep_0004'].make_form('form', 'Filters') - form['instructions'] = '🕸️ Manage filters' # 🪄️ + form['instructions'] = '🛡️ Manage filters' # 🪄️ value = await sqlite.get_filters_value(db_file, 'allow') form.add_field(var='allow', ftype='text-single', @@ -599,14 +599,10 @@ class Slixfeed(slixmpp.ClientXMPP): # keywords = await sqlite.get_filters_value(db_file, key) keywords = '' val = await config.add_to_list(val, keywords) - if await sqlite.get_filters_value(db_file, key): + if await sqlite.is_filter_key(db_file, key): await sqlite.update_filters_value(db_file, [key, val]) - else: + elif val: await sqlite.set_filters_value(db_file, [key, val]) - # result = '{}: {}'.format(key, val) - form.add_field(var=key + '_title', - ftype='fixed', - value=key.capitalize() + ' filter') form.add_field(var=key.capitalize() + ' list', ftype='text-single', value=val)