]> git.ipfire.org Git - dbl.git/commitdiff
dnsbl: Create a command to update a list
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Dec 2025 17:25:25 +0000 (17:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Dec 2025 17:25:25 +0000 (17:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.sql
src/dnsbl/database.py
src/dnsbl/lists.py
src/dnsbl/sources.py
src/scripts/dnsbl.in

index 6e18ae4c5ed5830eb6d633a316a972c2fc36439d..1c8083882c919b20a7e7c3d9b1252a23781f7723 100644 (file)
@@ -2,7 +2,7 @@
 -- PostgreSQL database dump
 --
 
-\restrict bCYCh5sZAo3Eaa8bUasedCV2H1jNs1GvAasBDDiQVe29BG0iu45omWev4EJu1ZQ
+\restrict MdBTHRTrPo5KwL7rBdfmjwUQ78IadFTz2sk6xzAeY0VTkdkQ2ifc9gAhyzISo3V
 
 -- Dumped from database version 17.6 (Debian 17.6-0+deb13u1)
 -- Dumped by pg_dump version 17.6 (Debian 17.6-0+deb13u1)
@@ -35,7 +35,8 @@ CREATE TABLE public.lists (
     created_by text NOT NULL,
     deleted_at timestamp with time zone,
     deleted_by text,
-    license text NOT NULL
+    license text NOT NULL,
+    updated_at timestamp with time zone
 );
 
 
@@ -152,5 +153,5 @@ ALTER TABLE ONLY public.sources
 -- PostgreSQL database dump complete
 --
 
-\unrestrict bCYCh5sZAo3Eaa8bUasedCV2H1jNs1GvAasBDDiQVe29BG0iu45omWev4EJu1ZQ
+\unrestrict MdBTHRTrPo5KwL7rBdfmjwUQ78IadFTz2sk6xzAeY0VTkdkQ2ifc9gAhyzISo3V
 
index b97eb89461f4f97d916d74dbe3355567587a9342..2399f10754c0ac16c62d06637f40bb2b79988ee5 100644 (file)
@@ -80,6 +80,20 @@ class Database(object):
                if exception is None:
                        session.commit()
 
+       def transaction(self):
+               """
+                       Opens a new transaction
+               """
+               # Fetch our session
+               session = self.session()
+
+               # If we are already in a transaction, begin a nested one
+               if session.in_transaction():
+                       return session.begin_nested()
+
+               # Otherwise begin the first transaction of the session
+               return session.begin()
+
        def execute(self, stmt):
                """
                        Executes a statement and returns a result object
index 479ec87cb61217ede3be33371134e82ff761a193..38cab1ee40165424047f027bf003b6d5d3bbefd3 100644 (file)
@@ -96,6 +96,9 @@ class Lists(object):
 class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
        __tablename__ = "lists"
 
+       def __str__(self):
+               return self.name
+
        # ID
        id : int = sqlmodel.Field(primary_key=True)
 
@@ -124,3 +127,20 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
 
        # Sources
        sources : typing.List["Source"] = sqlmodel.Relationship(back_populates="list")
+
+       # Updated At
+       updated_at : datetime.datetime | None
+
+       # Update!
+
+       def update(self):
+               """
+                       Updates the list
+               """
+               with self.backend.db.transaction():
+                       # Update the timestamp
+                       self.updated_at = sqlmodel.func.current_timestamp()
+
+                       # Update all sources
+                       for source in self.sources:
+                               source.update()
index 8465b47d65e0b8910f4be49daa6ce40ab15a828d..6577060e1d75c3540a666789d274172bc92b13df 100644 (file)
 ###############################################################################
 
 import datetime
+import logging
 import sqlmodel
 
 from . import database
 
+# Setup logging
+log = logging.getLogger(__name__)
+
 class Sources(object):
        def __init__(self, backend):
                self.backend = backend
@@ -65,6 +69,9 @@ class Sources(object):
 class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
        __tablename__ = "sources"
 
+       def __str__(self):
+               return self.name
+
        # ID
        id : int = sqlmodel.Field(primary_key=True)
 
@@ -96,3 +103,9 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
 
        # List
        list : "List" = sqlmodel.Relationship(back_populates="sources")
+
+       def update(self):
+               """
+                       Updates this source.
+               """
+               log.debug("%s: Updating source %s" % (self.list, self))
index dd7b7eb6a96e6a5a6656d9be271dec86e5c07103..b779b8418c69afd9a7d2d7017c792df6518b5521 100644 (file)
@@ -71,6 +71,11 @@ class CLI(object):
                list_show.add_argument("list", help=_("The name of the list"))
                list_show.set_defaults(func=self.__list_show)
 
+               # list-update
+               list_update = subparsers.add_parser("list-update", help=_("Updates a list"))
+               list_update.add_argument("list", help=_("The name of the list"))
+               list_update.set_defaults(func=self.__list_update)
+
                # list-add-source
                list_add_source = subparsers.add_parser("list-add-source",
                                help=_("Creates a new source to a list"))
@@ -194,6 +199,16 @@ class CLI(object):
                # Print the sources
                self.console.print(table)
 
+       def __list_update(self, backend, args):
+               """
+                       Updates a single list
+               """
+               # Fetch the list
+               list = backend.lists.get_by_slug(args.list)
+
+               # Update!
+               list.update()
+
        def __list_add_source(self, backend, args):
                """
                        Adds a new source to a list