Remove all "old-style" (aka. version 1) grids
This commit is contained in:
parent
0befc46070
commit
62fa0f9fcb
19 changed files with 32 additions and 1604 deletions
|
@ -1,8 +1,8 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# -*- coding: utf-8; -*-
|
||||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2016 Lance Edgar
|
||||
# Copyright © 2010-2017 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
@ -29,5 +29,5 @@ from __future__ import unicode_literals, absolute_import
|
|||
from .core import BatchMasterView, FileBatchMasterView
|
||||
|
||||
# TODO: deprecate / remove this
|
||||
from .core import (BaseGrid, BatchGrid, FileBatchGrid, BaseCrud, BatchCrud, FileBatchCrud,
|
||||
StatusRenderer, BatchRowGrid, ProductBatchRowGrid, BatchRowCrud, defaults)
|
||||
from .core import (BaseCrud, BatchCrud, FileBatchCrud,
|
||||
StatusRenderer, BatchRowCrud, defaults)
|
||||
|
|
|
@ -51,9 +51,8 @@ from webhelpers.html import HTML, tags
|
|||
|
||||
from tailbone import forms, newgrids as grids
|
||||
from tailbone.db import Session
|
||||
from tailbone.views import MasterView, SearchableAlchemyGridView, CrudView
|
||||
from tailbone.views import MasterView, CrudView
|
||||
from tailbone.forms.renderers.batch import FileFieldRenderer
|
||||
from tailbone.grids.search import BooleanSearchFilter, EnumSearchFilter
|
||||
from tailbone.progress import SessionProgress
|
||||
|
||||
|
||||
|
@ -1137,264 +1136,6 @@ class FileBatchMasterView(BatchMasterView):
|
|||
"Download existing {} data file".format(model_title))
|
||||
|
||||
|
||||
class BaseGrid(SearchableAlchemyGridView):
|
||||
"""
|
||||
Base view for batch and batch row grid views. You should not derive from
|
||||
this class, but :class:`BatchGrid` or :class:`BatchRowGrid` instead.
|
||||
"""
|
||||
|
||||
@property
|
||||
def config_prefix(self):
|
||||
"""
|
||||
Config prefix for the grid view. This is used to keep track of current
|
||||
filtering and sorting, within the user's session. Derived classes may
|
||||
override this.
|
||||
"""
|
||||
return self.mapped_class.__name__.lower()
|
||||
|
||||
@property
|
||||
def permission_prefix(self):
|
||||
"""
|
||||
Permission prefix for the grid view. This is used to automatically
|
||||
protect certain views common to all batches. Derived classes can
|
||||
override this.
|
||||
"""
|
||||
return self.route_prefix
|
||||
|
||||
def join_map_extras(self):
|
||||
"""
|
||||
Derived classes can override this. The value returned will be used to
|
||||
supplement the default join map.
|
||||
"""
|
||||
return {}
|
||||
|
||||
def filter_map_extras(self):
|
||||
"""
|
||||
Derived classes can override this. The value returned will be used to
|
||||
supplement the default filter map.
|
||||
"""
|
||||
return {}
|
||||
|
||||
def make_filter_map(self, **kwargs):
|
||||
"""
|
||||
Make a filter map by combining kwargs from the base class, with extras
|
||||
supplied by a derived class.
|
||||
"""
|
||||
extras = self.filter_map_extras()
|
||||
exact = extras.pop('exact', None)
|
||||
if exact:
|
||||
kwargs.setdefault('exact', []).extend(exact)
|
||||
ilike = extras.pop('ilike', None)
|
||||
if ilike:
|
||||
kwargs.setdefault('ilike', []).extend(ilike)
|
||||
kwargs.update(extras)
|
||||
return super(BaseGrid, self).make_filter_map(**kwargs)
|
||||
|
||||
def filter_config_extras(self):
|
||||
"""
|
||||
Derived classes can override this. The value returned will be used to
|
||||
supplement the default filter config.
|
||||
"""
|
||||
return {}
|
||||
|
||||
def sort_map_extras(self):
|
||||
"""
|
||||
Derived classes can override this. The value returned will be used to
|
||||
supplement the default sort map.
|
||||
"""
|
||||
return {}
|
||||
|
||||
def _configure_grid(self, grid):
|
||||
"""
|
||||
Internal method for configuring the grid. This is meant only for base
|
||||
classes; derived classes should not need to override it.
|
||||
"""
|
||||
|
||||
def configure_grid(self, grid):
|
||||
"""
|
||||
Derived classes can override this. Customizes a grid which has already
|
||||
been created with defaults by the base class.
|
||||
"""
|
||||
|
||||
|
||||
class BatchGrid(BaseGrid):
|
||||
"""
|
||||
Base grid view for batches, which can be filtered and sorted.
|
||||
"""
|
||||
|
||||
@property
|
||||
def batch_class(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def mapped_class(self):
|
||||
return self.batch_class
|
||||
|
||||
@property
|
||||
def batch_display(self):
|
||||
"""
|
||||
Singular display text for the batch type, e.g. "Vendor Invoice".
|
||||
Override this as necessary.
|
||||
"""
|
||||
return self.batch_class.__name__
|
||||
|
||||
@property
|
||||
def batch_display_plural(self):
|
||||
"""
|
||||
Plural display text for the batch type, e.g. "Vendor Invoices".
|
||||
Override this as necessary.
|
||||
"""
|
||||
return "{0}s".format(self.batch_display)
|
||||
|
||||
def join_map(self):
|
||||
"""
|
||||
Provides the default join map for batch grid views. Derived classes
|
||||
should *not* override this, but :meth:`join_map_extras()` instead.
|
||||
"""
|
||||
map_ = {
|
||||
'created_by':
|
||||
lambda q: q.join(model.User, model.User.uuid == self.batch_class.created_by_uuid),
|
||||
'executed_by':
|
||||
lambda q: q.outerjoin(model.User, model.User.uuid == self.batch_class.executed_by_uuid),
|
||||
}
|
||||
map_.update(self.join_map_extras())
|
||||
return map_
|
||||
|
||||
def filter_map(self):
|
||||
"""
|
||||
Provides the default filter map for batch grid views. Derived classes
|
||||
should *not* override this, but :meth:`filter_map_extras()` instead.
|
||||
"""
|
||||
|
||||
def executed_is(q, v):
|
||||
if v == 'True':
|
||||
return q.filter(self.batch_class.executed != None)
|
||||
else:
|
||||
return q.filter(self.batch_class.executed == None)
|
||||
|
||||
def executed_nt(q, v):
|
||||
if v == 'True':
|
||||
return q.filter(self.batch_class.executed == None)
|
||||
else:
|
||||
return q.filter(self.batch_class.executed != None)
|
||||
|
||||
return self.make_filter_map(
|
||||
executed={'is': executed_is, 'nt': executed_nt})
|
||||
|
||||
def filter_config(self):
|
||||
"""
|
||||
Provides the default filter config for batch grid views. Derived
|
||||
classes should *not* override this, but :meth:`filter_config_extras()`
|
||||
instead.
|
||||
"""
|
||||
defaults = self.filter_config_extras()
|
||||
config = self.make_filter_config(
|
||||
filter_factory_executed=BooleanSearchFilter,
|
||||
filter_type_executed='is',
|
||||
executed=False,
|
||||
include_filter_executed=True)
|
||||
defaults.update(config)
|
||||
return defaults
|
||||
|
||||
def sort_map(self):
|
||||
"""
|
||||
Provides the default sort map for batch grid views. Derived classes
|
||||
should *not* override this, but :meth:`sort_map_extras()` instead.
|
||||
"""
|
||||
map_ = self.make_sort_map(
|
||||
created_by=self.sorter(model.User.username),
|
||||
executed_by=self.sorter(model.User.username))
|
||||
map_.update(self.sort_map_extras())
|
||||
return map_
|
||||
|
||||
def sort_config(self):
|
||||
"""
|
||||
Provides the default sort config for batch grid views. Derived classes
|
||||
may override this.
|
||||
"""
|
||||
return self.make_sort_config(sort='created', dir='desc')
|
||||
|
||||
def grid(self):
|
||||
"""
|
||||
Creates the grid for the view. Derived classes should *not* override
|
||||
this, but :meth:`configure_grid()` instead.
|
||||
"""
|
||||
g = self.make_grid()
|
||||
g.created_by.set(renderer=forms.renderers.UserFieldRenderer)
|
||||
g.cognized_by.set(renderer=forms.renderers.UserFieldRenderer)
|
||||
g.executed_by.set(renderer=forms.renderers.UserFieldRenderer)
|
||||
self._configure_grid(g)
|
||||
self.configure_grid(g)
|
||||
if self.request.has_perm('{0}.view'.format(self.permission_prefix)):
|
||||
g.viewable = True
|
||||
g.view_route_name = '{0}.view'.format(self.route_prefix)
|
||||
if self.request.has_perm('{0}.edit'.format(self.permission_prefix)):
|
||||
g.editable = True
|
||||
g.edit_route_name = '{0}.edit'.format(self.route_prefix)
|
||||
if self.request.has_perm('{0}.delete'.format(self.permission_prefix)):
|
||||
g.deletable = True
|
||||
g.delete_route_name = '{0}.delete'.format(self.route_prefix)
|
||||
return g
|
||||
|
||||
def _configure_grid(self, grid):
|
||||
grid.created_by.set(label="Created by")
|
||||
grid.executed_by.set(label="Executed by")
|
||||
|
||||
def configure_grid(self, grid):
|
||||
"""
|
||||
Derived classes can override this. Customizes a grid which has already
|
||||
been created with defaults by the base class.
|
||||
"""
|
||||
g = grid
|
||||
g.configure(
|
||||
include=[
|
||||
g.created,
|
||||
g.created_by,
|
||||
g.executed,
|
||||
g.executed_by,
|
||||
],
|
||||
readonly=True)
|
||||
|
||||
def render_kwargs(self):
|
||||
"""
|
||||
Add some things to the template context: batch type display name, route
|
||||
and permission prefixes.
|
||||
"""
|
||||
return {
|
||||
'batch_display': self.batch_display,
|
||||
'batch_display_plural': self.batch_display_plural,
|
||||
'route_prefix': self.route_prefix,
|
||||
'permission_prefix': self.permission_prefix,
|
||||
}
|
||||
|
||||
|
||||
class FileBatchGrid(BatchGrid):
|
||||
"""
|
||||
Base grid view for batches, which involve primarily a file upload.
|
||||
"""
|
||||
|
||||
def _configure_grid(self, g):
|
||||
super(FileBatchGrid, self)._configure_grid(g)
|
||||
g.created.set(label="Uploaded")
|
||||
g.created_by.set(label="Uploaded by")
|
||||
|
||||
def configure_grid(self, grid):
|
||||
"""
|
||||
Derived classes can override this. Customizes a grid which has already
|
||||
been created with defaults by the base class.
|
||||
"""
|
||||
g = grid
|
||||
g.configure(
|
||||
include=[
|
||||
g.created,
|
||||
g.created_by,
|
||||
g.filename,
|
||||
g.executed,
|
||||
g.executed_by,
|
||||
],
|
||||
readonly=True)
|
||||
|
||||
|
||||
class BaseCrud(CrudView):
|
||||
"""
|
||||
Base CRUD view for batches and batch rows.
|
||||
|
@ -2000,173 +1741,6 @@ class StatusRenderer(forms.renderers.EnumFieldRenderer):
|
|||
return status_code_text
|
||||
|
||||
|
||||
class BatchRowGrid(BaseGrid):
|
||||
"""
|
||||
Base grid view for batch rows, which can be filtered and sorted. Also it
|
||||
can delete all rows matching the current list view query.
|
||||
"""
|
||||
|
||||
@property
|
||||
def row_class(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def mapped_class(self):
|
||||
return self.row_class
|
||||
|
||||
@property
|
||||
def config_prefix(self):
|
||||
"""
|
||||
Config prefix for the grid view. This is used to keep track of current
|
||||
filtering and sorting, within the user's session. Derived classes may
|
||||
override this.
|
||||
"""
|
||||
return '{0}.{1}'.format(self.mapped_class.__name__.lower(),
|
||||
self.request.matchdict['uuid'])
|
||||
|
||||
@property
|
||||
def batch_class(self):
|
||||
"""
|
||||
Model class of the batch to which the rows belong.
|
||||
"""
|
||||
return self.row_class.__batch_class__
|
||||
|
||||
@property
|
||||
def batch_display(self):
|
||||
"""
|
||||
Singular display text for the batch type, e.g. "Vendor Invoice".
|
||||
Override this as necessary.
|
||||
"""
|
||||
return self.batch_class.__name__
|
||||
|
||||
def current_batch(self):
|
||||
"""
|
||||
Return the current batch, based on the UUID within the URL.
|
||||
"""
|
||||
return Session.query(self.batch_class).get(self.request.matchdict['uuid'])
|
||||
|
||||
def modify_query(self, q):
|
||||
q = super(BatchRowGrid, self).modify_query(q)
|
||||
q = q.filter(self.row_class.batch == self.current_batch())
|
||||
q = q.filter(self.row_class.removed == False)
|
||||
return q
|
||||
|
||||
def join_map(self):
|
||||
"""
|
||||
Provides the default join map for batch row grid views. Derived
|
||||
classes should *not* override this, but :meth:`join_map_extras()`
|
||||
instead.
|
||||
"""
|
||||
return self.join_map_extras()
|
||||
|
||||
def filter_map(self):
|
||||
"""
|
||||
Provides the default filter map for batch row grid views. Derived
|
||||
classes should *not* override this, but :meth:`filter_map_extras()`
|
||||
instead.
|
||||
"""
|
||||
return self.make_filter_map(exact=['status_code'])
|
||||
|
||||
def filter_config(self):
|
||||
"""
|
||||
Provides the default filter config for batch grid views. Derived
|
||||
classes should *not* override this, but :meth:`filter_config_extras()`
|
||||
instead.
|
||||
"""
|
||||
kwargs = {'filter_label_status_code': "Status",
|
||||
'filter_factory_status_code': EnumSearchFilter(self.row_class.STATUS)}
|
||||
kwargs.update(self.filter_config_extras())
|
||||
return self.make_filter_config(**kwargs)
|
||||
|
||||
def sort_map(self):
|
||||
"""
|
||||
Provides the default sort map for batch grid views. Derived classes
|
||||
should *not* override this, but :meth:`sort_map_extras()` instead.
|
||||
"""
|
||||
map_ = self.make_sort_map()
|
||||
map_.update(self.sort_map_extras())
|
||||
return map_
|
||||
|
||||
def sort_config(self):
|
||||
"""
|
||||
Provides the default sort config for batch grid views. Derived classes
|
||||
may override this.
|
||||
"""
|
||||
return self.make_sort_config(sort='sequence', dir='asc')
|
||||
|
||||
def grid(self):
|
||||
"""
|
||||
Creates the grid for the view. Derived classes should *not* override
|
||||
this, but :meth:`configure_grid()` instead.
|
||||
"""
|
||||
g = self.make_grid()
|
||||
g.extra_row_class = self.tr_class
|
||||
g.sequence.set(label="Seq.")
|
||||
g.status_code.set(label="Status", renderer=StatusRenderer(self.row_class.STATUS))
|
||||
self._configure_grid(g)
|
||||
self.configure_grid(g)
|
||||
|
||||
batch = self.current_batch()
|
||||
g.viewable = True
|
||||
g.view_route_name = '{0}.row.view'.format(self.route_prefix)
|
||||
# TODO: Fix this check for edit mode.
|
||||
edit_mode = self.request.referrer.endswith('/edit')
|
||||
if edit_mode and not batch.executed and self.request.has_perm('{0}.edit'.format(self.permission_prefix)):
|
||||
# g.editable = True
|
||||
# g.edit_route_name = '{0}.rows.edit'.format(self.route_prefix)
|
||||
g.deletable = True
|
||||
g.delete_route_name = '{0}.rows.delete'.format(self.route_prefix)
|
||||
return g
|
||||
|
||||
def tr_class(self, row, i):
|
||||
pass
|
||||
|
||||
def render_kwargs(self):
|
||||
"""
|
||||
Add the current batch and route prefix to the template context.
|
||||
"""
|
||||
return {'batch': self.current_batch(),
|
||||
'route_prefix': self.route_prefix}
|
||||
|
||||
def bulk_delete(self):
|
||||
"""
|
||||
"Delete" all rows matching the current row grid view query. This sets
|
||||
the ``removed`` flag on the rows but does not truly delete them.
|
||||
"""
|
||||
self.query().update({'removed': True}, synchronize_session=False)
|
||||
return httpexceptions.HTTPFound(location=self.request.route_url('{}.view'.format(self.route_prefix),
|
||||
uuid=self.request.matchdict['uuid']))
|
||||
|
||||
|
||||
class ProductBatchRowGrid(BatchRowGrid):
|
||||
"""
|
||||
Base grid view for batch rows which deal directly with products.
|
||||
"""
|
||||
|
||||
def filter_map(self):
|
||||
"""
|
||||
Provides the default filter map for batch row grid views. Derived
|
||||
classes should *not* override this, but :meth:`filter_map_extras()`
|
||||
instead.
|
||||
"""
|
||||
return self.make_filter_map(exact=['status_code'],
|
||||
ilike=['brand_name', 'description', 'size'],
|
||||
upc=self.filter_gpc(self.row_class.upc))
|
||||
|
||||
def filter_config(self):
|
||||
"""
|
||||
Provides the default filter config for batch grid views. Derived
|
||||
classes should *not* override this, but :meth:`filter_config_extras()`
|
||||
instead.
|
||||
"""
|
||||
kwargs = {'filter_label_status_code': "Status",
|
||||
'filter_factory_status_code': EnumSearchFilter(self.row_class.STATUS),
|
||||
'filter_label_upc': "UPC",
|
||||
'filter_label_brand_name': "Brand"}
|
||||
kwargs.update(self.filter_config_extras())
|
||||
return self.make_filter_config(**kwargs)
|
||||
|
||||
|
||||
class BatchRowCrud(BaseCrud):
|
||||
"""
|
||||
Base CRUD view for batch rows.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue