-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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?



Thanks,
- -- 
Jean-Denis Girard

SysNux                  Systèmes  Linux  en Polynésie française
http://www.sysnux.pf/   Tél: +689 50 10 40 / GSM: +689 79 75 27
-----BEGIN PGP SIGNATURE-----

iEYEARECAAYFAkyNRzoACgkQuu7Rv+oOo/iwkACfSz4n83fxsvUgLAxThIg9vaK/
4UsAn0UEMsQVt07RQ6v2tHcQrt9oL3pJ
=cNv7
-----END PGP SIGNATURE-----

-- 
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