Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djangoldp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
djangoldp-packages
djangoldp
Commits
34f75af4
Commit
34f75af4
authored
5 years ago
by
Calum Mackervoy
Committed by
Jean-Baptiste Pasquier
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
ID returned only in `to_representation` for remote resources (on GET)
parent
7bac3db4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
djangoldp/serializers.py
+15
-1
15 additions, 1 deletion
djangoldp/serializers.py
with
15 additions
and
1 deletion
djangoldp/serializers.py
+
15
−
1
View file @
34f75af4
...
...
@@ -27,7 +27,7 @@ from djangoldp.permissions import LDPPermissions
class
LDListMixin
:
'''
A Mixin
used by the custom Serializers in this file
'''
'''
A Mixin
for serializing containers into JSONLD format
'''
child_attr
=
'
child
'
# converts primitive data representation to the representation used within our application
...
...
@@ -220,6 +220,7 @@ class JsonLdRelatedField(JsonLdField):
class
JsonLdIdentityField
(
JsonLdField
):
'''
Represents an identity (url) field for a serializer
'''
def
__init__
(
self
,
view_name
=
None
,
**
kwargs
):
kwargs
[
'
read_only
'
]
=
True
kwargs
[
'
source
'
]
=
'
*
'
...
...
@@ -229,6 +230,7 @@ class JsonLdIdentityField(JsonLdField):
return
False
def
to_internal_value
(
self
,
data
):
'''
tells serializer how to write identity field
'''
try
:
return
super
().
to_internal_value
(
data
[
self
.
parent
.
url_field_name
])
except
KeyError
:
...
...
@@ -238,9 +240,12 @@ class JsonLdIdentityField(JsonLdField):
return
super
().
get_value
(
dictionary
)
def
to_representation
(
self
,
value
:
Any
)
->
Any
:
'''
returns hyperlink representation of identity field
'''
try
:
# we already have a url to return
if
isinstance
(
value
,
str
):
return
Hyperlink
(
value
,
value
)
# expecting a user instance. Compute the webid and return this in hyperlink format
else
:
return
Hyperlink
(
value
.
webid
(),
value
)
except
AttributeError
:
...
...
@@ -250,6 +255,8 @@ class JsonLdIdentityField(JsonLdField):
if
Model
.
is_external
(
instance
):
return
instance
.
urlid
else
:
# runs DRF's RelatedField.get_attribute
# returns the pk only if optimised or the instance itself in the standard case
return
super
().
get_attribute
(
instance
)
...
...
@@ -274,11 +281,17 @@ class LDPSerializer(HyperlinkedModelSerializer):
return
fields
+
list
(
getattr
(
self
.
Meta
,
'
extra_fields
'
,
[]))
def
to_representation
(
self
,
obj
):
# external Models should only be returned with an id (on GET)
if
self
.
context
[
'
request
'
].
method
==
'
GET
'
and
Model
.
is_external
(
obj
):
return
{
'
@id
'
:
obj
.
urlid
}
data
=
super
().
to_representation
(
obj
)
slug_field
=
Model
.
slug_field
(
obj
)
for
field
in
data
:
if
isinstance
(
data
[
field
],
dict
)
and
'
@id
'
in
data
[
field
]:
data
[
field
][
'
@id
'
]
=
data
[
field
][
'
@id
'
].
format
(
Model
.
container_id
(
obj
),
str
(
getattr
(
obj
,
slug_field
)))
# prioritise urlid field over generated @id
if
'
urlid
'
in
data
and
data
[
'
urlid
'
]
is
not
None
:
data
[
'
@id
'
]
=
data
.
pop
(
'
urlid
'
)[
'
@id
'
]
if
not
'
@id
'
in
data
:
...
...
@@ -456,6 +469,7 @@ class LDPSerializer(HyperlinkedModelSerializer):
else
:
ret
=
super
().
to_internal_value
(
data
)
# copy url_field_name value to urlid, if necessary
if
self
.
url_field_name
in
data
and
not
'
urlid
'
in
data
and
data
[
self
.
url_field_name
].
startswith
(
'
http
'
):
ret
[
'
urlid
'
]
=
data
[
self
.
url_field_name
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment