Hi,

> I would like to display / handle forms differently with regards to
> users permissions. For example, I have a form to create a user; only the
> admin can create a new user, or modify all the user's information. But
> the user himself can modify some of his own data. I'm able to display a
> different form for admin or user, but then how can I pass the @validate
> decorator the form to use? Here is what I have done so far in my user
> controller (I'm using turbogears 2.0):
> 
> user_form_fields = [
>         TextField('user_name', validator=NotEmpty,
>            label_text=u'Compte',
>            help_text=u'Entrez le nom d\'utilisateur'),
> ...
>         HiddenField('_method'), # Needed by RestController
>         HiddenField('user_id', validator=Int),
>         ]
> 
> admin_form_fields = []
> admin_form_fields.extend(user_form_fields)
> admin_form_fields.append( TextArea('numbers', validator=NotEmpty,
> ... )
> 
> new_user_form = TableForm(
>   fields = admin_form_fields,
>   submit_text = u'Valider...',
>   action = 'create',
>   hover_help = True
> )
> 
> admin_edit_user_form = TableForm(
>   fields = admin_form_fields,
>   submit_text = u'Valider...',
>   action = '/users/',
>   hover_help = True
> )
> 
> edit_user_form = TableForm(
>   fields = user_form_fields,
>   submit_text = u'Valider...',
>   action = '/users/',
>   hover_help = True
> )
> 
> class User_ctrl(RestController):
> 
>   @validate(new_user_form, error_handler=new)
>   @require(in_group('ADM',
>      msg=u'Seul un membre du groupe administrateur peut créer un
> utilisateur'))
>   @expose()
>   def create(self, **kw):
>      ''' Add new user to DB
>      ...
> 
>   @expose(template="astportal2.templates.form_new")
>   def edit(self, id=None, **kw):
>      ''' Display edit user form
>      ...
>      if in_group('ADM'):
>         tmpl_context.form = admin_edit_user_form
>      else:
>         tmpl_context.form = edit_user_form
>      return dict(title = u'Modification utilisateur ' + u.user_name,
> debug='', values=v)
> 
> 
>   def user_form_valid():
>      return admin_edit_user_form if in_group('ADM') else edit_user_form
> 
>   @validate(form=user_form_valid(), error_handler=edit)
>   @expose()
>   def put(self, **kw):
>      ''' Update user in DB
>      '''
> 
> - From my understanding, it does not work because user_form_valid() is
> only called once at instantiation. Is there a simple way to achieve what
> I want?


You need two things here:

 - some discriminating information to chose in the put-method which from there 
is to use. The best one is a hidden input field in the form which says which of 
the two forms it is.
 - instead of a form, you pass an instance of a custom object with a 
validate-method. Then, you delegate the call. Like this:

class FormChooser(object):

   def validate(self, params, state):
          f = FORMS_MAP.get(params.get("form_type"), default_form)
          return f.validate(params, state)



@validate(FormChooser())

Diez

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to