class AddForm(form.Form): template = viewpagetemplatefile.ViewPageTemplateFile('crud-add.pt') label = _(u"Add") ignoreContext = True ignoreRequest = True @property def prefix(self): parent_prefix = getattr(self.context, 'prefix', '') return 'crud-add.' + parent_prefix @property def fields(self): return field.Fields(self.context.add_schema) @button.buttonAndHandler(_('Add'), name='add') def handle_add(self, action): data, errors = self.extractData() if errors: self.status = form.AddForm.formErrorsMessage return try: item = self.context.add(data) except zope.schema.ValidationError, e: self.status = e else:
def handle_edit(self, action): success = _(u"Successfully updated") partly_success = _(u"Some of your changes could not be applied.") status = no_changes = _(u"No changes made.") for subform in self.subforms: # With the ``extractData()`` call, validation will occur, # and errors will be stored on the widgets amongst other # places. After this we have to be extra careful not to # call (as in ``__call__``) the subform again, since # that'll remove the errors again. With the results that # no changes are applied but also no validation error is # shown. data, errors = subform.extractData() if errors: if status is no_changes: status = subform.formErrorsMessage elif status is success: status = partly_success continue del data["select"] self.context.before_update(subform.content, data) changes = subform.applyChanges(data) if changes: if status is no_changes: status = success elif status is subform.formErrorsMessage: status = partly_success # If there were changes, we'll update the view widgets # again, so that they'll actually display the changes for widget in subform.widgets.values(): if widget.mode == DISPLAY_MODE: widget.update() zope.event.notify(z3c.form.widget.AfterWidgetUpdateEvent(widget)) self.status = status
def _select_field(self): select_field = field.Field( zope.schema.Bool(__name__='select', required=False, title=_(u'select'))) select_field.widgetFactory[INPUT_MODE] = singlecheckboxwidget_factory return select_field
def __call__(self): pages = [] batch = self.context if batch.total == 1: return u"" else: if batch.number > 1: pages.append(dict(label=_("Previous"), link=self.make_link(page=batch.number - 2))) for index in range(batch.total): link = index != batch.number - 1 and self.make_link(page=index) or None pages.append(dict(label=unicode(index + 1), link=link)) if batch.number < batch.total: pages.append(dict(label=_("Next"), link=self.make_link(page=batch.number))) return self.template(batch=batch, pages=pages)
def handle_delete(self, action): selected = self.selected_items() if selected: self.status = _(u"Successfully deleted items.") for id, item in selected: try: self.context.remove((id, item)) except ConflictError: raise except: # In case an exception is raised, we'll catch it # and notify the user; in general, this is # unexpected behavior: self.status = _(u"Unable to remove one or more items.") break # We changed the amount of entries, so we update the subforms again. self._update_subforms() else: self.status = _(u"Please select items to delete.")
def handle_delete(self, action): selected = self.selected_items() if selected: self.status = _(u"Successfully deleted items.") for id, item in selected: try: self.context.remove((id, item)) except ConflictError: raise except: # In case an exception is raised, we'll catch it # and notify the user; in general, this is # unexpected behavior: self.status = _(u'Unable to remove one or more items.') break # We changed the amount of entries, so we update the subforms again. self._update_subforms() else: self.status = _(u"Please select items to delete.")
def handle_add(self, action): data, errors = self.extractData() if errors: self.status = form.AddForm.formErrorsMessage return try: item = self.context.add(data) except zope.schema.ValidationError as e: self.status = e else: zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(item)) self.status = _(u"Item added successfully.")
class ExtensibleForm(GroupForm): groups = [] default_fieldset_label = _(u"Default") def update(self): self.updateFields() super(ExtensibleForm, self).update() def updateFields(self): extenders = getAdapters((self.context, self.request, self), IFormExtender) for name, extender in sorted(extenders, key=order_key): extender.update()
def handle_edit(self, action): success = _(u"Successfully updated") partly_success = _(u"Some of your changes could not be applied.") status = no_changes = _(u"No changes made.") for subform in self.subforms: # With the ``extractData()`` call, validation will occur, # and errors will be stored on the widgets amongst other # places. After this we have to be extra careful not to # call (as in ``__call__``) the subform again, since # that'll remove the errors again. With the results that # no changes are applied but also no validation error is # shown. data, errors = subform.extractData() if errors: if status is no_changes: status = subform.formErrorsMessage elif status is success: status = partly_success continue del data['select'] self.context.before_update(subform.content, data) changes = subform.applyChanges(data) if changes: if status is no_changes: status = success elif status is subform.formErrorsMessage: status = partly_success # If there were changes, we'll update the view widgets # again, so that they'll actually display the changes for widget in subform.widgets.values(): if widget.mode == DISPLAY_MODE: widget.update() zope.event.notify( z3c.form.widget.AfterWidgetUpdateEvent(widget)) self.status = status
def fields(self): return field.Fields(self.context.add_schema) @button.buttonAndHandler(_("Add"), name="add") def handle_add(self, action): data, errors = self.extractData() if errors: self.status = form.AddForm.formErrorsMessage return try: item = self.context.add(data) except zope.schema.ValidationError, e: self.status = e else: zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(item)) self.status = _(u"Item added successfully.") class NullForm(object): def __init__(self, context, request): self.context = context self.request = request def update(self): pass def render(self): return "" __call__ = render
class EditForm(form.Form): label = _(u"Edit") template = viewpagetemplatefile.ViewPageTemplateFile('crud-table.pt') #exposes the edit sub form for your own derivatives editsubform_factory = EditSubForm @property def prefix(self): parent_prefix = getattr(self.context, 'prefix', '') return 'crud-edit.' + parent_prefix def update(self): self._update_subforms() super(EditForm, self).update() def _update_subforms(self): self.subforms = [] for id, item in self.batch: subform = self.editsubform_factory(self, self.request) subform.content = item subform.content_id = id subform.update() self.subforms.append(subform) @property def batch(self): items = self.context.get_items() batch_size = self.context.batch_size or sys.maxint page = int(self.request.get('%spage' % self.prefix, 0)) return Batch.fromPagenumber(items, batch_size, page + 1) def render_batch_navigation(self): bv = CrudBatchView(self.context, self.request) bv.prefix = self.prefix return bv(self.batch) @button.buttonAndHandler(_('Apply changes'), name='edit', condition=lambda form: form.context.update_schema) def handle_edit(self, action): success = _(u"Successfully updated") partly_success = _(u"Some of your changes could not be applied.") status = no_changes = _(u"No changes made.") for subform in self.subforms: # With the ``extractData()`` call, validation will occur, # and errors will be stored on the widgets amongst other # places. After this we have to be extra careful not to # call (as in ``__call__``) the subform again, since # that'll remove the errors again. With the results that # no changes are applied but also no validation error is # shown. data, errors = subform.extractData() if errors: if status is no_changes: status = subform.formErrorsMessage elif status is success: status = partly_success continue del data['select'] self.context.before_update(subform.content, data) changes = subform.applyChanges(data) if changes: if status is no_changes: status = success elif status is subform.formErrorsMessage: status = partly_success # If there were changes, we'll update the view widgets # again, so that they'll actually display the changes for widget in subform.widgets.values(): if widget.mode == DISPLAY_MODE: widget.update() zope.event.notify( z3c.form.widget.AfterWidgetUpdateEvent(widget)) self.status = status @button.buttonAndHandler(_('Delete'), name='delete') def handle_delete(self, action): selected = self.selected_items() if selected: self.status = _(u"Successfully deleted items.") for id, item in selected: try: self.context.remove((id, item)) except ConflictError: raise except: # In case an exception is raised, we'll catch it # and notify the user; in general, this is # unexpected behavior: self.status = _(u'Unable to remove one or more items.') break # We changed the amount of entries, so we update the subforms again. self._update_subforms() else: self.status = _(u"Please select items to delete.") def selected_items(self): tuples = [] for subform in self.subforms: data = subform.widgets['select'].extract() if not data or data is NOVALUE: continue else: tuples.append((subform.content_id, subform.content)) return tuples
def fields(self): return field.Fields(self.context.add_schema) @button.buttonAndHandler(_('Add'), name='add') def handle_add(self, action): data, errors = self.extractData() if errors: self.status = form.AddForm.formErrorsMessage return try: item = self.context.add(data) except zope.schema.ValidationError, e: self.status = e else: zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(item)) self.status = _(u"Item added successfully.") class NullForm(object): def __init__(self, context, request): self.context = context self.request = request def update(self): pass def render(self): return '' __call__ = render