valuelist_validator.py 8.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# encoding: utf-8

"""
Module that exposes validation methods that validate values based on a restricted vocabulary.
"""

import json
import os
import ckan.plugins.toolkit as tk
from ckanext.dcatdonl.helper.caching import cached


13 14 15 16 17 18 19 20 21 22 23
def overheid_dataset_status(value):
    """
    Validates value against the overheid_dataset_status valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param value: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('overheid_dataset_status', value)


24 25 26 27 28 29 30 31
def overheid_taxonomiebeleidsagenda(values):
    """
    Validates values against the overheid_taxonomiebeleidsagenda valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when values fail validation.

    :param values: list, the value to validate
    :return: list, the original values, if they pass validation
    """
Willem ter Berg's avatar
Willem ter Berg committed
32
    return _check_value('overheid_taxonomiebeleidsagenda', values)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67


def overheid_openbaarheidsniveau(value):
    """
    Validates value against the overheid_openbaarheidsniveau valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param value: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('overheid_openbaarheidsniveau', value)


def overheid_frequency(value):
    """
    Validates value against the overheid_frequentie valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param value: str, the value to validate
    :return: str, the original values, if it passes validation
    """
    return _check_value('overheid_frequency', value)


def overheid_license(value):
    """
    Validates value against the overheid_license valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when values fail validation.

    :param value: list, the value to validate
    :return: list, the original values, if they pass validation
    """
    if isinstance(value, list):
        raise tk.Invalid('only one license can be provided, list given')

Willem ter Berg's avatar
Willem ter Berg committed
68
    if 'id' not in value:
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        raise tk.Invalid('value must have a id property')

    _check_license_file(value['id'])

    return value


def overheid_spatial_scheme(value):
    """
    Validates value against the overheid_spatial_scheme valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param value: str, the value to validate
    :return: str, the original value if it passes validation
    """
Willem ter Berg's avatar
Willem ter Berg committed
84
    return _check_value('overheid_spatial_scheme', value, 1, 0, False)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219


def overheid_spatial_gemeente(value):
    """
    Validates value against the overheid_spatial_gemeente valuelist.

    :param value: str, the value to check
    :return: bool, whether the value validates
    """
    try:
        _check_value('overheid_spatial_gemeente', value)
        return True
    except tk.Invalid:
        return False


def overheid_spatial_koninkrijksdeel(value):
    """
    Validates values against the overheid_spatial_koninkrijksdeel valuelist.

    :param value: str, the value to check
    :return: bool, whether the value validates
    """
    try:
        _check_value('overheid_spatial_koninkrijksdeel', value)
        return True
    except tk.Invalid:
        return False


def overheid_spatial_provincie(value):
    """
    Validates values against the overheid_spatial_provincie valuelist.

    :param value: str, the value to check
    :return: bool, whether the value validates
    """
    try:
        _check_value('overheid_spatial_provincie', value)
        return True
    except tk.Invalid:
        return False


def overheid_spatial_waterschap(value):
    """
    Validates values against the overheid_spatial_waterschap valuelist.

    :param value: str, the value to check
    :return: bool, whether the value validates
    """
    try:
        _check_value('overheid_spatial_waterschap', value)
        return True
    except tk.Invalid:
        return False


def donl_catalogs(value):
    """
    Validates value against the donl_catalogs valuelist. Will throw a ckan.plugins.toolkit.Invalid
    when the value fails validation.

    :param value: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('donl_catalogs', value)


def donl_language(values):
    """
    Validates value against the donl_language valuelist. Will throw a ckan.plugins.toolkit.Invalid
    when the value fails validation.

    :param values: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('donl_language', values)


def donl_organization(values):
    """
    Validates value against the donl_organization valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param values: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('donl_organization', values)


def donl_dcat_type(value):
    """
    Validates values against the donl_dcat_type valuelist. Will throw a ckan.plugins.toolkit.Invalid
    when values fail validation.

    :param value: str, the value to validate
    :return: str, the original value, if it passes validation
    """
    return _check_value('donl_dcat_type', value)


def mdr_filetype_nal(values):
    """
    Validates value against the mdr_filetype_nal valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param values: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('mdr_filetype_nal', values)


def iana_mediatypes(values):
    """
    Validates value against the iana_mediatypes valuelist. Will throw a ckan.plugins.toolkit.Invalid
    when the value fails validation.

    :param values: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('iana_mediatypes', values)


def adms_distributiestatus(values):
    """
    Validates value against the adms_distributiestatus valuelist. Will throw a
    ckan.plugins.toolkit.Invalid when the value fails validation.

    :param values: str, the value to validate
    :return: str, the original value if it passes validation
    """
    return _check_value('adms_distributiestatus', values)


220
def _check_value(name, values):
221 222 223 224
    """
    Checks if a given value is present in the given valuelist.

    :param name: str, the name of the valuelist
225
    :param values: str, the value to check
226 227
    :return: str, the original value if it passes validation
    """
Willem ter Berg's avatar
Willem ter Berg committed
228
    valid_values = _read_file_as_json(name)
229

230 231
    if isinstance(values, list):
        for val in values:
Willem ter Berg's avatar
Willem ter Berg committed
232 233
            if val not in valid_values:
                raise tk.Invalid('value [' + val + '] is not a valid ' + name)
234

235
            return values
236

237 238 239
    if isinstance(values, basestring):
        if values not in valid_values:
            raise tk.Invalid('value [' + values + '] is not a valid ' + name)
240

241
    return values
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288


@cached
def _check_license_file(value):
    """
    Reads the CKAN license file and tries to match its ids to the given value.

    :param value: str, the value to check
    :return: str, the original value if it passes validation
    """
    with open(_create_file_path('overheid_license'), 'rb') as file_contents:
        parsed = json.loads(file_contents.read())

    keys = []
    for block in parsed:
        if 'id' in block:
            keys.append(block['id'])

    for key in keys:
        if value == key:
            return value

    raise tk.Invalid('no matching license id found for given value')


@cached
def _read_file_as_json(filename):
    """
    Reads a given file and returns the JSON keys found within.

    :param filename: str, the name of the file, without its extension
    :return: list, a list of keys
    """
    with open(_create_file_path(filename), 'rb') as file_contents:
        parsed = json.loads(file_contents.read())

    return parsed.keys()


def _create_file_path(name):
    """
    Determines and returns the absolute path of a given filename.

    :param name: string, the name of the file
    :return: string, the absolute path of the given file
    """
    return os.path.join(os.path.dirname(__file__), '../resources/' + name + '.json')