Skip to content

Commit b1911b6

Browse files
author
Pablo Andino
committed
fix: Adapta parámetros de ID para localidades y asentamientos #232
Los asentamientos y las localidades, ambos provistos por BAHRA, han variado sus longitudes y podrían tener una longitud de ocho o diez caracteres. Los primeros son del tipo alfanuméricos y los segundos son numéricos.
1 parent 4eef435 commit b1911b6

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

service/constants.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
DEPT_ID_LEN = 5
2828
MUNI_ID_LEN = 6
2929
CENSUS_LOCALITY_ID_LEN = 8
30-
SETTLEMENT_ID_LEN = 10
31-
LOCALITY_ID_LEN = 11
30+
SETTLEMENT_ID_LEN = (8, 10)
31+
LOCALITY_ID_LEN = (8, 10)
3232
STREET_ID_LEN = 13

service/params.py

+41-6
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,40 @@ def _parse_value(self, val):
313313
return list(ids)
314314

315315

316-
class IdsAlphamericParameter(IdsParameter):
316+
class IdsTwoLengthParameter(IdsParameter):
317+
318+
def __init__(self, *two_length, padding_char='0', sep=','):
319+
max_length = max(two_length)
320+
min_length = min(two_length)
321+
super().__init__(max_length, padding_char=padding_char, padding_length=max_length - min_length, sep=sep)
322+
self._max_length = max_length
323+
self._min_length = min_length
324+
325+
def _parse_value(self, val):
326+
items = val.split(self._sep)
327+
if len(items) > constants.MAX_RESULT_LEN:
328+
raise ValueError(strings.ID_PARAM_LENGTH.format(
329+
constants.MAX_RESULT_LEN))
330+
331+
ids = set()
332+
for item in items:
333+
item = item.strip()
334+
335+
if not item.isdigit() or len(item) > self._id_length:
336+
raise ValueError(strings.ID_TWO_LENGTH_PARAM_INVALID.format(
337+
self._min_length, self._max_length))
338+
339+
id_length = self._id_length if len(item) > self._min_length else self._min_length
340+
item = item.rjust(id_length, self._padding_char)
341+
if item in ids:
342+
raise ValueError(strings.ID_PARAM_UNIQUE.format(item))
343+
344+
ids.add(item)
345+
346+
return list(ids)
347+
348+
349+
class IdsAlphamericTwoLengthParameter(IdsTwoLengthParameter):
317350

318351
def _parse_value(self, val):
319352
items = val.split(self._sep)
@@ -325,10 +358,12 @@ def _parse_value(self, val):
325358
for item in items:
326359
item = item.strip()
327360

328-
if len(item) > self._id_length or len(item) < self._min_length:
329-
raise ValueError(strings.ID_ALPHAMERIC_PARAM_INVALID.format(
361+
if len(item) > self._id_length:
362+
raise ValueError(strings.ID_ALPHAMERIC_TWO_LENGTH_PARAM_INVALID.format(
330363
self._min_length, self._id_length))
331364

365+
id_length = self._id_length if len(item) > self._min_length else self._min_length
366+
item = item.rjust(id_length, self._padding_char)
332367
if item in ids:
333368
raise ValueError(strings.ID_PARAM_UNIQUE.format(item))
334369

@@ -1212,7 +1247,7 @@ def parse_get_params(self, qs_params):
12121247
)
12131248

12141249
PARAMS_SETTLEMENTS = EndpointParameters(shared_params={
1215-
N.ID: IdsAlphamericParameter(id_length=constants.SETTLEMENT_ID_LEN, padding_length=2),
1250+
N.ID: IdsAlphamericTwoLengthParameter(*constants.SETTLEMENT_ID_LEN),
12161251
N.NAME: StrParameter(),
12171252
N.STATE: CompoundParameter([IdsParameter(constants.STATE_ID_LEN),
12181253
StrParameter()]),
@@ -1251,7 +1286,7 @@ def parse_get_params(self, qs_params):
12511286
)
12521287

12531288
PARAMS_LOCALITIES = EndpointParameters(shared_params={
1254-
N.ID: IdsParameter(id_length=constants.LOCALITY_ID_LEN),
1289+
N.ID: IdsTwoLengthParameter(*constants.LOCALITY_ID_LEN),
12551290
N.NAME: StrParameter(),
12561291
N.STATE: CompoundParameter([IdsParameter(constants.STATE_ID_LEN),
12571292
StrParameter()]),
@@ -1323,7 +1358,7 @@ def parse_get_params(self, qs_params):
13231358
IdsParameter(constants.CENSUS_LOCALITY_ID_LEN),
13241359
StrParameter()
13251360
]),
1326-
N.LOCALITY: CompoundParameter([IdsParameter(constants.LOCALITY_ID_LEN),
1361+
N.LOCALITY: CompoundParameter([IdsTwoLengthParameter(*constants.LOCALITY_ID_LEN),
13271362
StrParameter()]),
13281363
N.ORDER: StrParameter(choices=[N.ID, N.NAME]),
13291364
N.FLATTEN: BoolParameter(),

service/strings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
NOT_FOUND = 'No se encontró la URL especificada.'
3434
NOT_ALLOWED = 'Método no permitido en el recurso seleccionado.'
3535
ID_PARAM_INVALID = 'Cada ID debe ser numérico y de longitud {}.'
36-
ID_ALPHAMERIC_PARAM_INVALID = 'Cada ID debe ser de longitud entre {} y {}.'
36+
ID_TWO_LENGTH_PARAM_INVALID = 'Cada ID debe ser numérico y de longitud {} ó {}.'
37+
ID_ALPHAMERIC_TWO_LENGTH_PARAM_INVALID = 'Cada ID debe ser de longitud {} ó {}.'
3738
ID_PARAM_LENGTH = 'La cantidad de ID debe ser menor o igual que {}.'
3839
ID_PARAM_UNIQUE = 'La lista no debe contener ID repetidos (ID repetido: {}).'
3940
COMPOUND_PARAM_ERROR = 'El valor del parámetro no es válido.'

0 commit comments

Comments
 (0)