Skip to content

Commit

Permalink
Merge pull request #229 from pavloae/master
Browse files Browse the repository at this point in the history
fix: Corrige validación de parámetro ID para referenciar asentamiento…
  • Loading branch information
pavloae authored Oct 7, 2024
2 parents cfc1446 + 791fb1c commit ba750d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions service/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 25 additions & 1 deletion service/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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()]),
Expand Down
1 change: 1 addition & 0 deletions service/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down

0 comments on commit ba750d1

Please sign in to comment.