From: Michael Tremer Date: Fri, 5 Dec 2025 17:25:25 +0000 (+0000) Subject: dnsbl: Create a command to update a list X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eaa3bb543e68acf4013f4956d8d02cee3b2bce7e;p=dbl.git dnsbl: Create a command to update a list Signed-off-by: Michael Tremer --- diff --git a/src/database.sql b/src/database.sql index 6e18ae4..1c80838 100644 --- a/src/database.sql +++ b/src/database.sql @@ -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 diff --git a/src/dnsbl/database.py b/src/dnsbl/database.py index b97eb89..2399f10 100644 --- a/src/dnsbl/database.py +++ b/src/dnsbl/database.py @@ -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 diff --git a/src/dnsbl/lists.py b/src/dnsbl/lists.py index 479ec87..38cab1e 100644 --- a/src/dnsbl/lists.py +++ b/src/dnsbl/lists.py @@ -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() diff --git a/src/dnsbl/sources.py b/src/dnsbl/sources.py index 8465b47..6577060 100644 --- a/src/dnsbl/sources.py +++ b/src/dnsbl/sources.py @@ -19,10 +19,14 @@ ############################################################################### 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)) diff --git a/src/scripts/dnsbl.in b/src/scripts/dnsbl.in index dd7b7eb..b779b84 100644 --- a/src/scripts/dnsbl.in +++ b/src/scripts/dnsbl.in @@ -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