From bda404182f874b53d7f021471bd30f2c7c128469 Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Fri, 7 Oct 2022 11:17:46 +0200 Subject: [PATCH] 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')