Ignore:
Timestamp:
30 Jan 2009, 19:00:37 (16 years ago)
Author:
uli
Message:

Add a viewlet-mixin for rendering forms.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/viewlets.py

    r3882 r3895  
    22import grok
    33import zope.interface
     4from zope.component import getMultiAdapter
    45from interfaces import IUniversity, IWAeUPObject
    56
     
    8384class Copyright(grok.Viewlet):
    8485    grok.viewletmanager(Footer)
     86
     87class FormWrapMixin(object):
     88    """Mixin for viewlets that renders forms.
     89
     90    XXX: Need tests for this.
     91   
     92    This is a mixin that provides some convenience features for
     93    viewlets that want to render forms, by overriding the default
     94    `render()` and `update()` methods.
     95
     96    To create a viewlet that renders a form, you can define one as
     97    usual but also derive from this mixin-class.
     98
     99    Say we have a usual form view like this::
     100
     101      >>> import grok
     102      >>> class EditForm(grok.EditForm):
     103      ...   grok.context(SomeType)
     104      ...   grok.name('addsometype')
     105      ...   form_fields = grok.AutoFields(IUniversity)
     106      ...   @grok.action('Save')
     107      ...   def save(self, **data):
     108      ...     self.applyData(self.context, **data)
     109      ...     return
     110
     111    then we can create a viewlet that displays this view like this::
     112     
     113      >>> import grok
     114      >>> from waeup.viewlets import FormWrapMixin
     115      >>> class EditFormViewlet(FormWrapMixin, grok.Viewlet):
     116      ...     grok.viewletmanager(MainArea)
     117      ...     grok.context(ISomeType)
     118      ...     grok.view(Manage)
     119      ...     formview_name = 'addsometype'
     120
     121    Here we define a regular viewlet with two modifications:
     122
     123    * it is also derived from `FormWrapMixin` and
     124
     125    * it provides an attribute ``formview_name`` which tells us, what
     126      form we want to be rendered in this viewlet. We tell the name of
     127      this form and not its class or similar.
     128
     129    The associated view, context, viewletmanager, etc. can be set as
     130    with regular viewlets.
     131   
     132    """
     133    formview_name = None
     134   
     135    def update(self):
     136        self.form = getMultiAdapter((self.context, self.request),
     137                                    name=self.formview_name)
     138        self.form.update_form()
     139        if self.request.method == 'POST':
     140            #app = get_application(self.context)
     141            #self.__parent__.redirect(self.__parent__.url(obj=app))
     142            pass
     143
     144    def render(self):
     145        result = self.form.render()
     146        try:
     147            # strip all except the form part...
     148            result = re.match('^.+\(<form[^\>]+>.*</form>).+$', result,
     149                              re.DOTALL).groups()[0]
     150        except:
     151            # except there is no such part...
     152            pass
     153        return result
Note: See TracChangeset for help on using the changeset viewer.