class DefaultEditForm(DexterityExtensibleForm, form.EditForm): success_message = _(u"Changes saved") @button.buttonAndHandler(_(u'Save'), name='save') def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) IStatusMessage(self.request).addStatusMessage(self.success_message, "info") self.request.response.redirect(self.nextURL()) notify(EditFinishedEvent(self.context)) @button.buttonAndHandler(_(u'Cancel'), name='cancel') def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(EditCancelledEvent(self.context)) def nextURL(self): view_url = self.context.absolute_url() portal_type = getattr(self, 'portal_type', None) if portal_type is not None: registry = getUtility(IRegistry) use_view_action = registry.get( 'plone.types_use_view_action_in_listings', []) if portal_type in use_view_action: view_url = view_url + '/view' return view_url def update(self): self.portal_type = self.context.portal_type super(DefaultEditForm, self).update() # fire the edit begun only if no action was executed if len(self.actions.executedActions) == 0: notify(EditBegunEvent(self.context)) def updateActions(self): super(DefaultEditForm, self).updateActions() if 'save' in self.actions: self.actions["save"].addClass("context") if 'cancel' in self.actions: self.actions["cancel"].addClass("standalone") @property def fti(self): return getUtility(IDexterityFTI, name=self.portal_type) @property def label(self): type_name = self.fti.Title() return _(u"Edit ${name}", mapping={'name': type_name})
def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info") self.request.response.redirect(self.nextURL()) notify(EditFinishedEvent(self.context))
def handleAdd(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return obj = self.createAndAdd(data) if obj is not None: # mark only as finished if we get the new object self._finishedAdd = True IStatusMessage(self.request).addStatusMessage(_(u"Item created"), "info")
class AsyncEditForm(edit.DefaultEditForm): success_message = _(u"Changes will be saved") @button.buttonAndHandler(_(u"Save"), name="save") def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: notify(events.AsyncBeforeEdit(self.context)) except Unauthorized: IStatusMessage(self.request).add( _(u"You are not authorized to edit this element.")) return except interfaces.AsyncValidationFailed, e: IStatusMessage(self.request).add(unicode(e)) return content = self.getContent() changes = form.applyChanges(self, content, data) new_changes = list() for interface, names in changes.items(): new_changes.append((interface.__identifier__, names)) uuid = IUUID(content, 0) task_id = utils.register_task(action=constants.EDIT, changes=new_changes, context=uuid) tasks.finish_edit.apply_async([content, task_id], dict()) IStatusMessage(self.request).addStatusMessage(self.success_message, "info") url = content.absolute_url() portal_type = getattr(aq_base(content), "portal_type", None) registry = zope.component.getUtility(IRegistry) use_view_action = registry.get( "plone.types_use_view_action_in_listings", []) if portal_type in use_view_action: url = url + '/view' return self.request.response.redirect(url)
def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: notify(events.AsyncBeforeEdit(self.context)) except Unauthorized: IStatusMessage(self.request).add( _(u"You are not authorized to edit this element.")) return except interfaces.AsyncValidationFailed, e: IStatusMessage(self.request).add(unicode(e)) return
class DexterityExtensibleForm(AutoExtensibleForm): """Mixin class for Dexterity forms that support updatable fields """ default_fieldset_label = _(u"Content") @property def description(self): fti = getUtility(IDexterityFTI, name=self.portal_type) return fti.Description() # AutoExtensibleForm contract @property def schema(self): fti = getUtility(IDexterityFTI, name=self.portal_type) return fti.lookupSchema() @property def additionalSchemata(self): return getAdditionalSchemata(context=self.context, portal_type=self.portal_type)
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( _(u"Add New Item operation cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(AddCancelledEvent(self.context))
def label(self): portal_type = self.portal_type fti = getUtility(IDexterityFTI, name=portal_type) type_name = fti.Title() return _(u"Add ${name}", mapping={'name': type_name})
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Add New Item operation cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(AddCancelledEvent(self.context))
def label(self): type_name = self.fti.Title() return _(u"Edit ${name}", mapping={'name': type_name})
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(EditCancelledEvent(self.context))
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( _(u"Edit cancelled"), "info" ) self.request.response.redirect(self.nextURL()) notify(EditCancelledEvent(self.context))
def buttonHandler(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Draft has been saved."), "info") view = 'edit' redirectURL = "%s/%s" % (self.getContent().absolute_url(), view) self.request.response.redirect(redirectURL)
class DefaultAddForm(DexterityExtensibleForm, form.AddForm): """Standard add form, which is wrapped by DefaultAddView (see below). This form is capable of rendering the fields of any Dexterity schema, including behaviours. To do that, needs to know the portal_type, which can be set as a class variable (in a subclass), or on a created instance. By default, the DefaultAddView (see below) will set the portal_type based on the FTI. """ portal_type = None immediate_view = None success_message = _(u"Item created") def __init__(self, context, request, ti=None): super(DefaultAddForm, self).__init__(context, request) if ti is not None: self.ti = ti self.portal_type = ti.getId() self.request['disable_border'] = True @property def additionalSchemata(self): return getAdditionalSchemata(portal_type=self.portal_type) # API def create(self, data): fti = getUtility(IDexterityFTI, name=self.portal_type) container = aq_inner(self.context) content = createObject(fti.factory) # Note: The factory may have done this already, but we want to be sure # that the created type has the right portal type. It is possible # to re-define a type through the web that uses the factory from an # existing type, but wants a unique portal_type! if hasattr(content, '_setPortalTypeName'): content._setPortalTypeName(fti.getId()) # Acquisition wrap temporarily to satisfy things like vocabularies # depending on tools if IAcquirer.providedBy(content): content = content.__of__(container) form.applyChanges(self, content, data) for group in self.groups: form.applyChanges(group, content, data) return aq_base(content) def add(self, object): fti = getUtility(IDexterityFTI, name=self.portal_type) container = aq_inner(self.context) new_object = addContentToContainer(container, object) if fti.immediate_view: self.immediate_view = "/".join( [container.absolute_url(), new_object.id, fti.immediate_view]) else: self.immediate_view = "/".join( [container.absolute_url(), new_object.id]) def nextURL(self): if self.immediate_view is not None: return self.immediate_view else: return self.context.absolute_url() # Buttons @button.buttonAndHandler(_('Save'), name='save') def handleAdd(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return obj = self.createAndAdd(data) if obj is not None: # mark only as finished if we get the new object self._finishedAdd = True IStatusMessage(self.request).addStatusMessage( self.success_message, "info success") @button.buttonAndHandler(_(u'Cancel'), name='cancel') def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( _(u"Add New Item operation cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(AddCancelledEvent(self.context)) def update(self): super(DefaultAddForm, self).update() # fire the edit begun only if no action was executed if len(self.actions.executedActions) == 0: notify(AddBegunEvent(self.context)) def updateActions(self): super(DefaultAddForm, self).updateActions() if 'save' in self.actions: self.actions["save"].addClass("context") if 'cancel' in self.actions: self.actions["cancel"].addClass("standalone") @property def label(self): portal_type = self.portal_type fti = getUtility(IDexterityFTI, name=portal_type) type_name = fti.Title() return _(u"Add ${name}", mapping={'name': type_name})
for interface, names in changes.items(): new_changes.append((interface.__identifier__, names)) uuid = IUUID(content, 0) task_id = utils.register_task(action=constants.EDIT, changes=new_changes, context=uuid) tasks.finish_edit.apply_async([content, task_id], dict()) IStatusMessage(self.request).addStatusMessage(self.success_message, "info") url = content.absolute_url() portal_type = getattr(aq_base(content), "portal_type", None) registry = zope.component.getUtility(IRegistry) use_view_action = registry.get( "plone.types_use_view_action_in_listings", []) if portal_type in use_view_action: url = url + '/view' return self.request.response.redirect(url) @button.buttonAndHandler(_(u"Cancel"), name="cancel") def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(EditCancelledEvent(self.context)) AsyncEditView = layout.wrap_form(AsyncEditForm) classImplements(AsyncEditView, IDexterityEditForm)