Add basic versioning history support for master view
as with actual data versioning, we only support Person thus far
This commit is contained in:
parent
7340ef1f9b
commit
0b68d56ddb
7 changed files with 354 additions and 6 deletions
|
@ -30,7 +30,11 @@ import six
|
|||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
||||
import sqlalchemy_continuum as continuum
|
||||
|
||||
from rattail.db.continuum import model_transaction_query
|
||||
from rattail.util import prettify
|
||||
from rattail.time import localtime
|
||||
|
||||
import formalchemy as fa
|
||||
from pyramid import httpexceptions
|
||||
|
@ -75,6 +79,8 @@ class MasterView(View):
|
|||
grid_index = None
|
||||
use_index_links = False
|
||||
|
||||
has_versions = False
|
||||
|
||||
# ROW-RELATED ATTRS FOLLOW:
|
||||
|
||||
has_rows = False
|
||||
|
@ -323,6 +329,161 @@ class MasterView(View):
|
|||
tools=self.make_row_grid_tools(instance))
|
||||
return self.render_to_response('view', context)
|
||||
|
||||
def versions(self):
|
||||
"""
|
||||
View to list version history for an object.
|
||||
"""
|
||||
instance = self.get_instance()
|
||||
instance_title = self.get_instance_title(instance)
|
||||
grid = self.make_version_grid(instance=instance)
|
||||
|
||||
# return grid only, if partial page was requested
|
||||
if self.request.params.get('partial'):
|
||||
self.request.response.content_type = b'text/html'
|
||||
self.request.response.text = grid.render_grid()
|
||||
return self.request.response
|
||||
|
||||
return self.render_to_response('versions', {
|
||||
'instance': instance,
|
||||
'instance_title': instance_title,
|
||||
'index_title': "{}: {}".format(self.get_model_title_plural(), instance_title),
|
||||
'index_url': self.get_action_url('view', instance),
|
||||
'grid': grid,
|
||||
})
|
||||
|
||||
def make_version_grid(self, instance=None, **kwargs):
|
||||
"""
|
||||
Make and return a new (configured) version grid instance.
|
||||
"""
|
||||
if instance is None:
|
||||
instance = self.get_instance()
|
||||
factory = self.get_version_grid_factory()
|
||||
key = self.get_version_grid_key()
|
||||
data = self.get_version_data(instance)
|
||||
kwargs = self.make_version_grid_kwargs(**kwargs)
|
||||
kwargs['model_class'] = continuum.transaction_class(self.get_model_class())
|
||||
grid = factory(key, self.request, data=data, **kwargs)
|
||||
self.preconfigure_version_grid(grid)
|
||||
self.configure_version_grid(grid)
|
||||
grid.load_settings()
|
||||
return grid
|
||||
|
||||
@classmethod
|
||||
def get_version_grid_factory(cls):
|
||||
"""
|
||||
Returns the grid factory or class which is to be used when creating new
|
||||
version grid instances.
|
||||
"""
|
||||
return getattr(cls, 'version_grid_factory', AlchemyGrid)
|
||||
|
||||
@classmethod
|
||||
def get_version_grid_key(cls):
|
||||
"""
|
||||
Returns the unique key to be used for the version grid, for caching
|
||||
sort/filter options etc.
|
||||
"""
|
||||
if hasattr(cls, 'version_grid_key'):
|
||||
return cls.version_grid_key
|
||||
return '{}.history'.format(cls.get_route_prefix())
|
||||
|
||||
def get_version_data(self, instance):
|
||||
"""
|
||||
Generate the base data set for the version grid.
|
||||
"""
|
||||
model_class = self.get_model_class()
|
||||
transaction_class = continuum.transaction_class(model_class)
|
||||
query = model_transaction_query(self.Session(), instance.uuid, model_class)
|
||||
return query.order_by(transaction_class.issued_at.desc())
|
||||
|
||||
def make_version_grid_kwargs(self, **kwargs):
|
||||
"""
|
||||
Return a dictionary of kwargs to be passed to the factory when
|
||||
constructing a new version grid.
|
||||
"""
|
||||
defaults = {
|
||||
'width': 'full',
|
||||
'pageable': True,
|
||||
}
|
||||
if 'main_actions' not in kwargs:
|
||||
route = '{}.version'.format(self.get_route_prefix())
|
||||
instance = kwargs.get('instance') or self.get_instance()
|
||||
url = lambda txn, i: self.request.route_url(route, uuid=instance.uuid, txnid=txn.id)
|
||||
defaults['main_actions'] = [
|
||||
self.make_action('view', icon='zoomin', url=url),
|
||||
]
|
||||
defaults.update(kwargs)
|
||||
return defaults
|
||||
|
||||
def preconfigure_version_grid(self, g):
|
||||
g.issued_at.set(label="Changed")
|
||||
g.user.set(label="Changed by")
|
||||
g.remote_addr.set(label="IP Address")
|
||||
g.append(fa.Field('comment', value=lambda txn: txn.meta.get('comment')))
|
||||
g.default_sortkey = 'issued_at'
|
||||
g.default_sortdir = 'desc'
|
||||
|
||||
def configure_version_grid(self, g):
|
||||
"""
|
||||
Configure the version grid, customizing as necessary.
|
||||
"""
|
||||
g.configure(include=[
|
||||
g.issued_at,
|
||||
g.user,
|
||||
g.remote_addr,
|
||||
g.comment,
|
||||
], readonly=True)
|
||||
|
||||
def view_version(self):
|
||||
"""
|
||||
View showing diff details of a particular object version.
|
||||
"""
|
||||
instance = self.get_instance()
|
||||
model_class = self.get_model_class()
|
||||
Transaction = continuum.transaction_class(model_class)
|
||||
transactions = model_transaction_query(self.Session(), instance.uuid, model_class)
|
||||
transaction_id = self.request.matchdict['txnid']
|
||||
transaction = transactions.filter(Transaction.id == transaction_id).first()
|
||||
if not transaction:
|
||||
return self.notfound()
|
||||
older = transactions.filter(Transaction.issued_at <= transaction.issued_at)\
|
||||
.filter(Transaction.id != transaction_id)\
|
||||
.order_by(Transaction.issued_at.desc())\
|
||||
.first()
|
||||
newer = transactions.filter(Transaction.issued_at >= transaction.issued_at)\
|
||||
.filter(Transaction.id != transaction_id)\
|
||||
.order_by(Transaction.issued_at)\
|
||||
.first()
|
||||
|
||||
instance_title = self.get_instance_title(instance)
|
||||
return self.render_to_response('view_version', {
|
||||
'instance': instance,
|
||||
'instance_title': instance_title,
|
||||
'index_title': "{}: {} (History)".format(self.get_model_title_plural(), instance_title),
|
||||
'index_url': self.get_action_url('versions', instance),
|
||||
'transaction': transaction,
|
||||
'changed': localtime(self.rattail_config, transaction.issued_at, from_utc=True),
|
||||
'versions': self.get_relevant_versions(transaction, instance),
|
||||
'previous_transaction': older,
|
||||
'next_transaction': newer,
|
||||
'fields_for_version': self.fields_for_version,
|
||||
})
|
||||
|
||||
def fields_for_version(self, version):
|
||||
mapper = orm.class_mapper(version.__class__)
|
||||
fields = sorted(mapper.columns.keys())
|
||||
fields.remove('uuid')
|
||||
fields.remove('transaction_id')
|
||||
fields.remove('end_transaction_id')
|
||||
fields.remove('operation_type')
|
||||
return fields
|
||||
|
||||
def get_relevant_versions(self, transaction, instance):
|
||||
version_class = self.get_model_version_class()
|
||||
query = self.Session.query(version_class)\
|
||||
.filter(version_class.transaction == transaction)\
|
||||
.filter(version_class.uuid == instance.uuid)
|
||||
return query.all()
|
||||
|
||||
def mobile_view(self):
|
||||
"""
|
||||
Mobile view for displaying a single object's details
|
||||
|
@ -783,6 +944,13 @@ class MasterView(View):
|
|||
raise NotImplementedError("You must define the `model_class` for: {}".format(cls))
|
||||
return getattr(cls, 'model_class', None)
|
||||
|
||||
@classmethod
|
||||
def get_model_version_class(cls):
|
||||
"""
|
||||
Returns the version class for the master model class.
|
||||
"""
|
||||
return continuum.version_class(cls.get_model_class())
|
||||
|
||||
@classmethod
|
||||
def get_normalized_model_name(cls):
|
||||
"""
|
||||
|
@ -1562,6 +1730,7 @@ class MasterView(View):
|
|||
"""
|
||||
Provide default configuration for a master view.
|
||||
"""
|
||||
rattail_config = config.registry.settings.get('rattail_config')
|
||||
route_prefix = cls.get_route_prefix()
|
||||
url_prefix = cls.get_url_prefix()
|
||||
permission_prefix = cls.get_permission_prefix()
|
||||
|
@ -1638,6 +1807,17 @@ class MasterView(View):
|
|||
config.add_view(cls, attr='mobile_view', route_name='mobile.{}.view'.format(route_prefix),
|
||||
permission='{}.view'.format(permission_prefix))
|
||||
|
||||
# version history
|
||||
if cls.has_versions and rattail_config.versioning_enabled():
|
||||
config.add_tailbone_permission(permission_prefix, '{}.versions'.format(permission_prefix),
|
||||
"View version history for {}".format(model_title))
|
||||
config.add_route('{}.versions'.format(route_prefix), '{}/{{{}}}/versions/'.format(url_prefix, model_key))
|
||||
config.add_view(cls, attr='versions', route_name='{}.versions'.format(route_prefix),
|
||||
permission='{}.versions'.format(permission_prefix))
|
||||
config.add_route('{}.version'.format(route_prefix), '{}/{{{}}}/versions/{{txnid}}'.format(url_prefix, model_key))
|
||||
config.add_view(cls, attr='view_version', route_name='{}.version'.format(route_prefix),
|
||||
permission='{}.versions'.format(permission_prefix))
|
||||
|
||||
# download
|
||||
if cls.downloadable:
|
||||
config.add_route('{}.download'.format(route_prefix), '{}/{{{}}}/download'.format(url_prefix, model_key))
|
||||
|
|
|
@ -61,8 +61,9 @@ class PeopleView(MasterView):
|
|||
model_class = model.Person
|
||||
model_title_plural = "People"
|
||||
route_prefix = 'people'
|
||||
has_versions = True
|
||||
|
||||
def configure_grid(self, g):
|
||||
def _preconfigure_grid(self, g):
|
||||
g.joiners['email'] = lambda q: q.outerjoin(model.PersonEmailAddress, sa.and_(
|
||||
model.PersonEmailAddress.parent_uuid == model.Person.uuid,
|
||||
model.PersonEmailAddress.preference == 1))
|
||||
|
@ -89,6 +90,7 @@ class PeopleView(MasterView):
|
|||
|
||||
g.default_sortkey = 'display_name'
|
||||
|
||||
def configure_grid(self, g):
|
||||
g.configure(
|
||||
include=[
|
||||
g.display_name.label("Full Name"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue