MCPcopy
hub / github.com/django/django / verify_ogr_field

Method verify_ogr_field

django/contrib/gis/utils/layermapping.py:390–462  ·  view source on GitHub ↗

Verify if the OGR Field contents are acceptable to the model field. If they are, return the verified value, otherwise raise an exception.

(self, ogr_field, model_field)

Source from the content-addressed store, hash-verified

388
389 # Verification routines used in constructing model keyword arguments.
390 def verify_ogr_field(self, ogr_field, model_field):
391 """
392 Verify if the OGR Field contents are acceptable to the model field. If
393 they are, return the verified value, otherwise raise an exception.
394 """
395 if isinstance(ogr_field, OFTString) and isinstance(
396 model_field, (models.CharField, models.TextField)
397 ):
398 if self.encoding and ogr_field.value is not None:
399 # The encoding for OGR data sources may be specified here
400 # (e.g., 'cp437' for Census Bureau boundary files).
401 val = force_str(ogr_field.value, self.encoding)
402 else:
403 val = ogr_field.value
404 if (
405 model_field.max_length
406 and val is not None
407 and len(val) > model_field.max_length
408 ):
409 raise InvalidString(
410 "%s model field maximum string length is %s, given %s characters."
411 % (model_field.name, model_field.max_length, len(val))
412 )
413 elif isinstance(ogr_field, OFTReal) and isinstance(
414 model_field, models.DecimalField
415 ):
416 try:
417 # Creating an instance of the Decimal value to use.
418 d = Decimal(str(ogr_field.value))
419 except DecimalInvalidOperation:
420 raise InvalidDecimal(
421 "Could not construct decimal from: %s" % ogr_field.value
422 )
423
424 # Getting the decimal value as a tuple.
425 dtup = d.as_tuple()
426 digits = dtup[1]
427 d_idx = dtup[2] # index where the decimal is
428
429 # Maximum amount of precision, or digits to the left of the
430 # decimal.
431 max_prec = model_field.max_digits - model_field.decimal_places
432
433 # Getting the digits to the left of the decimal place for the
434 # given decimal.
435 if d_idx < 0:
436 n_prec = len(digits[:d_idx])
437 else:
438 n_prec = len(digits) + d_idx
439
440 # If we have more than the maximum digits allowed, then throw an
441 # InvalidDecimal exception.
442 if n_prec > max_prec:
443 raise InvalidDecimal(
444 "A DecimalField with max_digits %d, decimal_places %d must "
445 "round to an absolute value less than 10^%d."
446 % (model_field.max_digits, model_field.decimal_places, max_prec)
447 )

Callers 2

feature_kwargsMethod · 0.95
verify_fkMethod · 0.95

Calls 4

force_strFunction · 0.90
InvalidStringClass · 0.85
InvalidDecimalClass · 0.85
InvalidIntegerClass · 0.85

Tested by

no test coverage detected