def validate_access_application_url(key, data, errors, context): ''' Validate dataset's `access_application_URL`. Dummy value _must_ be added for a new form so that it can be overwritten in the same session in iPackageController `edit` hook. For REMS. ''' if data.get(('availability',)) == 'access_application': if data.get(('access_application_new_form',)) in [u'True', u'on']: data[key] = h.full_current_url().replace('/edit/', '/') else: not_empty(key, data, errors, context) url_validator(key, data, errors, context) else: data.pop(key, None) raise StopOnError
def validate_access_application_url(key, data, errors, context): ''' Validate dataset's `access_application_URL`. Dummy value _must_ be added if user chooses either reetta option so that its value can be overwritten in ckanext-rems when it knows what the access_application_URL will be. If user has chosen the option to which access application URL is input directly, then validation checks its not empty and that it is an url. ''' if data.get(('availability',)) == 'access_application_rems' or \ data.get(('availability',)) == 'access_application_other': if data.get(('availability',)) == 'access_application_rems': data[key] = h.full_current_url().replace('/edit/', '/') elif data.get(('availability',)) == 'access_application_other': not_empty(key, data, errors, context) url_validator(key, data, errors, context) else: data.pop(key, None) raise StopOnError
def validate_access_application_url(key, data, errors, context): ''' Validate dataset's `access_application_URL`. Dummy value _must_ be added if user chooses either reetta option so that its value can be overwritten in ckanext-rems when it knows what the access_application_URL will be. If user has chosen the option to which access application URL is input directly, then validation checks its not empty and that it is an url. ''' if data.get(('availability',)) == 'access_application_rems' or \ data.get(('availability',)) == 'access_application_other': if data.get(('availability', )) == 'access_application_rems': data[key] = h.full_current_url().replace('/edit/', '/') elif data.get(('availability', )) == 'access_application_other': not_empty(key, data, errors, context) url_validator(key, data, errors, context) else: data.pop(key, None) raise StopOnError
def call_validator(*args, **kwargs): return validators.url_validator(*args, **kwargs)
def _validate_url(self, url): errors = {'url':[]} url_validator('url', {'url':url}, errors, {'model':None, 'session':None}) if errors['url']: return errors['url'][0]
def validate_link(key, data, errors, context): link = data[key] if not link: return url_validator(key, data, errors, context)
def dcatapit_conforms_to(value, context): """ Validates conforms to structure [ {'identifier': str, 'title': {lang: str}, 'description': {lang: str}, 'referenceDocumentation: [ str, str], },.. ] This should also handle old notation: 'VAL1,VAL2' """ if not value: raise Invalid(_('Conforms to value should not be empty')) try: data = json.loads(value) except (TypeError, ValueError): try: old_data = value.split(',') return json.dumps([{ 'identifier': v, 'title': {}, 'description': {}, 'referenceDocumentation': [] } for v in old_data]) except (AttributeError, TypeError, ValueError): raise Invalid(_('Invalid payload for conforms_to')) if not isinstance(data, list): raise Invalid(_('List expected for conforms_to values')) allowed_keys = [ 'uri', 'identifier', 'title', 'description', 'referenceDocumentation' ] new_data = [] for elm in data: new_elm = {} if not isinstance(elm, dict): raise Invalid(_('Each conforms_to element should be a dict')) # LEGACY: rewrite _ref to uri for older data _ref = elm.pop('_ref', None) if _ref and not elm.get('uri'): elm['uri'] = _ref for k in elm.keys(): if k not in allowed_keys: raise Invalid( _('Unexpected {} key in conforms_to value').format(k)) if not isinstance(elm.get('identifier'), str): raise Invalid(_('conforms_to element should contain identifier')) for prop_name, allowed_types in ( ( 'uri', str, ), ('identifier', str), ( 'title', dict, ), ( 'description', dict, ), ( 'referenceDocumentation', list, ), ): # those are not obligatory fields try: prop_val = elm[prop_name] except KeyError: prop_val = None if prop_val is None: continue if not isinstance(prop_val, allowed_types): raise Invalid( _('conforms_to property {} is not valid type').format( prop_name)) # {lang -> value} mapping if allowed_types == dict: for k, v in prop_val.items(): if not isinstance(k, str): raise Invalid( _('conforms_to property {} should have {} key as string' ).format(prop_name, k)) if not isinstance(v, str): raise Invalid( _('conforms_to property {} should have {} value as string' ).format(prop_name, k)) if v is None: raise Invalid( _('conforms_to property {} for {} lang should not be empty' ).format(prop_name, k)) _populate_multilang_dict( prop_val ) # TODO: do we really want to forge entries for all languages? if prop_name == 'uri' and prop_val == '': continue if prop_name == 'referenceDocumentation': if prop_val: # keep unique values processed = set([]) for ref_doc in prop_val: if not isinstance(ref_doc, str): raise Invalid( _('conforms_to property referenceDocumentation should contain urls' )) errors = {'ref_doc': []} url_validator('ref_doc', {'ref_doc': ref_doc}, errors, { 'model': None, 'session': None }) if errors['ref_doc']: raise Invalid(errors['ref_doc']) processed.add(ref_doc) prop_val = list(processed) new_elm[prop_name] = prop_val new_data.append(new_elm) return json.dumps(new_data)