diff --git a/service/constants.py b/service/constants.py index 56e1eae..554e3c6 100644 --- a/service/constants.py +++ b/service/constants.py @@ -27,6 +27,6 @@ DEPT_ID_LEN = 5 MUNI_ID_LEN = 6 CENSUS_LOCALITY_ID_LEN = 8 -SETTLEMENT_ID_LEN = 11 -LOCALITY_ID_LEN = SETTLEMENT_ID_LEN +SETTLEMENT_ID_LEN = 10 +LOCALITY_ID_LEN = 11 STREET_ID_LEN = 13 diff --git a/service/params.py b/service/params.py index 6bc6b45..4179041 100644 --- a/service/params.py +++ b/service/params.py @@ -313,6 +313,30 @@ def _parse_value(self, val): return list(ids) +class IdsAlphamericParameter(IdsParameter): + + def _parse_value(self, val): + items = val.split(self._sep) + if len(items) > constants.MAX_RESULT_LEN: + raise ValueError(strings.ID_PARAM_LENGTH.format( + constants.MAX_RESULT_LEN)) + + ids = set() + for item in items: + item = item.strip() + + if len(item) > self._id_length or len(item) < self._min_length: + raise ValueError(strings.ID_ALPHAMERIC_PARAM_INVALID.format( + self._min_length, self._id_length)) + + if item in ids: + raise ValueError(strings.ID_PARAM_UNIQUE.format(item)) + + ids.add(item) + + return list(ids) + + class CompoundParameter(Parameter): """Representa un parámetro que puede tomar distintos valores, representados por una lista de objetos 'Parameter'. @@ -1188,7 +1212,7 @@ def parse_get_params(self, qs_params): ) PARAMS_SETTLEMENTS = EndpointParameters(shared_params={ - N.ID: IdsParameter(id_length=constants.SETTLEMENT_ID_LEN), + N.ID: IdsAlphamericParameter(id_length=constants.SETTLEMENT_ID_LEN, padding_length=2), N.NAME: StrParameter(), N.STATE: CompoundParameter([IdsParameter(constants.STATE_ID_LEN), StrParameter()]), diff --git a/service/strings.py b/service/strings.py index 4a1e365..29abf85 100644 --- a/service/strings.py +++ b/service/strings.py @@ -33,6 +33,7 @@ NOT_FOUND = 'No se encontró la URL especificada.' NOT_ALLOWED = 'Método no permitido en el recurso seleccionado.' ID_PARAM_INVALID = 'Cada ID debe ser numérico y de longitud {}.' +ID_ALPHAMERIC_PARAM_INVALID = 'Cada ID debe ser de longitud entre {} y {}.' ID_PARAM_LENGTH = 'La cantidad de ID debe ser menor o igual que {}.' ID_PARAM_UNIQUE = 'La lista no debe contener ID repetidos (ID repetido: {}).' COMPOUND_PARAM_ERROR = 'El valor del parámetro no es válido.'