def _serialize(self, value, attr, obj): if not self.many: value = [value] links = None result = [] if value: for instance in value: field = Relationship(type_=type_from_model_name( instance.__class__.__name__), id_field='pk', self_url=self.self_url, related_url=self.related_url, many=False) field._add_to_schema(self.name, self.parent) field.include_data = self.include_data data = field._serialize(instance, attr, obj) if links is None and 'links' in data: links = data['links'] result.append(data.get('data')) else: data = super(PolymorphicRelationship, self)._serialize(None, attr, obj) if links is None and 'links' in data: links = data['links'] if not self.many: result = result[0] data = {} if links is not None: data['links'] = order_dict(links, LINKS_OBJECT_ORDER) if self.include_resource_linkage or self.include_data: data['data'] = result return order_dict(data, TOP_LEVEL_OBJECT_ORDER)
def _serialize(self, value, attr, obj): if not self.many: value = [value] links = None result = [] if value: for instance in value: field = Relationship( type_=type_from_model_name(instance.__class__.__name__), id_field='pk', self_url=self.self_url, related_url=self.related_url, many=False) field._add_to_schema(self.name, self.parent) field.include_data = self.include_data data = field._serialize(instance, attr, obj) if links is None and 'links' in data: links = data['links'] result.append(data.get('data')) else: data = super(PolymorphicRelationship, self)._serialize( None, attr, obj) if links is None and 'links' in data: links = data['links'] if not self.many: result = result[0] data = {} if links is not None: data['links'] = order_dict(links, LINKS_OBJECT_ORDER) if self.include_resource_linkage or self.include_data: data['data'] = result return order_dict(data, TOP_LEVEL_OBJECT_ORDER)
def format_item(self, item): item = super(JsonApiSerializer, self).format_item(item) if 'attributes' in item: attributes = item.pop('attributes') attributes.pop('-url', None) # super call adds this if attributes: item['attributes'] = order_dict(attributes, self.field_order) if 'relationships' in item: item['relationships'] = order_dict(item['relationships'], self.relationship_order) return order_dict(item, RESOURCE_OBJECT_ORDER)
def format_item(self, item): item = super(JsonApiSerializer, self).format_item(item) if 'attributes' in item: attributes = item.pop('attributes') attributes.pop('-url', None) # super call adds this if attributes: item['attributes'] = order_dict(attributes, self.field_order) if 'relationships' in item: item['relationships'] = order_dict(item['relationships'], self.relationship_order) return order_dict(item, RESOURCE_OBJECT_ORDER)
def data(self): links = None data = [] included = [] included_set = set() default_type = type_from_model_name(self.default_model.__name__) default_serializer = get_schema(default_type) for instance in self.collection: type_ = type_from_model_name(instance.__class__.__name__) serializer_class = get_schema(type_) if not issubclass(serializer_class.opts.model, self.base): serializer_class = default_serializer serialized = serializer_class(instance=instance, **self.kwargs).data if not links and 'links' in serialized: links = serialized['links'] data.append(serialized['data']) for include in serialized.get('included', []): key = (include['type'], include['id']) if key not in included_set: included.append(include) included_set.add(key) response = {'data': data} if links: response['links'] = links if included: included.sort(key=itemgetter('type', 'id')) response['included'] = included return order_dict(response, TOP_LEVEL_OBJECT_ORDER)
def data(self): links = None data = [] included = [] included_set = set() default_type = type_from_model_name(self.default_model.__name__) default_serializer = get_schema(default_type) for instance in self.collection: type_ = type_from_model_name(instance.__class__.__name__) serializer_class = get_schema(type_) if not issubclass(serializer_class.opts.model, self.base): serializer_class = default_serializer serialized = serializer_class(instance=instance, **self.kwargs).data if not links and 'links' in serialized: links = serialized['links'] data.append(serialized['data']) for include in serialized.get('included', []): key = (include['type'], include['id']) if key not in included_set: included.append(include) included_set.add(key) response = { 'data': data } if links: response['links'] = links if included: included.sort(key=itemgetter('type', 'id')) response['included'] = included return order_dict(response, TOP_LEVEL_OBJECT_ORDER)
def format_json_api_response(self, data, many): updated_set = set() deleted_set = set() updated = [] deleted = [] for instance in self.deleted: if instance.data_key in deleted_set: continue deleted_set.add(instance.data_key) if instance != self.instance: deleted.append(instance) for instance in self.updated: if (instance.data_key in updated_set or instance.data_key in deleted_set): continue updated_set.add(instance.data_key) updated.append(instance) self.add_includes(updated) response = super(JsonApiSerializer, self).format_json_api_response(data, many) if 'included' in response: response['included'].sort(key=itemgetter('type', 'id')) deleted = self.format_profile_references(deleted) if deleted: self.add_profile_to_response(DELETED_PROFILE, DELETED_PROFILE_ALIAS, deleted, response) updated = self.format_profile_references(updated) if updated: self.add_profile_to_response(UPDATES_PROFILE, UPDATES_PROFILE_ALIAS, updated, response) if (isinstance(self.instance, Model) and self.instance.data_key in deleted_set): response.pop('data', None) if not response.get('meta'): return {} return order_dict(response, TOP_LEVEL_OBJECT_ORDER)
def format_json_api_response(self, data, many): updated_set = set() deleted_set = set() updated = [] deleted = [] for instance in self.deleted: if instance.data_key in deleted_set: continue deleted_set.add(instance.data_key) if instance != self.instance: deleted.append(instance) for instance in self.updated: if (instance.data_key in updated_set or instance.data_key in deleted_set): continue updated_set.add(instance.data_key) updated.append(instance) self.add_includes(updated) response = super(JsonApiSerializer, self).format_json_api_response( data, many) if 'included' in response: response['included'].sort(key=itemgetter('type', 'id')) deleted = self.format_profile_references(deleted) if deleted: self.add_profile_to_response(DELETED_PROFILE, DELETED_PROFILE_ALIAS, deleted, response) updated = self.format_profile_references(updated) if updated: self.add_profile_to_response(UPDATES_PROFILE, UPDATES_PROFILE_ALIAS, updated, response) if (isinstance(self.instance, Model) and self.instance.data_key in deleted_set): response.pop('data', None) if not response.get('meta'): return {} return order_dict(response, TOP_LEVEL_OBJECT_ORDER)
def _serialize(self, value, attr, obj): data = super(Relationship, self)._serialize(value, attr, obj) if 'links' in data: data['links'] = order_dict(data['links'], LINKS_OBJECT_ORDER) return order_dict(data, TOP_LEVEL_OBJECT_ORDER)
def get_resource_linkage(self, value): data = super(Relationship, self).get_resource_linkage(value) if self.many: return [order_dict(item, TOP_LEVEL_OBJECT_ORDER) for item in data] return order_dict(data, TOP_LEVEL_OBJECT_ORDER)
def _serialize(self, value, attr, obj): data = super(Relationship, self)._serialize(value, attr, obj) if 'links' in data: data['links'] = order_dict(data['links'], LINKS_OBJECT_ORDER) return order_dict(data, TOP_LEVEL_OBJECT_ORDER)
def get_resource_linkage(self, value): data = super(Relationship, self).get_resource_linkage(value) if self.many: return [order_dict(item, TOP_LEVEL_OBJECT_ORDER) for item in data] return order_dict(data, TOP_LEVEL_OBJECT_ORDER)