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 file_field = self.file_field() if data[file_field.getName( )] is None and data['IBasic.title'] is None: message = _( u'input_title_or_add_file', default= u'You need to input title or add file to create this content type.' ) IStatusMessage(self.request).addStatusMessage(message, type='info') else: IStatusMessage(self.request).addStatusMessage( DMF(u"Item created"), "info success")
class AddOrganization(form.AddForm): implements(IFieldsAndContentProvidersForm) contentProviders = ContentProviders(['organization-ms']) contentProviders['organization-ms'].position = 2 label = _(u"Create ${name}", mapping={'name': _(u"organization/position")}) description = u"" prefix = 'oform' fields = field.Fields(IAddContact).select('organization', 'position') def updateWidgets(self): super(AddOrganization, self).updateWidgets() self.widgets['organization'].label = _( 'help_add_organization_or_position_organization', "Please fill the organization first " "and then eventually select position") @button.buttonAndHandler(_('Add'), name='save') def handleAdd(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self._finishedAdd = True if data['position'] is not None: self.request.response.redirect(data['position'].absolute_url()) return elif data['organization'] is not None: self.request.response.redirect(data['organization'].absolute_url()) return @button.buttonAndHandler(DMF(u'Cancel'), name='cancel') def handleCancel(self, action): pass
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( DMF(u"Item created"), "info")
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( DMF(u"Add New Item operation cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(AddCancelledEvent(self.context))
class AddContact(DefaultAddForm, form.AddForm): implements(IFieldsAndContentProvidersForm) contentProviders = ContentProviders(['organization-ms']) # contentProviders['organization-ms'] = MasterSelectAddContactProvider contentProviders['organization-ms'].position = -1 label = _(u"Create ${name}", mapping={'name': _(u"Contact")}) description = _( u"A contact is a position held by a person in an organization") schema = IAddContact portal_type = 'held_position' prefix = 'oform' @property def additionalSchemata(self): fti = getUtility(IDexterityFTI, name=self.portal_type) schema = fti.lookupSchema() # save the schema name to be able to remove a field afterwards self._schema_name = schema.__name__ return (schema, ) def updateFieldsFromSchemata(self): super(AddContact, self).updateFieldsFromSchemata() # IHeldPosition and IAddContact have both a field named position # hide the one from IHeldPosition # TODO: there is no hidden template for autocomplete widget, # we hide it in javascript for now. self.fields[self._schema_name + '.position'].mode = HIDDEN_MODE hp_fti = api.portal.get_tool('portal_types').held_position if IContactDetails.__identifier__ in hp_fti.behaviors: self.fields += field.Fields(IContactDetails) def updateWidgets(self): super(AddContact, self).updateWidgets() for widget in self.widgets.values(): if getattr(widget, 'required', False): # This is really a hack to not have required field errors # but have the visual required nevertheless. # We need to revert this after updateActions # because this change impact the held position form widget.field.required = False if 'parent_address' in self.widgets: self.widgets['parent_address'].mode = DISPLAY_MODE def update(self): super(AddContact, self).update() # revert required field changes for widget in self.widgets.values(): if getattr(widget, 'required', False): widget.field.required = True @button.buttonAndHandler(_('Add'), 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( DMF(u"Item created"), "info") @button.buttonAndHandler(DMF(u'Cancel'), name='cancel') def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( DMF(u"Add New Item operation cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(AddCancelledEvent(self.context)) def createAndAdd(self, data): if data['person'] is None and data['organization'] is None: return elif data['organization'] is not None and data['person'] is None: self.request.response.redirect(data['organization'].absolute_url()) self._finishedAdd = True return elif data['person'] is not None and data['organization'] is None: self.request.response.redirect(data['person'].absolute_url()) self._finishedAdd = True return else: return super(AddContact, self).createAndAdd(data) def create(self, data): self._container = data.pop('person') position = data.pop('position') orga = data.pop('organization') if position is None: position = orga data[self._schema_name + '.position'] = position return super(AddContact, self).create(data) def add(self, obj): container = self._container fti = getUtility(IDexterityFTI, name=self.portal_type) new_object = addContentToContainer(container, obj) if fti.immediate_view: self.immediate_view = "%s/%s/%s" % ( container.absolute_url(), new_object.id, fti.immediate_view, ) else: self.immediate_view = "%s/%s" % (container.absolute_url(), new_object.id)
class AddContact(DefaultAddForm, form.AddForm): """ The following is possible with this AddContact form: - Returning a contact (held position) when you select an organization and a person. - Returning a person when you select only a person - Returning a organization when you select only an organization. It's for this case we want no required errors in the form if the IHeldPosition required fields are not filled. """ implements(IFieldsAndContentProvidersForm) contentProviders = ContentProviders(['organization-ms']) # contentProviders['organization-ms'] = MasterSelectAddContactProvider contentProviders['organization-ms'].position = -1 label = _(u"Create ${name}", mapping={'name': _(u"Contact")}) description = _( u"A contact is a position held by a person in an organization") schema = IAddContact portal_type = 'held_position' prefix = 'oform' allow_prefill_from_GET_request = True @property def additionalSchemata(self): fti = getUtility(IDexterityFTI, name=self.portal_type) schema = fti.lookupSchema() # save the schema name to be able to remove a field afterwards self._schema_name = schema.__name__ return (schema, ) def updateFieldsFromSchemata(self): super(AddContact, self).updateFieldsFromSchemata() hp_fti = api.portal.get_tool('portal_types').held_position if IContactDetails.__identifier__ in hp_fti.behaviors: self.fields += field.Fields(IContactDetails) def updateWidgets(self): super(AddContact, self).updateWidgets() # IHeldPosition and IAddContact have both a field named position # del the widget of the one from IHeldPosition but keep its field del self.widgets[self._schema_name + '.position'] if self.schema != IAddHeldPosition: for widget in self.widgets.values(): if getattr(widget, 'required', False): # copy field to not modify original one widget.field = copy.copy(widget.field) widget.field.required = False if 'parent_address' in self.widgets: self.widgets['parent_address'].mode = DISPLAY_MODE def update(self): super(AddContact, self).update() @button.buttonAndHandler(_('Add'), 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( DMF(u"Item created"), "info") @button.buttonAndHandler(DMF(u'Cancel'), name='cancel') def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( DMF(u"Add New Item operation cancelled"), "info") self.request.response.redirect(self.nextURL()) notify(AddCancelledEvent(self.context)) def createAndAdd(self, data): if data['person'] is None and data['organization'] is None: return elif data['organization'] is not None and data['person'] is None: self.request.response.redirect(data['organization'].absolute_url()) self._finishedAdd = True return elif data['person'] is not None and data['organization'] is None: self.request.response.redirect(data['person'].absolute_url()) self._finishedAdd = True return else: return super(AddContact, self).createAndAdd(data) def create(self, data): self._container = data.pop('person') position = data.pop('position') orga = data.pop('organization') if position is None: position = orga data[self._schema_name + '.position'] = position return super(AddContact, self).create(data) def add(self, obj): container = self._container fti = getUtility(IDexterityFTI, name=self.portal_type) new_object = addContentToContainer(container, obj) if fti.immediate_view: self.immediate_view = "%s/%s/%s" % ( container.absolute_url(), new_object.id, fti.immediate_view, ) else: self.immediate_view = "%s/%s" % (container.absolute_url(), new_object.id)
class AddArchiveForm(add.DefaultAddForm): portal_type = "archive" @instance.memoize def file_field(self): """First file field""" file_field = None for field in self.fields.values(): if isinstance(field.field, NamedBlobFile): file_field = field.field break return file_field @instance.memoize def image_field(self): """First image field""" image_field = None for field in self.fields.values(): if isinstance(field.field, NamedBlobImage): image_field = field.field break return image_field def add(self, object): alsoProvides(object, IArchive) container = aq_inner(self.context) new_object = addContentToContainer(container, object) file_field = self.file_field() if object.title is None and getattr(object, file_field.getName()) is None: container._delObject(new_object.id) self.immediate_view = '/'.join(container.getPhysicalPath()) else: fti = getUtility(IDexterityFTI, name=self.portal_type) 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]) @button.buttonAndHandler(DMF('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 file_field = self.file_field() if data[file_field.getName( )] is None and data['IBasic.title'] is None: message = _( u'input_title_or_add_file', default= u'You need to input title or add file to create this content type.' ) IStatusMessage(self.request).addStatusMessage(message, type='info') else: IStatusMessage(self.request).addStatusMessage( DMF(u"Item created"), "info success") def updateWidgets(self): update_widget(self) super(AddArchiveForm, self).updateWidgets()