Add initial support for mobile "quick row" feature, for ordering
at least for now, ordering only, but hopefully much more soon...
This commit is contained in:
parent
aa6e540abd
commit
68bd3047c4
10 changed files with 143 additions and 23 deletions
|
@ -45,6 +45,7 @@ from rattail.csvutil import UnicodeDictWriter
|
|||
from rattail.files import temp_path
|
||||
from rattail.excel import ExcelWriter
|
||||
|
||||
import colander
|
||||
import deform
|
||||
from pyramid import httpexceptions
|
||||
from pyramid.renderers import get_renderer, render_to_response, render
|
||||
|
@ -132,6 +133,8 @@ class MasterView(View):
|
|||
rows_downloadable_csv = False
|
||||
|
||||
mobile_rows_creatable = False
|
||||
mobile_rows_creatable_via_browse = False
|
||||
mobile_rows_quickable = False
|
||||
mobile_rows_filterable = False
|
||||
mobile_rows_viewable = False
|
||||
mobile_rows_editable = False
|
||||
|
@ -1060,6 +1063,46 @@ class MasterView(View):
|
|||
self.configure_mobile_row_form(form)
|
||||
return form
|
||||
|
||||
def make_quick_row_form(self, instance=None, factory=None, fields=None, schema=None, mobile=False, **kwargs):
|
||||
"""
|
||||
Creates a "quick" form for adding a new row to the given instance.
|
||||
"""
|
||||
if factory is None:
|
||||
factory = self.get_quick_row_form_factory(mobile=mobile)
|
||||
if fields is None:
|
||||
fields = self.get_quick_row_form_fields(mobile=mobile)
|
||||
if schema is None:
|
||||
schema = self.make_quick_row_form_schema(mobile=mobile)
|
||||
|
||||
kwargs['mobile'] = mobile
|
||||
kwargs = self.make_quick_row_form_kwargs(**kwargs)
|
||||
form = factory(fields, schema, **kwargs)
|
||||
self.configure_quick_row_form(form, mobile=mobile)
|
||||
return form
|
||||
|
||||
def get_quick_row_form_factory(self, mobile=False):
|
||||
return forms.Form
|
||||
|
||||
def get_quick_row_form_fields(self, mobile=False):
|
||||
pass
|
||||
|
||||
def make_quick_row_form_schema(self, mobile=False):
|
||||
schema = colander.MappingSchema()
|
||||
schema.add(colander.SchemaNode(colander.String(), name='quick_row_entry'))
|
||||
return schema
|
||||
|
||||
def make_quick_row_form_kwargs(self, **kwargs):
|
||||
defaults = {
|
||||
'request': self.request,
|
||||
'model_class': getattr(self, 'model_row_class', None),
|
||||
'cancel_url': self.request.get_referrer(),
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return defaults
|
||||
|
||||
def configure_quick_row_form(self, form, mobile=False):
|
||||
pass
|
||||
|
||||
def get_mobile_row_form_fields(self):
|
||||
if hasattr(self, 'mobile_row_form_fields'):
|
||||
return self.mobile_row_form_fields
|
||||
|
@ -1117,6 +1160,9 @@ class MasterView(View):
|
|||
return False
|
||||
return True
|
||||
|
||||
def validate_quick_row_form(self, form):
|
||||
return form.validate(newstyle=True)
|
||||
|
||||
def get_mobile_row_data(self, parent):
|
||||
return self.get_row_data(parent)
|
||||
|
||||
|
@ -1723,6 +1769,7 @@ class MasterView(View):
|
|||
"""
|
||||
context = {
|
||||
'master': self,
|
||||
'mobile': mobile,
|
||||
'model_title': self.get_model_title(),
|
||||
'model_title_plural': self.get_model_title_plural(),
|
||||
'route_prefix': self.get_route_prefix(),
|
||||
|
@ -1747,7 +1794,7 @@ class MasterView(View):
|
|||
context.update(self.template_kwargs(**context))
|
||||
if hasattr(self, 'template_kwargs_{}'.format(template)):
|
||||
context.update(getattr(self, 'template_kwargs_{}'.format(template))(**context))
|
||||
if hasattr(self, 'mobile_template_kwargs_{}'.format(template)):
|
||||
if mobile and hasattr(self, 'mobile_template_kwargs_{}'.format(template)):
|
||||
context.update(getattr(self, 'mobile_template_kwargs_{}'.format(template))(**context))
|
||||
|
||||
# First try the template path most specific to the view.
|
||||
|
@ -2448,6 +2495,33 @@ class MasterView(View):
|
|||
'form': form,
|
||||
}, mobile=True)
|
||||
|
||||
def mobile_quick_row(self):
|
||||
"""
|
||||
Mobile view for "quick" location or creation of a row object
|
||||
"""
|
||||
parent = self.get_instance()
|
||||
parent_url = self.get_action_url('view', parent, mobile=True)
|
||||
form = self.make_quick_row_form(self.model_row_class, mobile=True, cancel_url=parent_url)
|
||||
if self.request.method == 'POST':
|
||||
if self.validate_quick_row_form(form):
|
||||
row = self.save_quick_row_form(form)
|
||||
if not row:
|
||||
self.request.session.flash("Could not locate/create row for entry: "
|
||||
"{}".format(form.validated['quick_row_entry']),
|
||||
'error')
|
||||
return self.redirect(parent_url)
|
||||
return self.redirect_after_quick_row(row, mobile=True)
|
||||
return self.redirect(parent_url)
|
||||
|
||||
def save_quick_row_form(self, form):
|
||||
raise NotImplementedError("You must define `{}:{}.save_quick_row_form()` "
|
||||
"in order to process quick row forms".format(
|
||||
self.__class__.__module__,
|
||||
self.__class__.__name__))
|
||||
|
||||
def redirect_after_quick_row(self, row, mobile=False):
|
||||
return self.redirect(self.get_row_action_url('edit', row, mobile=mobile))
|
||||
|
||||
def view_row(self):
|
||||
"""
|
||||
View for viewing details of a single data row.
|
||||
|
@ -2877,6 +2951,10 @@ class MasterView(View):
|
|||
config.add_route('mobile.{}.create_row'.format(route_prefix), '/mobile{}/{{{}}}/new-row'.format(url_prefix, model_key))
|
||||
config.add_view(cls, attr='mobile_create_row', route_name='mobile.{}.create_row'.format(route_prefix),
|
||||
permission='{}.create_row'.format(permission_prefix))
|
||||
if cls.mobile_rows_quickable:
|
||||
config.add_route('mobile.{}.quick_row'.format(route_prefix), '/mobile{}/{{{}}}/quick-row'.format(url_prefix, model_key))
|
||||
config.add_view(cls, attr='mobile_quick_row', route_name='mobile.{}.quick_row'.format(route_prefix),
|
||||
permission='{}.create_row'.format(permission_prefix))
|
||||
|
||||
# view row
|
||||
if cls.has_rows:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue