@@ -313,7 +313,40 @@ def _parse_value(self, val):
313
313
return list (ids )
314
314
315
315
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 ):
317
350
318
351
def _parse_value (self , val ):
319
352
items = val .split (self ._sep )
@@ -325,10 +358,12 @@ def _parse_value(self, val):
325
358
for item in items :
326
359
item = item .strip ()
327
360
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 (
330
363
self ._min_length , self ._id_length ))
331
364
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 )
332
367
if item in ids :
333
368
raise ValueError (strings .ID_PARAM_UNIQUE .format (item ))
334
369
@@ -1212,7 +1247,7 @@ def parse_get_params(self, qs_params):
1212
1247
)
1213
1248
1214
1249
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 ),
1216
1251
N .NAME : StrParameter (),
1217
1252
N .STATE : CompoundParameter ([IdsParameter (constants .STATE_ID_LEN ),
1218
1253
StrParameter ()]),
@@ -1251,7 +1286,7 @@ def parse_get_params(self, qs_params):
1251
1286
)
1252
1287
1253
1288
PARAMS_LOCALITIES = EndpointParameters (shared_params = {
1254
- N .ID : IdsParameter ( id_length = constants .LOCALITY_ID_LEN ),
1289
+ N .ID : IdsTwoLengthParameter ( * constants .LOCALITY_ID_LEN ),
1255
1290
N .NAME : StrParameter (),
1256
1291
N .STATE : CompoundParameter ([IdsParameter (constants .STATE_ID_LEN ),
1257
1292
StrParameter ()]),
@@ -1323,7 +1358,7 @@ def parse_get_params(self, qs_params):
1323
1358
IdsParameter (constants .CENSUS_LOCALITY_ID_LEN ),
1324
1359
StrParameter ()
1325
1360
]),
1326
- N .LOCALITY : CompoundParameter ([IdsParameter ( constants .LOCALITY_ID_LEN ),
1361
+ N .LOCALITY : CompoundParameter ([IdsTwoLengthParameter ( * constants .LOCALITY_ID_LEN ),
1327
1362
StrParameter ()]),
1328
1363
N .ORDER : StrParameter (choices = [N .ID , N .NAME ]),
1329
1364
N .FLATTEN : BoolParameter (),
0 commit comments