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 
00005 import zope.event
00006 import zope.lifecycleevent
00007 
00008 from Acquisition import aq_parent, aq_inner
00009 from Products.Five.browser import BrowserView
00010 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00011 from Products.Five.formlib import formbase
00012 from Products.CMFPlone import PloneMessageFactory as _
00013 
00014 from plone.app.form import named_template_adapter
00015 from plone.app.form.validators import null_validator
00016 
00017 from plone.app.portlets.interfaces import IPortletPermissionChecker
00018 
00019 from plone.app.portlets.browser.interfaces import IPortletAddForm
00020 from plone.app.portlets.browser.interfaces import IPortletEditForm
00021  
00022 # Add a named template form, which allows us to carry some extra information
00023 # about the referer
00024 _template = ViewPageTemplateFile('templates/portlets-pageform.pt')
00025 portlets_named_template_adapter = named_template_adapter(_template)
00026 
00027 class AddForm(formbase.AddFormBase):
00028     """A base add form for portlets.
00029     
00030     Use this for portlet assignments that require configuration before being 
00031     added. Assignment types that do not should use NullAddForm instead.
00032     
00033     Sub-classes should define create() and set the form_fields class variable.
00034     
00035     Notice the suble difference between AddForm and NullAddform in that the
00036     create template method for AddForm takes as a parameter a dict 'data':
00037     
00038         def create(self, data):
00039             return MyAssignment(data.get('foo'))
00040             
00041     whereas the NullAddForm has no data parameter:
00042     
00043         def create(self):
00044             return MyAssignment()
00045     """
00046     
00047     implements(IPortletAddForm)
00048     
00049     form_name = _(u"Configure portlet")
00050     
00051     def __call__(self):
00052         IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
00053         return super(AddForm, self).__call__()
00054     
00055     def referer(self):
00056         return self.request.get('referer', '')
00057 
00058     def nextURL(self):
00059         referer = self.request.form.get('referer')
00060         if referer:
00061             return referer
00062         else:
00063             addview = aq_parent(aq_inner(self.context))
00064             context = aq_parent(aq_inner(addview))
00065             url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00066             return url + '/@@manage-portlets'
00067     
00068     @form.action(_(u"label_save", default=u"Save"), name=u'save')
00069     def handle_save_action(self, action, data):
00070         self.createAndAdd(data)
00071 
00072     @form.action(_(u"label_cancel", default=u"Cancel"),
00073                  validator=null_validator,
00074                  name=u'cancel')
00075     def handle_cancel_action(self, action, data):
00076         nextURL = self.nextURL()
00077         if nextURL:
00078             self.request.response.redirect(self.nextURL())
00079         return ''
00080         
00081 class NullAddForm(BrowserView):
00082     """An add view that will add its content immediately, without presenting
00083     a form.
00084     
00085     You should subclass this for portlets that do not require any configuration
00086     before being added, and write a create() method that takes no parameters
00087     and returns the appropriate assignment instance.
00088     """
00089     
00090     def __call__(self):
00091         IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
00092         ob = self.create()
00093         zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(ob))
00094         self.context.add(ob)
00095         nextURL = self.nextURL()
00096         if nextURL:
00097             self.request.response.redirect(self.nextURL())
00098         return ''
00099     
00100     def nextURL(self):
00101         referer = self.request.get('referer')
00102         if referer:
00103             return referer
00104         else:
00105             addview = aq_parent(aq_inner(self.context))
00106             context = aq_parent(aq_inner(addview))
00107             url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00108             return url + '/@@manage-portlets'
00109     
00110     def create(self):
00111         raise NotImplementedError("concrete classes must implement create()")
00112     
00113 
00114 class EditForm(formbase.EditFormBase):
00115     """An edit form for portlets.
00116     """
00117     
00118     implements(IPortletEditForm)
00119     
00120     form_name = _(u"Modify portlet")
00121     
00122     def __call__(self):
00123         IPortletPermissionChecker(aq_parent(aq_inner(self.context)))()
00124         return super(EditForm, self).__call__()
00125     
00126     def referer(self):
00127         return self.request.get('referer', '')
00128 
00129     def nextURL(self):
00130         referer = self.request.form.get('referer')
00131         if referer:
00132             return referer
00133         else:    
00134             portlet = aq_inner(self.context)
00135             context = aq_parent(portlet)
00136             url = str(getMultiAdapter((context, self.request), name=u"absolute_url"))
00137             return url + '/@@manage-portlets'
00138     
00139     @form.action(_(u"label_save", default=u"Save"),
00140                  condition=form.haveInputWidgets,
00141                  name=u'save')
00142     def handle_save_action(self, action, data):
00143         if form.applyChanges(self.context, self.form_fields, data, self.adapters):
00144             zope.event.notify(zope.lifecycleevent.ObjectModifiedEvent(self.context))
00145             self.status = "Changes saved"
00146         else:
00147             self.status = "No changes"
00148             
00149         nextURL = self.nextURL()
00150         if nextURL:
00151             self.request.response.redirect(self.nextURL())
00152         return ''
00153             
00154     @form.action(_(u"label_cancel", default=u"Cancel"),
00155                  validator=null_validator,
00156                  name=u'cancel')
00157     def handle_cancel_action(self, action, data):
00158         nextURL = self.nextURL()
00159         if nextURL:
00160             self.request.response.redirect(self.nextURL())
00161         return ''