def workflow_stage_validator(key, data, errors, context): stage = data[key] type = data.get((h._workflow_type_field(),), missing) if type is missing or not type: type = 'base' wf = Workflow.get_workflow(type) first_stage = str(wf.start) if stage is missing or not stage: data[key] = first_stage return pkg = context.get('package') if pkg is None and stage != first_stage: raise Invalid('You cannot skip fist stage') current_stage = pkg.extras.get(key[0]) if current_stage == stage: return try: wf.get_stage(stage) except KeyError: raise Invalid('Unsupported workflow stage') user = context['auth_user_obj'] if user and (user.sysadmin or h.is_site_admin(user)): return if stage == wf.finish: raise Invalid('Only site admin can set this stage') if not user or user.id != pkg.creator_user_id: raise Invalid('Only dataset creator can change stage')
def custom_header_url_validator(value): def check_characters(value): if set(value) <= set(string.ascii_letters + string.digits + '-./'): return False return True for item in value.get('links', []): link = item.get('link', '') if len(link) > 2000: raise Invalid( 'Url is too long. Only 2000 characters allowed for "{}"'. format(link)) pieces = urlparse(link) if pieces.scheme and pieces.scheme != 'https': raise Invalid('Only HTTPS urls supported "{}"'.format(link)) elif not pieces.path and not all([pieces.scheme, pieces.netloc]): raise Invalid( 'Empty relative path in relative url {}'.format(link)) elif pieces.path and not all([pieces.scheme, pieces.netloc ]) and check_characters(pieces.path): raise Invalid( 'Relative path contains invalid characters {}'.format(link)) elif pieces.netloc and check_characters(pieces.netloc): raise Invalid('Url contains invalid characters "{}"'.format(link)) return value
def importable_string(value: str): from werkzeug.utils import import_string, ImportStringError from ckan.logic.validators import Invalid try: return import_string(value) except ImportStringError as e: raise Invalid(str(e))
def is_after_start(key, flattened_data, errors, context): temporal_end_dict = None try: for k in flattened_data: if k == ('__extras', ): temporal_end_dict = flattened_data[k] elif k == (u'temporal_end', ): if temporal_end_dict and temporal_end_dict.get('temporal_end_date', None) and \ temporal_end_dict.get('temporal_end_time', None): flattened_data[k] = temporal_end_dict[ 'temporal_end_date'] + ' ' + temporal_end_dict[ 'temporal_end_time'] elif temporal_end_dict and temporal_end_dict.get( 'temporal_end_date', None): flattened_data[k] = temporal_end_dict[ 'temporal_end_date'] + ' 00:00:00.000000' else: flattened_data[k] = '' except KeyError: return True if temporal_end_dict: start_date = temporal_end_dict.get('temporal_start_date', '') + ' ' + \ temporal_end_dict.get('temporal_start_time', '') end_date = temporal_end_dict.get('temporal_end_date', '') + ' ' + temporal_end_dict.get( 'temporal_end_time', '') if temporal_end_dict.get('temporal_end_date', None): if start_date > end_date: raise Invalid(_('Start date must be before end date')) return True
def countries_covered_belgium(value, context): if value: if len( value ) != 0 and u'http://publications.europa.eu/resource/authority/country/BEL' not in value: raise Invalid(_('Belgium is a required country')) return value
def phone_number_validator(value, context): if value: if not phone_number_pattern.match(value): raise Invalid( _('Phone number {number} is not a valid format').format( number=value)) return value
def https_validator(value, context): if value and value.startswith(('http', 'https')) and len(value) > 0: if not https_pattern.match(value): if http_pattern.match(value): value = remove_prefix(value, "http://") raise Invalid( _('URL {url} has to start with https://').format(url=value)) return value
def workflow_type_validator(key, data, errors, context): value = data[key] if value is missing or not value: data[key] = 'base' return try: Workflow.get_workflow(value) except KeyError: raise Invalid('Unsupported workflow type')