Skip to content

Commit

Permalink
ext: add user to db history before each flush
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba committed Nov 7, 2023
1 parent 05657e0 commit 3bef6f4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions invenio_accounts/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from flask_security import Security
from invenio_db import db
from passlib.registry import register_crypt_handler
from sqlalchemy import event
from werkzeug.utils import cached_property

from invenio_accounts.forms import (
Expand All @@ -27,6 +28,8 @@
register_form_factory,
send_confirmation_form_factory,
)
from invenio_accounts.models import User
from invenio_accounts.proxies import current_db_change_history

from . import config
from .datastore import SessionAwareSQLAlchemyUserDatastore
Expand Down Expand Up @@ -330,3 +333,11 @@ def make_session_permanent(self, app):
@app.before_request
def make_session_permanent():
session.permanent = True


# Discuss: Logically, this should happen after_flush to avoid cases of rollbacks/failure.
@event.listens_for(db.session, "before_flush")
def update_db_history_user_updates(session, flush_context, instances):
for obj in session.dirty:
if isinstance(obj, User):
current_db_change_history.add_updated_user(id(db.session), obj.id)

0 comments on commit 3bef6f4

Please sign in to comment.