def render(self): try: getValidationErrors(interfaces.IBiblatexEntry, self.context) except Exception: return u'<span class="error schema-validation-error">S</span>' else: return u''
def __init__(self, context): logging.Filter.__init__(self) errs = getValidationErrors(ICheckContext, context) if errs: raise ValueError('Not a proper context: %r' %(errs)) self.context = context
def verify(self): errors = schema.getValidationErrors( [a for a in implementedBy(self.__class__)][0], self) if errors: raise schema.ValidationError(errors)
def test_historyevent_invariant(self): """ Exercise the different validation options fo HistoryEvent Objects. """ # Duration events with no end date are invalid hevent = HistoryEvent() hevent.title = u"95th World Congress of Esperanto" hevent.durationEvent = True hevent.start = u"Jul 17 2010 00:00:00 GMT-0600" errors = schema.getValidationErrors(IHistoryEvent, hevent) self.assertGreater(len(errors), 0) # adding the missing piece of data makes the validator happy hevent.end = u"JFeb 11 2012 00:00:00 GMT-0600" errors = schema.getValidationErrors(IHistoryEvent, hevent) self.assertEqual(len(errors), 0)
def test_instance_conforms_to_IPermission(self): from zope.interface.verify import verifyObject from zope.security.interfaces import IPermission from zope.schema import getValidationErrors verifyObject(IPermission, self._makeOne('testing')) self.assertEqual([], getValidationErrors(IPermission, self._makeOne('testing')))
def test_classification_edit_acceptable(self): second_classification_folder = api.content.create( container=self.parent, type="ClassificationFolder", id="classification_folder_2", title=u"Folder 2", classification_categories=[self.category.UID()], internal_reference_no=u"future acceptable", ) errors = getValidationErrors(IClassificationFolder, second_classification_folder) self.assertEquals(len(errors), 0) second_classification_folder.internal_reference_no = u"still acceptable" errors = getValidationErrors(IClassificationFolder, second_classification_folder) self.assertEquals(len(errors), 0)
def _validatePrincipal(self, principal): from zope.security.interfaces import IPrincipal from zope.interface.verify import verifyObject from zope.schema import getValidationErrors self.assertTrue(verifyObject(IPrincipal, principal)) errors = getValidationErrors(IPrincipal, principal) self.assertEqual([], errors)
def test_interfaces__IField__2(field): """Using the `choice` type requires values to be added.""" field.type = u'Choice' errors = getValidationErrors(IField, field) assert 1 == len(errors) assert errors[0][0] is None assert isinstance(errors[0][1], Invalid) assert ((u'type "choice" requires at least one field value.', ) == errors[0][1].args)
def create(cls, gc, **kwargs): factory = namedtuple('Configuration', kwargs.keys()) configuration = factory(**kwargs) if cls.check_configuration is not None: errors = [] for iface in cls.check_configuration: errors.extends(getValidationErrors(iface, configuration)) if errors: raise RuntimeError('Errors occured: %s' % ', '.join(errors)) return cls(configuration)
def isValid(self): """Check if the response is valid. Meaning: a response has been filled in. """ errors = schema.getValidationErrors(IIssue, self) if errors: return False else: return True
def push_data(self): data = self.request.get('json', '[]') data_list = json.loads(data) names = getFieldNamesInOrder(IAnalysisResults) names.remove('uuid') names.remove('sample_no') success_count = 0 for entry in data_list: return_obj = ReturnData() [setattr(return_obj, key, entry[key]) for key in entry] errors = schema.getValidationErrors(IAnalysisResults, return_obj) if errors: msg = [] for error in errors: error_type = error[1].__class__.__name__ field_value = unicode(error[1]) err = u'{0}: {1}; '.format(error_type, field_value) msg.append(err) msg = ''.join(msg) entry.update(dict(_back_success=False, _back_message=msg)) logger.warn(msg) continue else: uuid = entry['uuid'] obj = api.content.uuidToObject(uuid) if obj: [setattr(obj, k, entry[k]) for k in entry if k in names] old_steps = obj.steps new_steps = u'step5' obj.steps = new_steps notify(StepsChangedEvent(obj, old_steps, new_steps)) modified(obj) try: api.content.transition(obj=obj, transition='submit') except (MissingParameterError, InvalidParameterError) as error: msg = 'Warning, {0}'.format(str(error)) logger.warn(error) else: msg = 'Success.' success_count += 1 entry.update(dict(_back_success=True, _back_message=msg)) else: msg = u'Warning, ' \ u'uuid "{0}" not found, skipping.'.format(uuid) entry.update(dict(_back_success=False, _back_message=msg)) logger.warn(msg) failed_count = len(data_list) - success_count msg = u'Analysis of Results: {0} Item Succeeded, ' \ u'{1} Item Failed.'.format(success_count, failed_count) logger.info(msg) return json.dumps(data_list)
def test_valid(self): from zope.interface.verify import verifyObject from zope.schema import getValidationErrors schema = self._getTargetInterface() group = self._getTargetClass()("id", "title", "desc") self.assertTrue(verifyObject(schema, group)) errors = getValidationErrors(schema, group) self.assertEqual([], errors)
def validate_attribute_values(schema, values): obj = namedtuple('ImportObject', values.keys())(**values) for error in getValidationErrors(schema, obj): column = schema[error[0]].title if isinstance(error[1], (SchemaNotFullyImplemented, RequiredMissing)): raise ContentImportError( _(u'Required column is missing'), colname=column ) else: raise ContentImportError(error, colname=column)
def _extract_metadata(context, action): gsm = component.getGlobalSiteManager() # is this the right way to fetch the registered Interface ? interfaces = [interface for interfaceName, interface in gsm.getUtilitiesFor(IMetadata)] metadataInterface = interfaces[0] metadata = component.getAdapter(context, metadataInterface, name=action) errors = schema.getValidationErrors(IMetadata, metadata) if errors: raise interface.Invalid(errors) payload = serialize(metadataInterface, metadata) return payload
def _validate_after_update(self, iface, ext_self): errors = schema.getValidationErrors(iface, ext_self) if errors: __traceback_info__ = errors try: raise errors[0][1] except SchemaNotProvided as e: # pragma: no cover # XXX: We shouldn't be able to get here; # ext_setattr should be doing this # This can probably be removed if not e.args: # zope.schema doesn't fill in the details, which sucks e.args = (errors[0][0], ) raise
def test_zcml_valid(self): from zope.configuration import xmlconfig import zope.security from zope.interface.verify import verifyObject from zope.security.interfaces import IPermission from zope.schema import getValidationErrors xmlconfig.file('configure.zcml', zope.security) vocabulary = self._callFUT() vocabulary = sorted(vocabulary, key=lambda term: term.token) self.assertEqual(6, len(vocabulary)) for term in vocabulary: p = term.value __traceback_info__ = term.token, p verifyObject(IPermission, p) self.assertEqual([], getValidationErrors(IPermission, p))
def describe_mismatch(self, item, mismatch_description): x = None md = mismatch_description md.append_text(str(type(item))) errors = getValidationErrors(self.iface, item) for attr, exc in errors: try: raise exc except ValidationError: md.append_text(' has attribute "') md.append_text(attr) md.append_text('" with error "') md.append_text(repr(exc)) md.append_text('"\n\t ') except Invalid as x: # pragma: no cover md.append_text(str(x).replace('\n', ''))
def process_action(self, environ, datacls): params = self.extract(environ) fields = list(extract_fields(self.fields, params)) data = datacls(*fields) errors = getValidationErrors(self.iface, data) nb_errors = len(errors) if nb_errors: summary = {} for field, error in errors: doc = getattr(error, 'doc', error.__str__) field_errors = summary.setdefault(field, []) field_errors.append(doc()) return reply(400, text=json.dumps(summary), content_type="application/json") return data
def validate(self, data, **kw): pdb = kw.get('pdb', None) cdata = self.to_event_values(data, pdb=pdb) obj = Dummy(**cdata) alsoProvides(obj , i.ILiberticEventMapping) def filtered(value): ret = False fn, ferror = value if fn in not_settable: ret = True if not i.ILiberticEventMapping[fn].required and isinstance(ferror, SchemaNotFullyImplemented): ret = True return ret errors = [a for a in schema.getValidationErrors(i.ILiberticEventMapping, obj) if not filtered(a) ] if errors: raise schema.ValidationError(errors) return cdata
def _callFUT(self, schema, object): from zope.schema import getValidationErrors return getValidationErrors(schema, object)
def import_data(self, import_step=u'step1'): simulation_mode = self.request.form.get('form.widgets.simulation') self.request.form['form.widgets.simulation'] = simulation_mode if simulation_mode: msg = _(u'Dry run mode') api.portal.show_message(msg, self.request, 'info') all_true_mode = self.request.form.get('form.widgets.all_true') self.request.form['form.widgets.all_true'] = all_true_mode if all_true_mode: msg = _(u'Force all success') api.portal.show_message(msg, self.request, 'info') upload_file = self.request.form.get('form.widgets.excelFile', None) if upload_file is None or not upload_file.filename: msg = _(u'No files were selected.') api.portal.show_message(msg, self.request, 'warn') return self.index() self.request['form.widgets.filename'] = upload_file.filename steps_schema = ( IBloodSample, INAExtraction, ILibraryConstruction, IComputerDetecting, IAnalysisResults, ) valid_steps = ( u'step1', u'step2', u'step3', u'step4', u'step5', ) query_param = ( {}, { 'portal_type': 'Tumour', 'steps': u'step1', 'sort_on': 'modified', 'sort_order': 'descending' }, { 'portal_type': 'Tumour', 'steps': u'step2', 'sort_on': 'modified', 'sort_order': 'descending' }, { 'portal_type': 'Tumour', 'steps': u'step3', 'sort_on': 'modified', 'sort_order': 'descending' }, { 'portal_type': 'Tumour', 'steps': u'step4', 'sort_on': 'modified', 'sort_order': 'descending' }, { 'portal_type': 'Tumour', 'sort_on': 'modified', 'sort_order': 'descending' }, ) step_index = valid_steps.index(import_step) step_schema = steps_schema[step_index] start, end = utils.steps_start_end[step_index] query_type = { 'portal_type': 'Tumour', 'sort_on': 'modified', 'sort_order': 'descending' } query_type_steps = query_param[step_index] try: workbook = open_workbook(file_contents=upload_file.read()) except Exception: msg = _(u'File type not supported.') api.portal.show_message(msg, self.request, 'error') return self.index() sheet = workbook.sheet_by_index(0) data_cols = utils.import_len if sheet.ncols != data_cols: msg = translate(_( u'Excel data format error. ' u'Please correct it and retry. ' u'Error: expected ${expected} - got ${got}.', mapping={ 'expected': data_cols, 'got': sheet.ncols }), context=self.request) api.portal.show_message(msg, self.request, 'error') return self.index() fields_name = utils.fields_name() fields_title = utils.fields_title() step_fields = utils.fields()[start:end] step_fields_name = fields_name[start:end] feedback_title = [ _(u'Success or failure'), _(u'Line number'), _(u'Execution results') ] + fields_title[start:end] generator = getUtility(IUUIDGenerator) gid = generator() feedback_row = [] success_count = 0 starttime = datetime.now() msg = 'Start importing {0} tumour objects at {1}'.format( sheet.nrows - 2, starttime) logger.info(msg) sample_title = fields_title[0] sample_no_unique = [] for row_index in range(2, sheet.nrows): row_value = sheet.row_values(row_index) sample_value = row_value[0] step_value = row_value[start:end] row_buffer = [logging.NOTSET, row_index + 1, ''] step_obj = DummyStep() for index, field in enumerate(step_fields): if not step_value[index]: step_value[index] = None elif field[1].__class__ is schema.Date: try: date_value = xldate_as_tuple(step_value[index], workbook.datemode) step_value[index] = date(*date_value[:3]) except (ValueError, TypeError): row_buffer[0] = logging.ERROR elif field[1].__class__ is schema.Datetime: try: date_value = xldate_as_tuple(step_value[index], workbook.datemode) step_value[index] = datetime(*date_value[:6]) except (ValueError, TypeError): row_buffer[0] = logging.ERROR elif field[1].__class__ is schema.Int: try: step_value[index] = int(step_value[index]) except (ValueError, TypeError): row_buffer[0] = logging.ERROR elif field[1].__class__ is schema.Float: try: step_value[index] = float(step_value[index]) except (ValueError, TypeError): row_buffer[0] = logging.ERROR elif field[1].__class__ is schema.URI: try: step_value[index] = str(step_value[index]) except (ValueError, TypeError): row_buffer[0] = logging.ERROR elif field[1].__class__ is schema.Choice: if field[0] == 'result': title_value_mapping = invert_result_local value = unicode(step_value[index]) if value in title_value_mapping: step_value[index] = title_value_mapping[value] else: step_value[index] = value elif field[1].__class__ is schema.List: if field[0] == 'treatment_situation': title_value_mapping = invert_situation_local elif field[0] == 'na_extraction_type': title_value_mapping = invert_na_types_local value = unicode(step_value[index]).split(',') step_value[index] = [] for item in value: if item in title_value_mapping: step_value[index].append(title_value_mapping[item]) else: step_value[index].append(item) elif field[1].__class__.__name__ in ('NamedBlobImage', 'NamedBlobFile'): try: step_value[index] = str(step_value[index]).strip() except (ValueError, TypeError): row_buffer[0] = logging.ERROR else: value = step_value[index] if isinstance(value, float) and value - int(value) == 0.0: step_value[index] = int(value) step_value[index] = unicode(step_value[index]).strip() setattr(step_obj, field[0], step_value[index]) value = sample_value if isinstance(value, float) and value - int(value) == 0.0: sample_value = int(value) sample_value = unicode(sample_value).strip() setattr(step_obj, 'sample_no', sample_value) errors = schema.getValidationErrors(step_schema, step_obj) if errors: self.extract_error(errors, row_buffer, step_schema) if sample_value in sample_no_unique: msg = translate(_( u'ValuesError: ' u'${sample_title} ${sample_value} are not unique; ', mapping={ 'sample_title': sample_title, 'sample_value': sample_value }), context=self.request) row_buffer[0] = logging.ERROR row_buffer[2] += msg else: sample_no_unique.append(sample_value) row_buffer += step_value if row_buffer[0] == logging.ERROR: feedback_row.append(row_buffer) continue brains_sample = api.content.find( **merge_two_dicts(query_type, {'sample_no': sample_value})) if import_step == valid_steps[0]: if brains_sample: msg = translate(_( u'ValuesError: ' u'${sample_title} ${sample_value} already exist; ', mapping={ 'sample_title': sample_title, 'sample_value': sample_value }), context=self.request) row_buffer[0] = logging.ERROR row_buffer[2] += msg feedback_row.append(row_buffer) continue validate_obj = step_obj invariant_errors = [] try: ITumour.validateInvariants(validate_obj, invariant_errors) except zope.interface.exceptions.Invalid: pass except AttributeError: pass errors = [(None, e) for e in invariant_errors] invalid = False for error in errors: if error[0] is None: msg = error[1].args[0] msg = translate(_(msg), context=self.request) row_buffer[2] += msg invalid = True if invalid: row_buffer[0] = logging.ERROR feedback_row.append(row_buffer) continue if simulation_mode: obj = step_obj obj.gid = gid else: today = datetime.today() year, month, day = today.year, today.month, today.day container = make_folder(self.aq_parent, (year, month, day)) obj = createContentInContainer(container, portal_type='Tumour') map(lambda k, v: setattr(obj, k, v), step_fields_name, step_value) obj.steps = import_step obj.title = unicode(sample_value) obj.gid = gid modified(obj) msg = translate(_( u"Import successful: " u"${sample_title} ${sample_value}; ", mapping={ 'sample_title': sample_title, 'sample_value': sample_value }), context=self.request) if row_buffer[0] < logging.INFO: row_buffer[0] = logging.INFO row_buffer[2] += msg feedback_row.append(row_buffer) elif import_step in valid_steps: if not brains_sample: msg = translate(_( u"ValuesError: " u"${sample_title} ${sample_value} does't exist; ", mapping={ 'sample_title': sample_title, 'sample_value': sample_value }), context=self.request) row_buffer[0] = logging.ERROR row_buffer[2] += msg feedback_row.append(row_buffer) continue brains_steps = api.content.find(**merge_two_dicts( query_type_steps, {'sample_no': sample_value})) if not brains_steps: expected_steps = translate(_(query_type_steps['steps']), context=self.request) got_steps = translate(_(brains_sample[0].steps), context=self.request) msg = translate(_( u'StepsError: ' u'steps expected ${expected}, got ${got}; ', mapping={ 'expected': expected_steps, 'got': got_steps }), context=self.request) row_buffer[0] = logging.ERROR row_buffer[2] += msg feedback_row.append(row_buffer) continue validate_obj = brains_sample[0].getObject() invalid = False for the_field, the_value in zip(step_fields, step_value): field_type = the_field[1].__class__.__name__ field_name = the_field[0] if field_type in ('NamedBlobImage', 'NamedBlobFile'): if not simulation_mode and the_value != '': success = self.fetch_data(the_field, the_value, validate_obj, row_buffer) if not success: invalid = True else: setattr(validate_obj, field_name, None) else: setattr(validate_obj, field_name, the_value) if invalid: row_buffer[0] = logging.ERROR feedback_row.append(row_buffer) continue invariant_errors = [] try: ITumour.validateInvariants(validate_obj, invariant_errors) except zope.interface.exceptions.Invalid: pass except AttributeError: pass errors = [(None, e) for e in invariant_errors] invalid = False for error in errors: if error[0] is None: msg = error[1].args[0] msg = translate(_(msg), context=self.request) row_buffer[2] += msg invalid = True if invalid: row_buffer[0] = logging.ERROR feedback_row.append(row_buffer) continue if not simulation_mode: obj = validate_obj old_steps = obj.steps obj.steps = import_step notify(StepsChangedEvent(obj, old_steps, import_step)) modified(obj) msg = translate(_( u'Import successful: ' u'${sample_title} ${sample_value}; ', mapping={ 'sample_title': sample_title, 'sample_value': sample_value }), context=self.request) if row_buffer[0] < logging.INFO: row_buffer[0] = logging.INFO row_buffer[2] += msg feedback_row.append(row_buffer) else: msg = _(u'Invalid import steps.') api.portal.show_message(msg, self.request, type='error') logger.error(msg) success_count += 1 duration = str(timedelta(seconds=(datetime.now() - starttime).seconds)) msg = 'Finished importing {0} tumour objects in {1}'.format( sheet.nrows - 2, duration) logger.info(msg) failed_count = sheet.nrows - 2 - success_count if success_count: msg = _(u'${count} item(s) import successfully.', mapping={'count': success_count}) api.portal.show_message(msg, self.request) logger.info('{count} item(s) import successfully.'.format( count=success_count)) if failed_count: msg = _(u'${count} item(s) import failed.', mapping={'count': failed_count}) api.portal.show_message(msg, self.request, 'error') logger.warn( '{count} item(s) import failed.'.format(count=failed_count)) self.request['feedback_title'] = feedback_title self.request['feedback_value'] = feedback_row self.request['feedback_count'] = success_count + failed_count self.request['success_count'] = success_count self.request['failed_count'] = failed_count self.request['duration'] = duration if all_true_mode and failed_count: msg = _(u'An error occurred, and the operation aborted.') api.portal.show_message(msg, self.request, 'error') logger.info(msg) transaction.abort() return self.index()
def testInterface(self): verifyObject(ITranslationDomain, self._domain) errors = getValidationErrors(ITranslationDomain, self._domain) self.assertFalse(errors)
def render(self): if getValidationErrors(IQuotation, self.context): return u'<abbr title="Schema validation error" class="error schema-validation-error">S</abbr>' else: return u''
def __call__(self, data, validate_all=False): modified = False errors = [] for schema in iterSchemata(self.context): write_permissions = mergedTaggedValueDict( schema, WRITE_PERMISSIONS_KEY) for name, field in getFields(schema).items(): if field.readonly: continue if name in data: if not self.check_permission(write_permissions.get(name)): continue # Deserialize to field value deserializer = queryMultiAdapter( (field, self.context, self.request), IFieldDeserializer) if deserializer is None: continue try: value = deserializer(data[name]) except ValueError as e: errors.append({ 'message': e.message, 'field': name, 'error': e}) except ValidationError as e: errors.append({ 'message': e.doc(), 'field': name, 'error': e}) else: f = schema.get(name) try: f.validate(value) except ValidationError as e: errors.append({ 'message': e.doc(), 'field': name, 'error': e}) else: setattr(schema(self.context), name, value) if validate_all: validation = getValidationErrors(schema, schema(self.context)) if len(validation): for e in validation: errors.append({ 'message': e[1].doc(), 'field': e[0], 'error': e }) if errors: raise DeserializationError(errors) if modified: notify(ObjectModifiedEvent(self.context)) return self.context
def test_interfaces__IField__3(field): """It does not raise `Invalid` if there are values for a choice field.""" field.type = u'Choice' field.values = [u'sure', u'not really'] errors = getValidationErrors(IField, field) assert [] == errors
def testInterface(self): verifyObject(IMessageCatalog, self._catalog) errors = getValidationErrors(IMessageCatalog, self._catalog) self.assertFalse(errors)
def testImplementsATGoogleVideo(self): iface = IATGoogleVideo self.assertTrue(iface.providedBy(self.video1)) self.assertFalse(getValidationErrors(iface, self.video1))
def testImplementsATContentType(self): iface = IATContentType self.assertTrue(iface.providedBy(self.video1)) self.assertFalse(getValidationErrors(iface, self.video1))
def testImplementsImageContent(self): iface = IImageContent self.assertTrue(iface.providedBy(self.video1)) self.assertFalse(getValidationErrors(iface, self.video1))
def test_interfaces__IField__1(field): """It does not raise `Invalid` if the field type is not 'choice'.""" field.type = u'Text' errors = getValidationErrors(IField, field) assert [] == errors
def _matches(self, item): errors = getValidationErrors(self.iface, item) return not errors