def wrap(self, obj): for attr, _ in obj.items(): if attr not in self._properties_by_key: # JsonObject will raise an exception here anyways, but we need # a user-friendly error message raise BadValueError(f"'{attr}' is not a valid field.") return super().wrap(obj)
def wrap(cls, obj): wrapped = super(IndicatorSpecBase, cls).wrap(obj) if not wrapped.column_id: raise BadValueError('column_id must not be empty!') if not wrapped.display_name not in obj: wrapped.display_name = wrapped.column_id return wrapped
def wrap(cls, data): amount = data.get('amount', None) if amount: try: float_amount = float(amount) data['amount'] = float_amount except (ValueError, TypeError): raise BadValueError("amount '{}' is not a number".format(amount)) return super(PaymentUpdate, cls).wrap(data)
def wrap(self, obj): try: latitude, longitude, altitude, accuracy = obj.split(' ') except (TypeError, AttributeError, ValueError): raise BadValueError("GeoPoint format expects 4 decimals: {!r}" .format(obj)) try: latitude = _canonical_decimal(latitude) longitude = _canonical_decimal(longitude) # this should eventually be removed once it's fixed on the mobile # the mobile sometimes submits in scientific notation # but only comes up for very small values # http://manage.dimagi.com/default.asp?159863 altitude = _canonical_decimal_round_tiny_exp(altitude) accuracy = _canonical_decimal(accuracy) except ValueError: raise BadValueError("{!r} is not a valid format GeoPoint format" .format(obj)) return GeoPoint(latitude, longitude, altitude, accuracy)
def from_string(cls, input_string, flexible=False): """ Construct GeoPoint object from string containing space-separated decimals. Expects 4 elements, unless flexible=True, in which case 2 works too CommCare mobile GPS questions come back with 4 elements, Geocoder uses 2 """ try: latitude, longitude, altitude, accuracy = _extract_elements( input_string, flexible) return cls( _to_decimal(latitude), _to_decimal(longitude), _to_decimal(altitude), _to_decimal(accuracy), ) except _GeoPointGenerationError as e: raise BadValueError( f"{input_string} is not a valid format GeoPoint format") from e
def wrap(self, obj): try: return GeoPoint(*[_canonical_decimal(n) for n in obj.split(' ')]) except (ValueError, TypeError): raise BadValueError( "{!r} is not a valid format GeoPoint format".format(obj))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not (bool(self.case_id) ^ bool(self.temporary_id)): raise BadValueError("You must set either case_id or temporary_id, and not both.")
def is_simple_dict(d): if not isinstance(d, dict) or not all(isinstance(v, str) for v in d.values()): raise BadValueError("Case properties must be strings")