mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
remove history-meta for templates, replace with hand-made history table
history-meta's dynamic magic is insufficient for templates, where we need to be able to refer to the specific history table to take advantage of sqlalchemy's relationship management (2/3rds of an ORM). So replace it with a custom made version table. Had to change the version decorator slightly for this
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import itertools
|
||||
from functools import wraps
|
||||
from app.history_meta import versioned_objects, create_history
|
||||
from functools import wraps, partial
|
||||
|
||||
from app.history_meta import create_history
|
||||
|
||||
|
||||
def transactional(func):
|
||||
@@ -19,14 +20,16 @@ def transactional(func):
|
||||
return commit_or_rollback
|
||||
|
||||
|
||||
def version_class(model_class):
|
||||
def version_class(model_class, history_cls=None):
|
||||
create_hist = partial(create_history, history_cls=history_cls)
|
||||
|
||||
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))
|
||||
history_objects = [create_hist(obj) for obj in
|
||||
itertools.chain(db.session.new, db.session.dirty)
|
||||
if isinstance(obj, model_class)]
|
||||
for h_obj in history_objects:
|
||||
db.session.add(h_obj)
|
||||
|
||||
@@ -16,6 +16,7 @@ from app.models import (
|
||||
VerifyCode,
|
||||
ApiKey,
|
||||
Template,
|
||||
TemplateHistory,
|
||||
Job,
|
||||
NotificationHistory,
|
||||
Notification,
|
||||
@@ -122,7 +123,7 @@ def delete_service_and_all_associated_db_objects(service):
|
||||
_delete_commit(Notification.query.filter_by(service=service))
|
||||
_delete_commit(Job.query.filter_by(service=service))
|
||||
_delete_commit(Template.query.filter_by(service=service))
|
||||
_delete_commit(Template.get_history_model().query.filter_by(service_id=service.id))
|
||||
_delete_commit(TemplateHistory.query.filter_by(service_id=service.id))
|
||||
|
||||
verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users]))
|
||||
list(map(db.session.delete, verify_codes))
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import uuid
|
||||
from app import db
|
||||
from app.models import (Template, Service)
|
||||
|
||||
from sqlalchemy import (asc, desc)
|
||||
|
||||
from app import db
|
||||
from app.models import (Template, TemplateHistory)
|
||||
from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
@@ -10,7 +11,7 @@ from app.dao.dao_utils import (
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Template)
|
||||
@version_class(Template, TemplateHistory)
|
||||
def dao_create_template(template):
|
||||
template.id = uuid.uuid4() # must be set now so version history model can use same id
|
||||
template.archived = False
|
||||
@@ -18,14 +19,14 @@ def dao_create_template(template):
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Template)
|
||||
@version_class(Template, TemplateHistory)
|
||||
def dao_update_template(template):
|
||||
db.session.add(template)
|
||||
|
||||
|
||||
def dao_get_template_by_id_and_service_id(template_id, service_id, version=None):
|
||||
if version is not None:
|
||||
return Template.get_history_model().query.filter_by(
|
||||
return TemplateHistory.query.filter_by(
|
||||
id=template_id,
|
||||
service_id=service_id,
|
||||
version=version).one()
|
||||
@@ -34,7 +35,7 @@ def dao_get_template_by_id_and_service_id(template_id, service_id, version=None)
|
||||
|
||||
def dao_get_template_by_id(template_id, version=None):
|
||||
if version is not None:
|
||||
return Template.get_history_model().query.filter_by(
|
||||
return TemplateHistory.query.filter_by(
|
||||
id=template_id,
|
||||
version=version).one()
|
||||
return Template.query.filter_by(id=template_id).one()
|
||||
@@ -50,6 +51,8 @@ def dao_get_all_templates_for_service(service_id):
|
||||
|
||||
|
||||
def dao_get_template_versions(service_id, template_id):
|
||||
history_model = Template.get_history_model()
|
||||
return history_model.query.filter_by(service_id=service_id, id=template_id).order_by(
|
||||
desc(history_model.version)).all()
|
||||
return TemplateHistory.query.filter_by(
|
||||
service_id=service_id, id=template_id
|
||||
).order_by(
|
||||
desc(TemplateHistory.version)
|
||||
).all()
|
||||
|
||||
Reference in New Issue
Block a user