Back to index

plone3  3.1.7
formhelper.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getMultiAdapter
00003 from zope.formlib import form
00004 import zope.event
00005 import zope.lifecycleevent
00006 
00007 from plone.app.form import named_template_adapter
00008 from plone.app.form.validators import null_validator
00009 
00010 from Acquisition import aq_parent, aq_inner
00011 from Products.CMFPlone import PloneMessageFactory as _
00012 from Products.Five.browser import BrowserView
00013 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00014 from Products.Five.formlib import formbase
00015 
00016 from plone.app.contentrules.browser.interfaces import IContentRulesForm
00017 
00018 # Add a named template form, which allows us to carry some extra information
00019 # about the referer
00020 _template = ViewPageTemplateFile('templates/contentrules-pageform.pt')
00021 contentrules_named_template_adapter = named_template_adapter(_template)
00022 
00023 class AddForm(formbase.AddFormBase):
00024     """A base add form for content rule.
00025     
00026     Use this for rule elements that require configuration before being added to
00027     a rule. Element types that do not should use NullAddForm instead.
00028     
00029     Sub-classes should define create() and set the form_fields class variable.
00030     
00031     Notice the suble difference between AddForm and NullAddform in that the
00032     create template method for AddForm takes as a parameter a dict 'data':
00033     
00034         def create(self, data):
00035             return MyAssignment(data.get('foo'))
00036             
00037     whereas the NullAddForm has no data parameter:
00038     
00039         def create(self):
00040             return MyAssignment()
00041     """
00042     
00043     implements(IContentRulesForm)
00044     
00045     def nextURL(self):
00046         rule = aq_parent(aq_inner(self.context))
00047         context = aq_parent(aq_inner(rule))
00048         url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00049         return '%s/++rule++%s/@@manage-elements' % (url, rule.__name__)
00050     
00051     @form.action(_(u"label_save", default=u"Save"), name=u'save')
00052     def handle_save_action(self, action, data):
00053         self.createAndAdd(data)
00054     
00055     @form.action(_(u"label_cancel", default=u"Cancel"),
00056                  validator=null_validator,
00057                  name=u'cancel')
00058     def handle_cancel_action(self, action, data):
00059         nextURL = self.nextURL()
00060         if nextURL:
00061             self.request.response.redirect(self.nextURL())
00062         return ''
00063         
00064 class NullAddForm(BrowserView):
00065     """An add view that will add its content immediately, without presenting
00066     a form.
00067     
00068     You should subclass this for rule elements that do not require any 
00069     configuration before being added, and write a create() method that takes no 
00070     parameters and returns the appropriate assignment instance.
00071     """
00072     
00073     def __call__(self):
00074         ob = self.create()
00075         zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(ob))
00076         self.context.add(ob)
00077         nextURL = self.nextURL()
00078         if nextURL:
00079             self.request.response.redirect(self.nextURL())
00080         return ''
00081     
00082     def nextURL(self):
00083         rule = aq_parent(aq_inner(self.context))
00084         context = aq_parent(aq_inner(rule))
00085         url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00086         return '%s/++rule++%s/@@manage-elements' % (url, rule.__name__)
00087     
00088     def create(self):
00089         raise NotImplementedError("concrete classes must implement create()")
00090     
00091 
00092 class EditForm(formbase.EditFormBase):
00093     """An edit form for rule elements.
00094     """
00095     
00096     implements(IContentRulesForm)
00097     
00098     @form.action(_(u"label_save", default=u"Save"),
00099                  condition=form.haveInputWidgets,
00100                  name=u'save')
00101     def handle_save_action(self, action, data):
00102         if form.applyChanges(self.context, self.form_fields, data, self.adapters):
00103             zope.event.notify(zope.lifecycleevent.ObjectModifiedEvent(self.context))
00104             self.status = "Changes saved"
00105         else:
00106             self.status = "No changes"
00107             
00108         nextURL = self.nextURL()
00109         if nextURL:
00110             self.request.response.redirect(self.nextURL())
00111         return ''
00112             
00113     @form.action(_(u"label_cancel", default=u"Cancel"),
00114                  validator=null_validator,
00115                  name=u'cancel')
00116     def handle_cancel_action(self, action, data):
00117         nextURL = self.nextURL()
00118         if nextURL:
00119             self.request.response.redirect(self.nextURL())
00120         return ''
00121 
00122     def nextURL(self):
00123         element = aq_inner(self.context)
00124         rule = aq_parent(element)
00125         context = aq_parent(rule)
00126         url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00127         return '%s/++rule++%s/@@manage-elements' % (url, rule.__name__,)