Commit 109a4cf6 authored by Christophe Henry's avatar Christophe Henry

feat: Add support for defining a custom serializer

parent 48c16aeb
Pipeline #7122 passed with stage
in 1 minute and 15 seconds
...@@ -4,4 +4,4 @@ __version__ = '0.0.0' ...@@ -4,4 +4,4 @@ __version__ = '0.0.0'
options.DEFAULT_NAMES += ( options.DEFAULT_NAMES += (
'lookup_field', 'rdf_type', 'rdf_context', 'auto_author', 'owner_field', 'view_set', 'container_path', 'lookup_field', 'rdf_type', 'rdf_context', 'auto_author', 'owner_field', 'view_set', 'container_path',
'permission_classes', 'serializer_fields', 'nested_fields', 'depth', 'anonymous_perms', 'authenticated_perms', 'permission_classes', 'serializer_fields', 'nested_fields', 'depth', 'anonymous_perms', 'authenticated_perms',
'owner_perms') 'owner_perms', 'ldp_read_serializer', 'ldp_write_serializer')
from inspect import isclass
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
from django.conf.urls import url, include from django.conf.urls import include, url
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.exceptions import FieldDoesNotExist from django.core.exceptions import FieldDoesNotExist
from django.core.urlresolvers import get_resolver from django.core.urlresolvers import get_resolver
...@@ -20,7 +22,6 @@ from djangoldp.endpoints.webfinger import WebFingerEndpoint, WebFingerError ...@@ -20,7 +22,6 @@ from djangoldp.endpoints.webfinger import WebFingerEndpoint, WebFingerError
from djangoldp.models import LDPSource, Model from djangoldp.models import LDPSource, Model
from djangoldp.permissions import LDPPermissions from djangoldp.permissions import LDPPermissions
get_user_model()._meta.rdf_context = {"get_full_name": "rdfs:label"} get_user_model()._meta.rdf_context = {"get_full_name": "rdfs:label"}
...@@ -120,22 +121,39 @@ class LDPViewSet(LDPViewSetGenerator): ...@@ -120,22 +121,39 @@ class LDPViewSet(LDPViewSetGenerator):
def build_read_serializer(self): def build_read_serializer(self):
model_name = self.model._meta.object_name.lower() model_name = self.model._meta.object_name.lower()
lookup_field = get_resolver().reverse_dict[model_name + '-detail'][0][0][1][0] lookup_field = get_resolver().reverse_dict[model_name + '-detail'][0][0][1][0]
meta_args = {'model': self.model, 'extra_kwargs': { meta_args = {
'@id': {'lookup_field': lookup_field}}, "model": self.model,
'depth': getattr(self, 'depth', Model.get_meta(self.model, 'depth', 0)), "meta": self.model._meta,
'extra_fields': self.nested_fields} "extra_kwargs": {
"@id": {"lookup_field": lookup_field}
},
"depth": getattr(self, "depth", Model.get_meta(self.model, "depth", 0)),
"extra_fields": self.nested_fields
}
return self.build_serializer(meta_args, 'Read') return self.build_serializer(meta_args, 'Read')
def build_write_serializer(self): def build_write_serializer(self):
model_name = self.model._meta.object_name.lower() model_name = self.model._meta.object_name.lower()
lookup_field = get_resolver().reverse_dict[model_name + '-detail'][0][0][1][0] lookup_field = get_resolver().reverse_dict[model_name + "-detail"][0][0][1][0]
meta_args = {'model': self.model, 'extra_kwargs': { meta_args = {
'@id': {'lookup_field': lookup_field}}, "model": self.model,
'depth': 10, "meta": self.model._meta,
'extra_fields': self.nested_fields} "extra_kwargs": {
return self.build_serializer(meta_args, 'Write') "@id": {"lookup_field": lookup_field}
},
"depth": 10,
"extra_fields": self.nested_fields
}
return self.build_serializer(meta_args, "Write")
def build_serializer(self, meta_args, name_prefix): def build_serializer(self, meta_args, name_prefix):
# If the model specifies a custom write serializer, let's respect that choice
lower_name_prefix = name_prefix.lower()
ldp_serializer = getattr(meta_args["meta"], "ldp_{}_serializer".format(lower_name_prefix), None)
from djangoldp.serializers import LDPSerializer
if isclass(ldp_serializer) and issubclass(ldp_serializer, LDPSerializer):
return ldp_serializer
if self.fields: if self.fields:
meta_args['fields'] = self.fields meta_args['fields'] = self.fields
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment