mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-16 02:02:13 -05:00
revoking an api key the service it associated with was of course added to db.session.dirty. That resulted in an updated version of service being added to the service history table that showed no visible difference from that record immediately precending it as the change was to another table, namely the api_key table. A new api key or revoked api key was correctly added to api_key and api_key_history tables. However I think an 'unchanged' service history record may be a bit confusing as you'd need to correlate with api_keys to work out what the change was. I think it's best to just record the new/revoked api_key and not create another version of the service. This pr wraps the exisiting versioned decorator with one that take a class which you are interested in versioning. Using the new decorator you only get a new version and history record for the class you pass to outer decorator. If the exising behaviour is acceptable to the powers that be then by all means ignore/close this pr.
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
import itertools
|
|
from functools import wraps
|
|
from app.history_meta import versioned_objects, create_history
|
|
|
|
|
|
def transactional(func):
|
|
@wraps(func)
|
|
def commit_or_rollback(*args, **kwargs):
|
|
from flask import current_app
|
|
from app import db
|
|
try:
|
|
func(*args, **kwargs)
|
|
db.session.commit()
|
|
except Exception as e:
|
|
current_app.logger.error(e)
|
|
db.session.rollback()
|
|
raise
|
|
return commit_or_rollback
|
|
|
|
|
|
def version_class(model_class):
|
|
def versioned(func):
|
|
@wraps(func)
|
|
def record_version(*args, **kwargs):
|
|
from app import db
|
|
func(*args, **kwargs)
|
|
history_objects = [create_history(obj) for obj in
|
|
versioned_objects(itertools.chain(db.session.new, db.session.dirty))
|
|
if isinstance(obj, model_class)]
|
|
for h_obj in history_objects:
|
|
db.session.add(h_obj)
|
|
return record_version
|
|
return versioned
|