def set_value(self, obj, dottedname, field, field_value, attempts=0): """ Set the value for an object's field. Values must be validated as defined in the schema. This can be called recursively as a sort of hack where zope.schema gets finicky between unicode and str. :param obj: the object being modified :param dottedname: non-resolved schema interface :param field: field being changed :param field_value: the new value of the field. In this case, for multi values, it is the entire value :param attempts: recursive attempts made :return: """ attempt_limit = 1 # some of the more common validation problems are unicode where it expects ascii # or vice versa. Try once schema = get_behav(dottedname) bound = schema[field].bind(obj) try: bound.validate(field_value) except WrongType as e: if attempts < attempt_limit: attempts += 1 if isinstance(field_value, str): field_value = str(field_value) return self.set_value(obj, dottedname, field, field_value, attempts) else: api.portal.show_message(message='Failed to validate: %s' % e.__repr__(), request=self.request, type='error') else: setattr(obj, field, field_value) notify(ObjectModifiedEvent(obj)) notify(EditFinishedEvent(obj)) obj.reindexObject()
def handle_preview(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) self.request.response.redirect('nuplone-view') notify(EditFinishedEvent(self.context))
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))
def test_validate_with_package_uninstalled(self): from sc.social.like.config import PROJECTNAME qi = self.portal['portal_quickinstaller'] qi.uninstallProducts(products=[PROJECTNAME]) # should not raise exceptions on publishing with api.env.adopt_roles(['Manager']): api.content.transition(self.news_item, 'publish') # should not raise exceptions on editing notify(EditFinishedEvent(self.news_item))
def handleApply(self, action): data, errors = self.extractData() self.handle_image(data) self.set_custom_embed_code(data) if errors: self.status = self.formErrorsMessage return self.applyChanges(data) api.portal.show_message( _(u'Changes saved.'), self.request, type='info') self.request.response.redirect(self.nextURL()) notify(EditFinishedEvent(self.context))
def test_validate_on_edit_valid(self): with api.env.adopt_roles(['Manager']): api.content.transition(self.news_item, 'publish') # content follows best practices self.news_item.title = get_random_string(70) self.news_item.description = get_random_string(200) set_image_field(self.news_item, load_image(1920, 1080), 'image/png') notify(EditFinishedEvent(self.news_item)) # no feedback messages present messages = IStatusMessage(self.request).show() self.assertEqual(len(messages), 0)
def handleApply(self, action): self.request.response data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) api.portal.show_message( self.success_message, self.request, 'success', ) notify(EditFinishedEvent(self.context))
def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) self.context.collective_immediatecreate = "created" # rename chooser = INameChooser(aq_parent(self.context)) new_id = chooser.chooseName(None, self.context) api.content.rename(obj=self.context, new_id=new_id) api.portal.show_message(self.success_message, self.request) self.request.response.redirect(self.nextURL()) notify(EditFinishedEvent(self.context))
def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage self.request.response.setStatus(400) return manager = queryMultiAdapter((self.context, self.request), ICheckinCheckoutManager) if not manager.is_checked_out_by_current_user(): self.request.response.setStatus(412) return self.applyChanges(data) self.request.response.setStatus(204) notify(EditFinishedEvent(self.context)) self.render_form = False
def test_validate_on_edit_invalid(self): with api.env.adopt_roles(['Manager']): api.content.transition(self.news_item, 'publish') # content don't follow best practices self.news_item.title = get_random_string(80) self.news_item.description = get_random_string(300) set_image_field(self.news_item, load_image(200, 150), 'image/png') notify(EditFinishedEvent(self.news_item)) # no feedback messages present messages = IStatusMessage(self.request).show() self.assertEqual(len(messages), 3) self.assertEqual(messages[0].message, MSG_INVALID_OG_TITLE) self.assertEqual(messages[0].type, u'warning') self.assertEqual(messages[1].message, MSG_INVALID_OG_DESCRIPTION) self.assertEqual(messages[1].type, u'warning') self.assertEqual(messages[2].message, MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS) self.assertEqual(messages[2].type, u'warning')
def test_validate_on_no_workflow(self): # image and files have no associated workflow with api.env.adopt_roles(['Manager']): image = api.content.create(self.portal, 'Image', id='foo') # content don't follow best practices image.title = get_random_string(80) image.description = get_random_string(300) set_image_field(image, load_image(200, 150), 'image/png') notify(EditFinishedEvent(image)) # feedback messages present messages = IStatusMessage(self.request).show() self.assertEqual(messages[0].message, MSG_INVALID_OG_TITLE) self.assertEqual(messages[0].type, u'warning') self.assertEqual(messages[1].message, MSG_INVALID_OG_DESCRIPTION) self.assertEqual(messages[1].type, u'warning') self.assertEqual(messages[2].message, MSG_INVALID_OG_LEAD_IMAGE_DIMENSIONS) self.assertEqual(messages[2].type, u'warning')
def handleApply(self, action): # noqa # Override widget modes to ignore all other fields prefix = 'form.widgets.' field_ids = [k.split(prefix)[-1] for k in self.request.form.keys()] self.request.set('fields', field_ids) # Set all widgets to 'display' to prevent saving data self.set_all_widgets_mode(interfaces.DISPLAY_MODE) # Change the custom_form_fields to input-mode self.set_widgets_mode(field_ids, interfaces.INPUT_MODE) # Original code data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) IStatusMessage(self.request).addStatusMessage( self.success_message, 'info', ) # noqa self.request.response.redirect(self.nextURL()) notify(EditFinishedEvent(self.context))