def deserialize(self): '''Deserializes this instance and returns the object representation''' if self._obj: return self._obj new_obj_data = {} # take the intersection of the fields given and the fields in # self.model_fields for field_name in [f for f in self.model_fields if f in self._data]: value = self.sanitize_field_value(field_name, self._data[field_name]) new_obj_data[field_name] = value for stub_field_name in self.stub_fields.keys(): new_obj_data[stub_field_name] = self.stub_object_finding( new_obj_data, stub_field_name, self._data[stub_field_name]) if self.model_has_field('geo_location') and \ 'geoLocation' in self._data: dataloc = self._data['geoLocation'] loc = GeoLocation(elevation=dataloc.get('elevation', None), latitude=dataloc['latitude'], longitude=dataloc['longitude']) loc.save() new_obj_data['geo_location'] = loc # the query string may contain more object data, for instance if # we're posting to a child collection resource new_obj_data.update(self._filters) self._obj = self.model(**new_obj_data) return self._obj
def update(self, data): '''Updates this resource given the data, which is expected to be a dict in the format given by this resource's schema''' for k, v in data.iteritems(): if k in self.model_fields: setattr(self._obj, k, v) elif k in self.stub_fields: setattr(self._obj, k, self.stub_object_finding(self._obj, k, v)) elif k == 'geoLocation': loc = self._obj.geo_location if loc is None: loc = GeoLocation(elevation=v.get('elevation', None), latitude=v['latitude'], longitude=v['longitude']) loc.save() self._obj.geo_location = loc else: for field, value in v.iteritems(): setattr(loc, field, value) loc.save() self._obj.save()