Back to index

plone3  3.1.7
base.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getMultiAdapter, queryMultiAdapter
00003 from zope.formlib import form
00004 
00005 import zope.event
00006 import zope.lifecycleevent
00007 
00008 from Acquisition import aq_inner, aq_parent
00009 from Products.Five.formlib import formbase
00010 from Products.CMFPlone import PloneMessageFactory as _
00011 
00012 from plone.app.form.interfaces import IPlonePageForm
00013 from plone.app.form.interfaces import IEditForm
00014 from plone.app.form.validators import null_validator
00015 from plone.app.form.events import EditBegunEvent, EditCancelledEvent, EditSavedEvent
00016 
00017 class AddForm(formbase.AddForm):
00018     """An add form with standard Save and Cancel buttons
00019     """
00020     
00021     implements(IPlonePageForm)
00022         
00023     @form.action(_(u"label_save", default=u"Save"),
00024                  condition=form.haveInputWidgets,
00025                  name=u'save')
00026     def handle_save_action(self, action, data):
00027         self.createAndAdd(data)
00028     
00029     @form.action(_(u"label_cancel", default=u"Cancel"),
00030                  validator=null_validator,
00031                  name=u'cancel')
00032     def handle_cancel_action(self, action, data):
00033         parent = aq_parent(aq_inner(self.context))
00034         self.request.response.redirect(parent.absolute_url())
00035 
00036 
00037 class EditForm(formbase.EditForm):
00038     """An edit form with standard Save and Cancel buttons
00039     """
00040     
00041     implements(IPlonePageForm, IEditForm)
00042     
00043     def update(self):
00044         zope.event.notify(EditBegunEvent(self.context))
00045         super(EditForm, self).update()
00046         
00047     def render(self):
00048         # If the object is locked, don't show any widgets
00049         lock_info = queryMultiAdapter((self.context, self.request), name="plone_lock_info")
00050         if lock_info is not None and lock_info.is_locked_for_current_user():
00051             self.widgets = form.Widgets([], prefix=self.prefix)
00052             self.form_name = None # hide border
00053         return super(EditForm, self).render()
00054     
00055     @form.action(_(u"label_save", default="Save"),
00056                  condition=form.haveInputWidgets,
00057                  name=u'save')
00058     def handle_save_action(self, action, data):
00059         if form.applyChanges(self.context, self.form_fields, data, self.adapters):
00060             zope.event.notify(zope.lifecycleevent.ObjectModifiedEvent(self.context))
00061             zope.event.notify(EditSavedEvent(self.context))
00062             self.status = "Changes saved"
00063         else:
00064             zope.event.notify(EditCancelledEvent(self.context))
00065             self.status = "No changes"
00066             
00067         url = getMultiAdapter((self.context, self.request), name='absolute_url')()
00068         self.request.response.redirect(url)
00069             
00070     @form.action(_(u"label_cancel", default=u"Cancel"),
00071                  validator=null_validator,
00072                  name=u'cancel')
00073     def handle_cancel_action(self, action, data):
00074         zope.event.notify(EditCancelledEvent(self.context))
00075         url = getMultiAdapter((self.context, self.request), name='absolute_url')()
00076         self.request.response.redirect(url)