Replace before update listener with method in the Base class

main
Vincent Kling 2 years ago
parent 10583f57dd
commit bda404182f

@ -33,6 +33,7 @@ def dovecot_quota(ns, user_email):
user = models.User.query.get(user_email) or flask.abort(404) user = models.User.query.get(user_email) or flask.abort(404)
if ns == "storage": if ns == "storage":
user.quota_bytes_used = flask.request.get_json() user.quota_bytes_used = flask.request.get_json()
user.flag_updated_at_as_modified()
models.db.session.commit() models.db.session.commit()
return flask.jsonify(None) return flask.jsonify(None)

@ -155,6 +155,10 @@ class Base(db.Model):
self.__hashed = id(self) if primary is None else hash(primary) self.__hashed = id(self) if primary is None else hash(primary)
return self.__hashed 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 # Many-to-many association table for domain managers
managers = db.Table('manager', Base.metadata, managers = db.Table('manager', Base.metadata,
@ -983,13 +987,3 @@ class MailuConfig:
alias = MailuCollection(Alias) alias = MailuCollection(Alias)
relay = MailuCollection(Relay) relay = MailuCollection(Relay)
config = MailuCollection(Config) 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')

Loading…
Cancel
Save