From 102d96bc7d9226ec9e7d9f4e7412f3d95ab8abb1 Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Mon, 26 Sep 2022 09:48:29 +0200 Subject: [PATCH 1/5] Implement event lister to keep updated_at unchanged on quota_bytes_used updates --- core/admin/mailu/models.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/admin/mailu/models.py b/core/admin/mailu/models.py index 70a9528f..03434fee 100644 --- a/core/admin/mailu/models.py +++ b/core/admin/mailu/models.py @@ -25,6 +25,7 @@ from flask import current_app as app from sqlalchemy.ext import declarative from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.inspection import inspect +from sqlalchemy.orm.attributes import flag_modified from werkzeug.utils import cached_property from mailu import dkim, utils @@ -982,3 +983,13 @@ class MailuConfig: alias = MailuCollection(Alias) relay = MailuCollection(Relay) config = MailuCollection(Config) + + +@db.event.listens_for(User, 'before_update') +def receive_before_update(mapper, connection, target): + """ Mark updated_at as modified, but keep the old date when updating the quota_bytes_used + """ + insp = db.inspect(target) + quota_bytes_used_changed, _, _ = insp.attrs.quota_bytes_used.history + if quota_bytes_used_changed: + flag_modified(target, 'updated_at') From 10583f57dd3ce851e3c4af9a5e87a1e1887d2d5a Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Mon, 26 Sep 2022 09:53:10 +0200 Subject: [PATCH 2/5] Add newsfragment --- towncrier/newsfragments/1363.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 towncrier/newsfragments/1363.bugfix diff --git a/towncrier/newsfragments/1363.bugfix b/towncrier/newsfragments/1363.bugfix new file mode 100644 index 00000000..edb198d7 --- /dev/null +++ b/towncrier/newsfragments/1363.bugfix @@ -0,0 +1 @@ +Do not update the updated_at field of the User model when quota_bytes_used is updated \ No newline at end of file From bda404182f874b53d7f021471bd30f2c7c128469 Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Fri, 7 Oct 2022 11:17:46 +0200 Subject: [PATCH 3/5] Replace before update listener with method in the Base class --- core/admin/mailu/internal/views/dovecot.py | 1 + core/admin/mailu/models.py | 14 ++++---------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/core/admin/mailu/internal/views/dovecot.py b/core/admin/mailu/internal/views/dovecot.py index 2f5f68a4..e949e2eb 100644 --- a/core/admin/mailu/internal/views/dovecot.py +++ b/core/admin/mailu/internal/views/dovecot.py @@ -33,6 +33,7 @@ def dovecot_quota(ns, user_email): user = models.User.query.get(user_email) or flask.abort(404) if ns == "storage": user.quota_bytes_used = flask.request.get_json() + user.flag_updated_at_as_modified() models.db.session.commit() return flask.jsonify(None) diff --git a/core/admin/mailu/models.py b/core/admin/mailu/models.py index 03434fee..edcdcf27 100644 --- a/core/admin/mailu/models.py +++ b/core/admin/mailu/models.py @@ -155,6 +155,10 @@ class Base(db.Model): self.__hashed = id(self) if primary is None else hash(primary) return self.__hashed + def flag_updated_at_as_modified(self): + """ Mark updated_at as modified, but keep the old date when updating the model""" + flag_modified(self, 'updated_at') + # Many-to-many association table for domain managers managers = db.Table('manager', Base.metadata, @@ -983,13 +987,3 @@ class MailuConfig: alias = MailuCollection(Alias) relay = MailuCollection(Relay) config = MailuCollection(Config) - - -@db.event.listens_for(User, 'before_update') -def receive_before_update(mapper, connection, target): - """ Mark updated_at as modified, but keep the old date when updating the quota_bytes_used - """ - insp = db.inspect(target) - quota_bytes_used_changed, _, _ = insp.attrs.quota_bytes_used.history - if quota_bytes_used_changed: - flag_modified(target, 'updated_at') From 6b785abb01913092bc134898982a2700b9657e72 Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Fri, 28 Oct 2022 10:05:47 +0200 Subject: [PATCH 4/5] Rename flag_updated_at_as_modified to dont_change_updated_at --- core/admin/mailu/internal/views/dovecot.py | 2 +- core/admin/mailu/models.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/admin/mailu/internal/views/dovecot.py b/core/admin/mailu/internal/views/dovecot.py index e949e2eb..0d56950b 100644 --- a/core/admin/mailu/internal/views/dovecot.py +++ b/core/admin/mailu/internal/views/dovecot.py @@ -33,7 +33,7 @@ def dovecot_quota(ns, user_email): user = models.User.query.get(user_email) or flask.abort(404) if ns == "storage": user.quota_bytes_used = flask.request.get_json() - user.flag_updated_at_as_modified() + user.dont_change_updated_at() models.db.session.commit() return flask.jsonify(None) diff --git a/core/admin/mailu/models.py b/core/admin/mailu/models.py index edcdcf27..0071bc77 100644 --- a/core/admin/mailu/models.py +++ b/core/admin/mailu/models.py @@ -155,7 +155,7 @@ class Base(db.Model): self.__hashed = id(self) if primary is None else hash(primary) return self.__hashed - def flag_updated_at_as_modified(self): + def dont_change_updated_at(self): """ Mark updated_at as modified, but keep the old date when updating the model""" flag_modified(self, 'updated_at') From 6363acf30a063551976a1d6b3372d0f39cf208ee Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Fri, 28 Oct 2022 10:19:30 +0200 Subject: [PATCH 5/5] Add dont_change_updated_at to fetch_done --- core/admin/mailu/internal/views/fetch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/core/admin/mailu/internal/views/fetch.py b/core/admin/mailu/internal/views/fetch.py index ccd3a159..1945b9c7 100644 --- a/core/admin/mailu/internal/views/fetch.py +++ b/core/admin/mailu/internal/views/fetch.py @@ -27,6 +27,7 @@ def fetch_done(fetch_id): fetch = models.Fetch.query.get(fetch_id) or flask.abort(404) fetch.last_check = datetime.datetime.now() fetch.error_message = str(flask.request.get_json()) + fetch.dont_change_updated_at() models.db.session.add(fetch) models.db.session.commit() return ""