def to_representation(self, value): """ Returns nested dictionary in format {'links': {'self.link_type': ... } If no meta information, self.link_type is equal to a string containing link's URL. Otherwise, the link is represented as a links object with 'href' and 'meta' members. """ url = super(JSONAPIHyperlinkedIdentityField, self).to_representation(value) meta = {} for key in self.meta or {}: if key == 'count': show_related_counts = self.context['request'].query_params.get('related_counts', False) if utils.is_truthy(show_related_counts): meta[key] = _rapply(self.meta[key], _url_val, obj=value, serializer=self.parent) elif utils.is_falsy(show_related_counts): continue else: raise InvalidQueryStringError( detail="Acceptable values for the related_counts query param are 'true' or 'false'; got '{0}'".format(show_related_counts), parameter='related_counts' ) else: meta[key] = _rapply(self.meta[key], _url_val, obj=value, serializer=self.parent) return {'links': {self.link_type: {'href': url, 'meta': meta}}}
def to_representation(self, value): """ Returns nested dictionary in format {'links': {'self.link_type': ... } If no meta information, self.link_type is equal to a string containing link's URL. Otherwise, the link is represented as a links object with 'href' and 'meta' members. """ meta = _rapply(self.meta, _url_val, obj=value, serializer=self.parent) return {'links': {self.link_type: {'href': value.get_absolute_url(), 'meta': meta}}}
def to_representation(self, value): """ Returns nested dictionary in format {'links': {'self.link_type': ... } If no meta information, self.link_type is equal to a string containing link's URL. Otherwise, the link is represented as a links object with 'href' and 'meta' members. """ url = super(JSONAPIHyperlinkedRelatedField, self).to_representation(value) meta = _rapply(self.meta, _url_val, obj=value, serializer=self.parent) return {'links': {self.link_type: {'href': url, 'meta': meta}}}
def get_meta_information(self, meta_data, value): """ For retrieving meta values, otherwise returns {} """ meta = {} for key in meta_data or {}: if key == 'count': show_related_counts = self.context['request'].query_params.get('related_counts', False) if utils.is_truthy(show_related_counts): meta[key] = _rapply(meta_data[key], _url_val, obj=value, serializer=self.parent) elif utils.is_falsy(show_related_counts): continue if not utils.is_truthy(show_related_counts): raise InvalidQueryStringError( detail="Acceptable values for the related_counts query param are 'true' or 'false'; got '{0}'".format(show_related_counts), parameter='related_counts' ) else: meta[key] = _rapply(meta_data[key], _url_val, obj=value, serializer=self.parent) return meta
def is_valid(self, clean_html=True, **kwargs): """ After validation, scrub HTML from validated_data prior to saving (for create and update views) Exclude 'type' from validated_data. """ ret = super(JSONAPIListSerializer, self).is_valid(**kwargs) if clean_html is True: self._validated_data = _rapply(self.validated_data, strip_html) for data in self._validated_data: data.pop('type', None) return ret
def is_valid(self, clean_html=True, **kwargs): """ After validation, scrub HTML from validated_data prior to saving (for create and update views) Exclude 'type' and '_id' from validated_data. """ ret = super(JSONAPISerializer, self).is_valid(**kwargs) if clean_html is True: self._validated_data = _rapply(self.validated_data, strip_html) self._validated_data.pop('type', None) update_methods = ['PUT', 'PATCH'] if self.context['request'].method in update_methods: self._validated_data.pop('_id', None) return ret
def to_representation(self, obj): ret = _rapply(self.links, _url_val, obj=obj, serializer=self.parent) if hasattr(obj, 'get_absolute_url') and 'self' not in self.links: ret['self'] = obj.get_absolute_url() return ret