class PaymentUpdate(jsonobject.JsonObject): id = jsonobject.StringProperty(required=True) status = jsonobject.StringProperty(required=True, choices=[SUCCESS, FAILURE]) amount = jsonobject.FloatProperty(required=False) paymentDate = FlexibleDateTimeProperty(required=True) comments = jsonobject.StringProperty(required=False) failureDescription = jsonobject.StringProperty(required=False) paymentMode = jsonobject.StringProperty(required=False) checkNumber = jsonobject.StringProperty(required=False) bankName = jsonobject.StringProperty(required=False) @classmethod 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) @property def case_id(self): return self.id
class StockLedgerValueWrapper(jsonobject.JsonObject): """ Wrapper class to abstract StockState and the equivalent model coming out of Elasticsearch """ domain = jsonobject.StringProperty() case_id = jsonobject.StringProperty() section_id = jsonobject.StringProperty() entry_id = jsonobject.StringProperty() balance = jsonobject.DecimalProperty() # todo: should this be an int? last_modified = jsonobject.DateTimeProperty() last_modified_form_id = jsonobject.StringProperty() daily_consumption = jsonobject.FloatProperty() location_id = jsonobject.StringProperty() _sql_product = None _sql_location = None def __init__(self, _obj=None, sql_product=None, sql_location=None, *args, **kwargs): self._sql_product = sql_product self._sql_location = sql_location super(StockLedgerValueWrapper, self).__init__(_obj, *args, **kwargs) @property def sql_product(self): if self.entry_id and not self._sql_product: try: self._sql_product = SQLProduct.objects.get(domain=self.domain, product_id=self.entry_id) except ObjectDoesNotExist: # todo: cache this result so multiple failing calls don't keep hitting the DB return None return self._sql_product @property def sql_location(self): if self.location_id and not self._sql_location: try: self._sql_location = SQLLocation.objects.get(domain=self.domain, location_id=self.location_id) except ObjectDoesNotExist: # todo: cache this result so multiple failing calls don't keep hitting the DB return None return self._sql_location @classmethod def from_stock_state(cls, stock_state): return cls( case_id=stock_state.case_id, section_id=stock_state.section_id, entry_id=stock_state.product_id, balance=stock_state.stock_on_hand, last_modified=stock_state.last_modified_date, last_modified_form_id=stock_state.last_modified_form_id, daily_consumption=stock_state.daily_consumption, location_id=stock_state.location_id, sql_location=stock_state.sql_location, sql_product=stock_state.sql_product, )
class StockLedgerValueWrapper(jsonobject.JsonObject): """ Wrapper class to abstract StockState and the equivalent model coming out of Elasticsearch """ domain = jsonobject.StringProperty() case_id = jsonobject.StringProperty() section_id = jsonobject.StringProperty() entry_id = jsonobject.StringProperty() balance = jsonobject.DecimalProperty() # todo: should this be an int? last_modified = jsonobject.DateTimeProperty() last_modified_form_id = jsonobject.StringProperty() daily_consumption = jsonobject.FloatProperty() location_id = jsonobject.StringProperty() @property @quickcache(['self.domain', 'self.entry_id']) def sql_product(self): try: return SQLProduct.objects.get(domain=self.domain, product_id=self.entry_id) except ObjectDoesNotExist: return None @property @quickcache(['self.domain', 'self.location_id']) def sql_location(self): try: return ( SQLLocation.objects .prefetch_related('location_type') .get(domain=self.domain, location_id=self.location_id) ) except ObjectDoesNotExist: return None @classmethod def from_stock_state(cls, stock_state): return cls( case_id=stock_state.case_id, section_id=stock_state.section_id, entry_id=stock_state.product_id, balance=stock_state.stock_on_hand, last_modified=stock_state.last_modified_date, last_modified_form_id=stock_state.last_modified_form_id, daily_consumption=stock_state.daily_consumption, location_id=stock_state.location_id, sql_location=stock_state.sql_location, sql_product=stock_state.sql_product, )