Add has_rows support to MasterView class

Eventually the batch views should be refactored to leverage this..
This commit is contained in:
Lance Edgar 2016-08-20 22:49:41 -05:00
parent 340f02a3f2
commit 53950931af
2 changed files with 122 additions and 3 deletions

View file

@ -3,6 +3,23 @@
<%def name="title()">${model_title}: ${instance_title}</%def> <%def name="title()">${model_title}: ${instance_title}</%def>
<%def name="head_tags()">
${parent.head_tags()}
% if master.has_rows:
${h.javascript_link(request.static_url('tailbone:static/js/jquery.ui.tailbone.js'))}
<script type="text/javascript">
$(function() {
$('.newgrid-wrapper').gridwrapper();
});
</script>
<style type="text/css">
.newgrid-wrapper {
margin-top: 10px;
}
</style>
% endif
</%def>
<%def name="context_menu_items()"> <%def name="context_menu_items()">
<li>${h.link_to("Back to {}".format(model_title_plural), index_url)}</li> <li>${h.link_to("Back to {}".format(model_title_plural), index_url)}</li>
<li>${h.link_to("Permalink for this {}".format(model_title), action_url('view', instance))}</li> <li>${h.link_to("Permalink for this {}".format(model_title), action_url('view', instance))}</li>
@ -24,3 +41,7 @@
<div class="form-wrapper"> <div class="form-wrapper">
${form.render()|n} ${form.render()|n}
</div><!-- form-wrapper --> </div><!-- form-wrapper -->
% if master.has_rows:
${rows_grid|n}
% endif

View file

@ -37,7 +37,7 @@ import formalchemy
from pyramid import httpexceptions from pyramid import httpexceptions
from pyramid.renderers import get_renderer, render_to_response, render from pyramid.renderers import get_renderer, render_to_response, render
from tailbone import forms from tailbone import forms, newgrids as grids
from tailbone.views import View from tailbone.views import View
from tailbone.newgrids import filters, AlchemyGrid, GridAction from tailbone.newgrids import filters, AlchemyGrid, GridAction
@ -67,6 +67,11 @@ class MasterView(View):
grid_index = None grid_index = None
use_index_links = False use_index_links = False
# ROW-RELATED ATTRS FOLLOW:
has_rows = False
model_row_class = None
@property @property
def Session(self): def Session(self):
""" """
@ -139,12 +144,105 @@ class MasterView(View):
if instance is None: if instance is None:
instance = self.get_instance() instance = self.get_instance()
form = self.make_form(instance) form = self.make_form(instance)
return self.render_to_response('view', { if self.has_rows:
# If user just refreshed the page with a reset instruction, issue a
# redirect in order to clear out the query string.
if self.request.GET.get('reset-to-default-filters') == 'true':
return self.redirect(self.request.current_route_url(_query=None))
grid = self.make_row_grid(instance=instance)
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
context = {
'instance': instance, 'instance': instance,
'instance_title': self.get_instance_title(instance), 'instance_title': self.get_instance_title(instance),
'instance_editable': self.editable_instance(instance), 'instance_editable': self.editable_instance(instance),
'instance_deletable': self.deletable_instance(instance), 'instance_deletable': self.deletable_instance(instance),
'form': form}) 'form': form,
}
if self.has_rows:
context['rows_grid'] = grid.render_complete(allow_save_defaults=False,
tools=self.make_row_grid_tools())
return self.render_to_response('view', context)
def make_row_grid_tools(self):
pass
def make_row_grid(self, **kwargs):
"""
Make and return a new (configured) rows grid instance.
"""
instance = kwargs.pop('instance', self.get_instance())
data = self.get_row_data(instance)
kwargs = self.make_row_grid_kwargs(**kwargs)
key = '{}.{}'.format(self.get_grid_key(), self.request.matchdict[self.get_model_key()])
grid = grids.AlchemyGrid(key, self.request, data=data, model_class=self.model_row_class, **kwargs)
self._preconfigure_row_grid(grid)
self.configure_row_grid(grid)
grid.load_settings()
return grid
def _preconfigure_row_grid(self, g):
pass
def configure_row_grid(self, grid):
grid.configure()
def get_row_data(self, instance):
"""
Generate the base data set for a rows grid.
"""
raise NotImplementedError
@classmethod
def get_row_route_prefix(cls):
"""
Route prefix specific to the row-level views for a batch, e.g.
``'vendorcatalogs.rows'``.
"""
return "{}.rows".format(cls.get_route_prefix())
@classmethod
def get_row_permission_prefix(cls):
"""
Permission prefix specific to the row-level data for this batch type,
e.g. ``'vendorcatalogs.rows'``.
"""
return "{}.rows".format(cls.get_permission_prefix())
def make_row_grid_kwargs(self, **kwargs):
"""
Return a dict of kwargs to be used when constructing a new rows grid.
"""
route_prefix = self.get_row_route_prefix()
permission_prefix = self.get_row_permission_prefix()
defaults = {
'width': 'full',
'filterable': True,
'sortable': True,
'pageable': True,
'row_attrs': self.row_grid_row_attrs,
'model_title': self.get_row_model_title(),
'model_title_plural': self.get_row_model_title_plural(),
'permission_prefix': permission_prefix,
'route_prefix': route_prefix,
}
defaults.update(kwargs)
return defaults
def row_grid_row_attrs(self, row, i):
return {}
def get_row_model_title(self):
return "{} Row".format(self.get_model_title())
def get_row_model_title_plural(self):
return "{} Rows".format(self.get_model_title())
def view_index(self): def view_index(self):
""" """