Пример #1
0
    def pre_validate(self, form):
        if self.data:
            backend = self.get_backend(form)
            # Validate filters
            for f in (self.data.get('filters') or []):
                if not ('key' in f and 'value' in f):
                    msg = 'A field should have both key and value properties'
                    raise validators.ValidationError(msg)
                specs = self.get_filter_specs(backend, f['key'])
                if not specs:
                    msg = 'Unknown filter key "{0}" for "{1}" backend'
                    msg = msg.format(f['key'], backend.name)
                    raise validators.ValidationError(msg)

                if isinstance(f['value'], str):
                    f['value'] = safe_unicode(f['value'])  # Fix encoding error

                if not isinstance(f['value'], specs.type):
                    msg = '"{0}" filter should of type "{1}"'
                    msg = msg.format(specs.key, specs.type.__name__)
                    raise validators.ValidationError(msg)
            # Validate features
            for key, value in (self.data.get('features') or {}).items():
                if not isinstance(value, bool):
                    msg = 'A feature should be a boolean'
                    raise validators.ValidationError(msg)
                if not self.get_feature_specs(backend, key):
                    msg = 'Unknown feature "{0}" for "{1}" backend'
                    msg = msg.format(key, backend.name)
                    raise validators.ValidationError(msg)
Пример #2
0
 def pre_validate(self, form):
     if self.data:
         if not isinstance(self.data, geojson.GeoJSON):
             raise validators.ValidationError('Not a valid GeoJSON')
         if not self.data.is_valid:
             raise validators.ValidationError(self.data.errors())
     return True
Пример #3
0
 def pre_validate(self, form):
     if self.data:
         backend = self.get_backend(form)
         # Validate filters
         for f in (self.data.get('filters') or []):
             if not ('key' in f and 'value' in f):
                 msg = 'A field should have both key and value properties'
                 raise validators.ValidationError(msg)
             specs = self.get_specs(backend, f['key'])
             if not specs:
                 msg = 'Unknown filter key "{0}" for "{1}" backend'
                 msg = msg.format(f['key'], backend.name)
                 raise validators.ValidationError(msg)
             if not isinstance(f['value'], specs.type):
                 msg = '"{0}" filter should of type "{1}"'
                 msg = msg.format(specs.key, specs.type.__name__)
                 raise validators.ValidationError(msg)
Пример #4
0
def enforce_allowed_schemas(form, field):
    schema = field.data
    allowed_schemas = [s['id'] for s in ResourceSchema.objects()]
    if schema not in allowed_schemas:
        message = _('Schema "{schema}" is not an allowed value. Allowed values: {values}')
        raise validators.ValidationError(message.format(
            schema=schema,
            values=', '.join(allowed_schemas)
        ))
Пример #5
0
def enforce_allowed_schemas(form, field):
    schema = field.data
    if schema:
        allowed_schemas = [s['id'] for s in ResourceSchema.objects()]
        if schema.get('name') not in allowed_schemas:
            message = _(
                'Schema name "{schema}" is not an allowed value. Allowed values: {values}'
            )
            raise validators.ValidationError(
                message.format(schema=schema.get('name'),
                               values=', '.join(allowed_schemas)))

        schema_versions = [
            d['versions'] for d in ResourceSchema.objects()
            if d['id'] == schema.get('name')
        ]
        allowed_versions = schema_versions[0] if schema_versions else []
        allowed_versions.append('latest')
        if 'version' in schema:
            if schema.get('version') not in allowed_versions:
                message = _(
                    'Version "{version}" is not an allowed value. Allowed values: {values}'
                )
                raise validators.ValidationError(
                    message.format(version=schema.get('version'),
                                   values=', '.join(allowed_versions)))
        properties = ['name', 'version']
        for prop in schema:
            if prop not in properties:
                message = _(
                    'Sub-property "{prop}" is not allowed value in schema field. Allowed values is : {properties}'
                )
                raise validators.ValidationError(
                    message.format(
                        prop=prop,
                        properties=', '.join(properties),
                    ))
Пример #6
0
def enforce_filetype_file(form, field):
    '''Only allowed domains in resource.url when filetype is file'''
    if form._fields.get('filetype').data != RESOURCE_FILETYPE_FILE:
        return
    domain = urlparse(field.data).netloc
    allowed_domains = current_app.config['RESOURCES_FILE_ALLOWED_DOMAINS']
    allowed_domains += [current_app.config.get('SERVER_NAME')]
    if current_app.config.get('CDN_DOMAIN'):
        allowed_domains.append(current_app.config['CDN_DOMAIN'])
    if '*' in allowed_domains:
        return
    if domain and domain not in allowed_domains:
        message = _('Domain "{domain}" not allowed for filetype "{filetype}"')
        raise validators.ValidationError(
            message.format(domain=domain, filetype=RESOURCE_FILETYPE_FILE))
Пример #7
0
def enforce_filetype_file(form, field):
    '''Only allowed domains in resource.url when filetype is file'''
    if field.data != RESOURCE_FILETYPE_FILE:
        return
    url = form._fields.get('url').data
    domain = urlparse(url).netloc
    allowed_domains = current_app.config.get(
        'RESOURCES_FILE_ALLOWED_DOMAINS', [])
    allowed_domains += [current_app.config.get('SERVER_NAME')]
    if '*' in allowed_domains:
        return
    if domain and domain not in allowed_domains:
        message = 'URL domain not allowed for filetype {}'.format(
            RESOURCE_FILETYPE_FILE)
        raise validators.ValidationError(_(message))
Пример #8
0
    def fetch_objects(self, geoids):
        '''
        Custom object retrieval.

        Zones are resolved from their identifier
        instead of the default bulk fetch by ID.
        '''
        zones = []
        no_match = []
        for geoid in geoids:
            zone = GeoZone.objects.resolve(geoid)
            if zone:
                zones.append(zone)
            else:
                no_match.append(geoid)

        if no_match:
            msg = _('Unknown geoid(s): {identifiers}').format(
                identifiers=', '.join(str(id) for id in no_match))
            raise validators.ValidationError(msg)

        return zones
Пример #9
0
def check_url_does_not_exists(form, field):
    '''Ensure a reuse URL is not yet registered'''
    if field.data != field.object_data and Reuse.url_exists(field.data):
        raise validators.ValidationError(_('This URL is already registered'))
Пример #10
0
 def pre_validate(self, form):
     if self.data:
         result = geojson.is_valid(self.data)
         if result['valid'] == 'no':
             raise validators.ValidationError(result['message'])
     return True