class SetFieldEditForm(EditForm): """An edit form for local roles action.""" form_fields = form.FormFields(ISetFieldAction) label = _(u"Edit a Set Field Action") description = _(u"An action for setting the value of a field on an object." ) # noqa: E501 schema = ISetFieldAction
class SetFieldAddForm(AddForm): """An add form for local roles action.""" form_fields = form.FormFields(ISetFieldAction) label = _(u"Add a Set Field Value Action") description = _(u"An action for setting the value of a field on an object." ) # noqa: E501 schema = ISetFieldAction def create(self, data): a = SetFieldAction() form.applyChanges(a, self.form_fields, data) return a
def __call__(self): self.obj = self.event.object self.value_script = getattr(self.element, "value_script", "") self.update_all = getattr(self.element, "update_all", "object") self.preserve_modification_date = getattr( self.element, "preserve_modification_date", False) self.conditions = self.get_conditions() self.portal = api.portal.get() try: objects = self.get_objects() except Exception as e: # noqa:B902 self.error(self.obj, e) return False for item in objects: # Test the object against the conditions before updating passing = True if item != self.obj: self.event.object = item for condition in self.conditions: executable = getMultiAdapter((item, condition, self.event), IExecutable) if not executable(): passing = False break if passing is False: logger.warning("Not executing setField action on %s" % item) # noqa: E501 continue old_date = None if self.preserve_modification_date is True: old_date = item.modification_date self.process_script(item) if self.preserve_modification_date is True: item.modification_date = old_date item.reindexObject(idxs="modified") # If there are < 5 warnings, display them as messages. Otherwise we # set a more generic message & point the user to the zope logs. if self.request is not None and len(self.warnings) > 0: if len(self.warnings) < 6: for warning in self.warnings: IStatusMessage(self.request).addStatusMessage(warning, type="warn") else: IStatusMessage(self.request).addStatusMessage( _(u"%i objects could not be updated. Please see the debug" # noqa: E501 u"logs for more information." % len(self.warnings)), type="warn", ) return True
def error(self, obj_being_processed, error): title = pretty_title_or_id( obj_being_processed, obj_being_processed, ) message = _( u"Unable to set values on %s: %s, %s" % (title, str(type(error)), error), ) logger.error(message) if self.request is not None: IStatusMessage(self.request).addStatusMessage( message, type="error") # noqa: E501
class ISetFieldAction(model.Schema): value_script = schema.Text( title=_(u"Value Script"), description=_(u"Enter PythonScript to calculate the values you want to" u" set. Return a dictionary of {'field': value}. " u"Available variables are: context, state, history, " u"event"), default=_(u"""# values = {'field': some_value}"""), required=True, ) update_all = schema.Choice( title=_("Update all content?"), description=_(u"Walks the site to update all matching content as well " u"as the item that triggered the content rule. May take" u"a long time to complete depending on how many objects" u"need to be updated."), vocabulary=SimpleVocabulary(terms), default=u"object", ) preserve_modification_date = schema.Bool( title=_("Preserve modification date?"), description=_( "Does not change the modification date. Beware that the action" " that triggered the rule may update the modification date and" " this setting will not override it"), ) model.fieldset( "script", label=_(u"Script"), fields=["value_script"], ) model.fieldset( "advanced", label=_(u"Advanced"), fields=["update_all", "preserve_modification_date"], )
def summary(self): return _(u"Set field values")
def warn(self, url, warning): message = _(u"Unable to update %s - %s: %s" % (url, str(type(warning)), warning) # noqa: E501 ) logger.warn(message) self.warnings.append(message)