class WrappedForm(BaseWizardStepForm, parent_form_class): step_name = 'add-meeting-dossier' step_title = steptitle steps = ADD_MEETING_STEPS label = _(u'Add Dossier for Meeting') passed_data = ['committee-oguid'] @buttonAndHandler(pd_mf(u'Save'), name='save') def handleAdd(self, action): # create the dossier data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return committee_oguid = get_committee_oguid() dossier = self.create_meeting_dossier(data) self.create_meeting(dossier, committee_oguid) api.portal.show_message(_( u"The meeting and its dossier were created successfully"), request=self.request, type="info") committee = committee_oguid.resolve_object() return self.request.RESPONSE.redirect('{}#meetings'.format( committee.absolute_url())) @buttonAndHandler(pd_mf(u'Cancel'), name='cancel') def handleCancel(self, action): committee_oguid = get_committee_oguid() dm = getUtility(IWizardDataStorage) dm.drop_data(get_dm_key(committee_oguid)) committee = committee_oguid.resolve_object() return self.request.RESPONSE.redirect(committee.absolute_url()) def create_meeting_dossier(self, data): obj = self.createAndAdd(data) if obj is not None: # mark only as finished if we get the new object self._finishedAdd = True return obj def create_meeting(self, dossier, committee_oguid): dm = getUtility(IWizardDataStorage) data = dm.get_data(get_dm_key()) data['dossier_oguid'] = Oguid.for_object(dossier) meeting = Meeting(**data) meeting.initialize_participants() session = create_session() session.add(meeting) session.flush() # required to create an autoincremented id dm.drop_data(get_dm_key()) return meeting
def label(self): if IDossierMarker.providedBy(self.context): return _(u'Add Subdossier') else: portal_type = self.portal_type fti = getUtility(IDexterityFTI, name=portal_type) type_name = fti.Title() return pd_mf(u"Add ${name}", mapping={'name': type_name})
class WrappedForm(parent_form_class): skip_validate_file_field = True label = _(u'Add document from officeatwork') def updateFields(self): """Hide the file-field. OfficeConnector creates a file with officeatwork on the desktop and adds it in a later step. """ super(WrappedForm, self).updateFields() hide_fields_from_behavior(self, ['file']) @buttonAndHandler(_(u'Create with officeatwork'), name='save') def handleAdd(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return document = self.createAndAdd(data) if document is None: return self._finishedAdd = True api.portal.show_message( _(u'Creation with officeatwork initiated successfully'), request=self.request, type='info') # temporarily aq-wrap the document to be able to get an url aq_wrapped_doc = document.__of__(self.context) target_url = '{}/create_with_officeatwork'.format( aq_wrapped_doc.absolute_url()) redirector = IRedirector(self.request) redirector.redirect(target_url) return self.request.RESPONSE.redirect('{}#documents'.format( self.context.absolute_url())) def create(self, data): document = super(WrappedForm, self).create(data) self.initialize_in_shadow_state(document) return document def initialize_in_shadow_state(self, document): """Force the initial state to be document-state-shadow.""" document.as_shadow_document() @buttonAndHandler(pd_mf(u'Cancel'), name='cancel') def handleCancel(self, action): return self.request.RESPONSE.redirect( self.context.absolute_url())
def _wrap_form(self, parent_form_class): """ The original form is passed as `parent_form_class` here and is "extended" with the wizard stuff (different template, passing of values from earlier steps, step configuration etc.). This is done by subclassing the original form and overwriting the buttons, since we need to do our custom stuff. """ steptitle = pd_mf(u'Add ${name}', mapping={'name': self.fti.Title()}) form_class = self._create_form_class(parent_form_class, steptitle) form_class.__name__ = 'WizardForm: %s' % parent_form_class.__name__ return form_class
def label(self): if IDossierMarker.providedBy(aq_parent(aq_inner(self.context))): return _(u'Edit Subdossier') else: type_name = self.fti.Title() return pd_mf(u"Edit ${name}", mapping={'name': type_name})
class WrappedForm(CreateDossierMixin, BaseWizardStepForm, parent_form_class): step_name = 'add-dossier-from-template' def update(self): """Update the widget-values of the dossier add-form with the values of the selected dossiertemplate values. """ super(WrappedForm, self).update() if not getSecurityManager().getUser().getId(): # this happens during ++widget++ traversal return self.is_available() template_obj = get_saved_template_obj(self.context) if not template_obj: # This happens if the user access the step directly and # the wizard storage was expired or never existent. return self.request.RESPONSE.redirect( '{}/dossier_with_template'.format( self.context.absolute_url())) template_values = template_obj.get_schema_values() title_help = IDossierTemplateSchema(template_obj).title_help for group in self.groups: for widgetname in group.widgets: # Skip not whitelisted template fields. # We don't want to update fields which are not # whitelisted in the template. template_widget_name = self.get_template_widget_name( widgetname) if template_widget_name not in TEMPLATABLE_FIELDS: continue value = template_values.get(template_widget_name) widget = group.widgets.get(widgetname) # If the current field is the title field and the # title_help is set, we remove the input-value and # add a field description with the title_help text # instead. if widget.field == IOpenGeverBase[ 'title'] and title_help: widget.dynamic_description = title_help value = '' # Set the template value to the dossier add-form widget. widget.value = IDataConverter(widget).toWidgetValue( value) if widgetname == 'IDossier.keywords': self._modify_keyword_widget_according_to_template( widget) def _modify_keyword_widget_according_to_template(self, widget): template_obj = get_saved_template_obj(self.context) if template_obj.restrict_keywords: # since the widget gets somehow reinitialized it's not # possible to manipulate the add_permission directly. # Changing other values like something on a field may # lead to unexpected behavior. # This is the insecure option - but it fits for this # usecase select2_config = json.loads(widget.config_json) select2_config['tags'] = False widget.config_json = json.dumps(select2_config) # The vocabular should only contain the terms from # the template. terms = filter(lambda term: term.token in widget.value, widget.terms.terms._terms) widget.terms.terms = SimpleVocabulary(terms) if not template_obj.predefined_keywords: widget.value = () def get_template_widget_name(self, widgetname): """The dossiertemplates uses the same fields as the dossier (IDossier) but it includes it with another interface. We have to map this two interface names to get the correct value or widget. This function maps an original interface to the DossierTemplate interfaces: Example: IDossier.keywords => IDossierTemplate.keywords """ interface_name, name = widgetname.split('.') return '.'.join([ BEHAVIOR_INTERFACE_MAPPING.get(interface_name, interface_name), name ]) @buttonAndHandler(pd_mf(u'Save'), name='save') def handleAdd(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return container = self.createAndAdd(data) if container is not None: container = self.context.get(container.getId()) template_container = get_saved_template_obj(self.context) with DeactivatedCatalogIndexing(): # While generating content, each newly created object # will be indexed up to 4 times in the creation process. # # This is not necessary and takes a long time. # # Creating all the subdossier without reindexing the catalog # will improve the performance massively and we can manually # reindex the created objects once at the end of the creation # process. self.recursive_content_creation( template_container, container) self.recursive_reindex(container) self._finishedAdd = True api.portal.show_message(PDMF(u"Item created"), request=self.request, type="info") @buttonAndHandler(pd_mf(u'Cancel'), name='cancel') def handleCancel(self, action): return self.request.RESPONSE.redirect( self.context.absolute_url()) def recursive_reindex(self, obj): for child_obj in obj.listFolderContents(): child_obj.reindexObject() if IDexterityContainer.providedBy(child_obj): self.recursive_reindex(child_obj) def recursive_content_creation(self, template_obj, target_container): responsible = IDossier(target_container).responsible for child_obj in template_obj.listFolderContents(): if IDossierTemplateSchema.providedBy(child_obj): dossier = CreateDossierFromTemplateCommand( target_container, child_obj).execute() IDossier(dossier).responsible = responsible self.recursive_content_creation(child_obj, dossier) else: CreateDocumentFromTemplateCommand( target_container, child_obj, child_obj.title).execute()
class ManualJournalEntryAddForm(AddForm): """Provide a z3c.form to enter a manual journal entry.""" label = _(u'label_add_journal_entry', default=u'Add journal entry') fields = Fields(IManualJournalEntry) fields['contacts'].widgetFactory = ParameterizedWidget( KeywordWidget, async=True ) def updateWidgets(self, prefix=None): super(ManualJournalEntryAddForm, self).updateWidgets(prefix=prefix) if not is_contact_feature_enabled(): self.widgets['contacts'].mode = HIDDEN_MODE @button.buttonAndHandler(z3c_mf('Add'), name='add') 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 @button.buttonAndHandler(pd_mf(u'Cancel'), name='cancel') def handleCancel(self, action): return self.request.RESPONSE.redirect( '{}#journal'.format(self.context.absolute_url())) def updateActions(self): super(ManualJournalEntryAddForm, self).updateActions() self.actions['add'].addClass("context") def createAndAdd(self, data): contacts, users = self.split_contacts_and_users(data.get('contacts')) entry = ManualJournalEntry(self.context, data.get('category'), data.get('comment'), contacts, users, data.get('related_documents')) entry.save() return entry def split_contacts_and_users(self, items): """Spliting up the contact list, in to a list of contact objects and a list of adapted users. """ contacts = [] users = [] for item in items: if item.is_adapted_user: users.append(item) else: contacts.append(item) return contacts, users def nextURL(self): return '{}#journal'.format(self.context.absolute_url())
def _wrap_form(self, formclass): # The original form is passed as `formclass` here and is "extended" # with the wizard stuff (different template, passing of values from # earlier steps, step configuration etc.). This is done by # subclassing the original form and overwriting the buttons, since # we need to do our custom stuff instead of the default dossier # creation. steptitle = pd_mf(u'Add ${name}', mapping={'name': self.ti.Title()}) class WrappedForm(AcceptWizardNewDossierFormMixin, formclass): step_name = 'accept_dossier_add_form' passed_data = ['oguid', 'dossier_type'] step_title = steptitle @buttonAndHandler(_(u'button_save', default=u'Save'), name='save') def handleAdd(self, action): # create the dossier 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 # Get a properly aq wrapped object dossier = self.context.get(obj.id) dm = getUtility(IWizardDataStorage) oguid = self.request.get('oguid') dmkey = 'accept:%s' % oguid # forwarding if dm.get(dmkey, 'is_forwarding'): if dm.get(dmkey, 'is_only_assign'): task = assign_forwarding_to_dossier( self.context, oguid, dossier, dm.get( dmkey, 'text')) IStatusMessage(self.request).addStatusMessage( _(u'The forwarding is now assigned to the new ' 'dossier'), 'info') self.request.RESPONSE.redirect( '%s/edit' % task.absolute_url()) else: task = accept_forwarding_with_successor( self.context, oguid, dm.get(dmkey, 'text'), dossier=dossier) IStatusMessage(self.request).addStatusMessage( _(u'The forwarding has been stored in the ' u'local inbox and the succesor task has been' u' created'), 'info') self.request.RESPONSE.redirect( '%s/edit' % task.absolute_url()) else: # create the successor task, accept the predecessor task = accept_task_with_successor( dossier, oguid, dm.get(dmkey, 'text')) IStatusMessage(self.request).addStatusMessage( _(u'The new dossier has been created and the task ' u'has been copied to the new dossier.'), 'info') self.request.RESPONSE.redirect(task.absolute_url()) @buttonAndHandler(dexterityMF(u'Cancel'), name='cancel') def handleCancel(self, action): portal_url = getToolByName(self.context, 'portal_url') url = '%s/resolve_oguid?oguid=%s' % ( portal_url(), self.request.get('oguid')) return self.request.RESPONSE.redirect(url) WrappedForm.__name__ = 'WizardForm: %s' % formclass.__name__ return WrappedForm
def _wrap_form(self, formclass): # The original form is passed as `formclass` here and is "extended" # with the wizard stuff (different template, passing of values from # earlier steps, step configuration etc.). This is done by # subclassing the original form and overwriting the buttons, since # we need to do our custom stuff instead of the default dossier # creation. steptitle = pd_mf(u'Add ${name}', mapping={'name': self.ti.Title()}) class WrappedForm(AcceptWizardNewDossierFormMixin, formclass): step_name = 'accept_dossier_add_form' passed_data = ['oguid', 'dossier_type'] step_title = steptitle @buttonAndHandler(_(u'button_save', default=u'Save'), name='save') def handleAdd(self, action): # create the dossier 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 # Get a properly aq wrapped object dossier = self.context.get(obj.id) dm = getUtility(IWizardDataStorage) oguid = self.request.get('oguid') dmkey = 'accept:%s' % oguid # forwarding if dm.get(dmkey, 'is_forwarding'): if dm.get(dmkey, 'is_only_assign'): task = assign_forwarding_to_dossier( self.context, oguid, dossier, dm.get(dmkey, 'text')) IStatusMessage(self.request).addStatusMessage( _(u'The forwarding is now assigned to the new ' 'dossier'), 'info') self.request.RESPONSE.redirect('%s/edit' % task.absolute_url()) else: task = accept_forwarding_with_successor( self.context, oguid, dm.get(dmkey, 'text'), dossier=dossier) IStatusMessage(self.request).addStatusMessage( _(u'The forwarding has been stored in the ' u'local inbox and the succesor task has been' u' created'), 'info') self.request.RESPONSE.redirect('%s/edit' % task.absolute_url()) else: # create the successor task, accept the predecessor task = accept_task_with_successor(dossier, oguid, dm.get(dmkey, 'text')) IStatusMessage(self.request).addStatusMessage( _(u'The new dossier has been created and the task ' u'has been copied to the new dossier.'), 'info') self.request.RESPONSE.redirect(task.absolute_url()) @buttonAndHandler(dexterityMF(u'Cancel'), name='cancel') def handleCancel(self, action): portal_url = getToolByName(self.context, 'portal_url') url = '%s/resolve_oguid?oguid=%s' % (portal_url(), self.request.get('oguid')) return self.request.RESPONSE.redirect(url) WrappedForm.__name__ = 'WizardForm: %s' % formclass.__name__ return WrappedForm