From 102d96bc7d9226ec9e7d9f4e7412f3d95ab8abb1 Mon Sep 17 00:00:00 2001 From: Vincent Kling Date: Mon, 26 Sep 2022 09:48:29 +0200 Subject: [PATCH] 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')