Skip to content

Commit

Permalink
Remove __setters__ feature and add edit/new specific form options sup…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
amol- committed Oct 30, 2015
1 parent db1c143 commit d2e4287
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
34 changes: 27 additions & 7 deletions tgext/crud/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tgext.crud.decorators import (registered_validate, register_validators, catch_errors,
optional_paginate)
from tgext.crud.utils import (SmartPaginationCollection, RequestLocalTableFiller, create_setter,
from tgext.crud.utils import (SmartPaginationCollection, RequestLocalTableFiller,
set_table_filler_getter, SortableTableBase, map_args_to_pks,
adapt_params_for_pagination, allow_json_parameters,
force_response_type, redirect_on_completion)
Expand Down Expand Up @@ -481,6 +481,19 @@ def get_delete(self, *args, **kw):
return dict(args=args)

class EasyCrudRestController(CrudRestController):
"""A CrudRestController that provides a quick way to setup Sprox forms and Table.
Form options are available through the ``__form_options__`` dictionary which
can contain any option accepted by sprox :class:`FormBase`. Options specific to
*NewForm* and *EditForm* can be provided through ``__form_new_options__`` and
``__form_edit_options__``.
Table options are available through the ``__table_options__`` dictionary which
can contain any option accepted by sprox :class:`TableBase`. Dictionary keys
that do not start with **__** will be threated as :class:`TableFiller` attributes
apart from ``__actions__`` which is always assigned to the :class:`TableFiller`.
"""
def __init__(self, session, menu_items=None):
if not (hasattr(self, 'table') or hasattr(self, 'table_type')):
class Table(SortableTableBase):
Expand Down Expand Up @@ -514,20 +527,27 @@ class NewFiller(AddFormFiller):

super(EasyCrudRestController, self).__init__(session, menu_items)

#Permit to quickly customize form options
# Permit to quickly customize form options
if hasattr(self, '__form_options__'):
for name, value in self.__form_options__.items():
for form in (self.edit_form, self.new_form):
if form:
setattr(form, name, value)

#Permit to quickly create custom actions to set values
if hasattr(self, '__setters__'):
for name, config in self.__setters__.items():
setattr(self, name, create_setter(self, self.get_all, config))
if hasattr(self, '__form_new_options__'):
for name, value in self.__form_new_options__.items():
if self.new_form:
setattr(self.new_form, name, value)

if hasattr(self, '__form_edit_options__'):
for name, value in self.__form_edit_options__.items():
if self.edit_form:
setattr(self.edit_form, name, value)

if hasattr(self, '__setters__'):
raise ValueError('__setters__ are deprecated and no longer supported.')

#Permit to quickly customize table options
# Permit to quickly customize table options
if hasattr(self, '__table_options__'):
for name, value in self.__table_options__.items():
if name.startswith('__') and name != '__actions__':
Expand Down
14 changes: 0 additions & 14 deletions tgext/crud/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from tg import expose, validate, redirect, request, url, tmpl_context
from .validators import EntityValidator
from sprox.tablebase import TableBase
from sprox.fillerbase import TableFiller
from markupsafe import Markup
Expand All @@ -22,19 +21,6 @@ def sprox_with_tw2():
else:
from tw.forms.datagrid import Column

def create_setter(crud_controller, err_handler, config):
provider = crud_controller.provider
model = crud_controller.model

@expose()
@validate({'obj':EntityValidator(provider, model)}, error_handler=err_handler)
def autogenerated_setter(self, obj):
name, value = config
setattr(obj, name, callable(value) and value(obj) or value)
return redirect('../')

return autogenerated_setter.__get__(crud_controller, crud_controller.__class__)

def set_table_filler_getter(filler, name, function):
meth = function.__get__(filler, filler.__class__)
setattr(filler, name, meth)
Expand Down
18 changes: 0 additions & 18 deletions tgext/crud/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,3 @@
def report_json_error(*args, **kw):
response.status_code = 400
return tmpl_context.form_errors


class EntityValidator(FancyValidator):
def __init__(self, provider, model):
super(FancyValidator, self).__init__()
self.model = model
self.provider = provider
self.primary_field = self.provider.get_primary_field(self.model)

def _to_python(self, value, state):
try:
return self.provider.get_obj(self.model, {self.primary_field:value})
except:
return None

def validate_python(self, value, state):
if not value:
raise Invalid('object not found', value, state)

0 comments on commit d2e4287

Please sign in to comment.