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
6ba5bb3b
Commit
6ba5bb3b
authored
7 years ago
by
Sylvain Le Bon
Browse files
Options
Downloads
Patches
Plain Diff
feature: slug fields lookup
parent
b82bdd5f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
serializers.py
+10
-0
10 additions, 0 deletions
serializers.py
views.py
+8
-5
8 additions, 5 deletions
views.py
with
18 additions
and
5 deletions
serializers.py
+
10
−
0
View file @
6ba5bb3b
from
django.core.urlresolvers
import
get_resolver
from
rest_framework.relations
import
HyperlinkedRelatedField
from
rest_framework.serializers
import
HyperlinkedModelSerializer
,
ListSerializer
,
CharField
from
rest_framework.serializers
import
HyperlinkedModelSerializer
,
ListSerializer
,
CharField
from
rest_framework.utils.serializer_helpers
import
ReturnDict
from
rest_framework.utils.serializer_helpers
import
ReturnDict
...
@@ -11,6 +13,14 @@ class ContainerSerializer(ListSerializer):
...
@@ -11,6 +13,14 @@ class ContainerSerializer(ListSerializer):
class
LDPSerializer
(
HyperlinkedModelSerializer
):
class
LDPSerializer
(
HyperlinkedModelSerializer
):
url_field_name
=
"
@id
"
url_field_name
=
"
@id
"
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
LDPSerializer
,
self
).
__init__
(
*
args
,
**
kwargs
)
for
name
,
field
in
self
.
fields
.
items
():
if
isinstance
(
field
,
HyperlinkedRelatedField
):
lookup_field
=
get_resolver
().
reverse_dict
[
field
.
view_name
][
0
][
0
][
1
][
0
]
field
.
lookup_field
=
lookup_field
field
.
lookup_url_kwarg
=
lookup_field
@classmethod
@classmethod
def
many_init
(
cls
,
*
args
,
**
kwargs
):
def
many_init
(
cls
,
*
args
,
**
kwargs
):
kwargs
[
'
child
'
]
=
cls
()
kwargs
[
'
child
'
]
=
cls
()
...
...
This diff is collapsed.
Click to expand it.
views.py
+
8
−
5
View file @
6ba5bb3b
from
pyld
import
jsonld
from
pyld
import
jsonld
from
django.apps
import
apps
from
django.conf
import
settings
from
django.conf
import
settings
from
django.conf.urls
import
url
from
django.conf.urls
import
url
from
django.utils.decorators
import
classonlymethod
from
django.utils.decorators
import
classonlymethod
...
@@ -27,7 +28,7 @@ class LDPViewSet(ModelViewSet):
...
@@ -27,7 +28,7 @@ class LDPViewSet(ModelViewSet):
def
__init__
(
self
,
model
,
**
kwargs
):
def
__init__
(
self
,
model
,
**
kwargs
):
class_attrs
=
{
'
Meta
'
:
type
(
'
Meta
'
,
(),
{
'
model
'
:
model
,
'
exclude
'
:
()})}
class_attrs
=
{
'
Meta
'
:
type
(
'
Meta
'
,
(),
{
'
model
'
:
model
,
'
exclude
'
:
()})}
self
.
serializer_class
=
type
(
LDPSerializer
)(
model
.
_
_name__
+
'
Serializer
'
,
(
LDPSerializer
,),
class_attrs
)
self
.
serializer_class
=
type
(
LDPSerializer
)(
model
.
_
meta
.
object_name
.
lower
()
+
'
Serializer
'
,
(
LDPSerializer
,),
class_attrs
)
super
().
__init__
(
model
=
model
,
**
kwargs
)
super
().
__init__
(
model
=
model
,
**
kwargs
)
def
get_queryset
(
self
,
*
args
,
**
kwargs
):
def
get_queryset
(
self
,
*
args
,
**
kwargs
):
...
@@ -39,12 +40,14 @@ class LDPViewSet(ModelViewSet):
...
@@ -39,12 +40,14 @@ class LDPViewSet(ModelViewSet):
@classonlymethod
@classonlymethod
def
urls
(
cls
,
**
kwargs
):
def
urls
(
cls
,
**
kwargs
):
if
isinstance
(
kwargs
[
'
model
'
],
str
):
if
isinstance
(
kwargs
[
'
model
'
],
str
):
model
=
kwargs
[
'
model
'
].
split
(
'
.
'
)[
-
1
].
lower
()
kwargs
[
'
model
'
]
=
apps
.
get_model
(
kwargs
[
'
model
'
])
else
:
model
=
kwargs
[
'
model
'
].
_meta
.
object_name
.
lower
()
model
=
kwargs
[
'
model
'
].
_meta
.
object_name
.
lower
()
detail_url
=
r
'
^(?P<pk>\d+)$
'
if
kwargs
.
get
(
'
lookup_field
'
):
detail_url
=
r
'
^(?P<{}>[\w-]+)$
'
.
format
(
kwargs
[
'
lookup_field
'
])
return
[
return
[
url
(
r
'
^$
'
,
cls
.
as_view
({
'
get
'
:
'
list
'
,
'
post
'
:
'
create
'
},
**
kwargs
),
name
=
'
{}-list
'
.
format
(
model
)),
url
(
r
'
^$
'
,
cls
.
as_view
({
'
get
'
:
'
list
'
,
'
post
'
:
'
create
'
},
**
kwargs
),
name
=
'
{}-list
'
.
format
(
model
)),
url
(
r
'
^(?P<pk>\d+)$
'
,
cls
.
as_view
({
'
get
'
:
'
retrieve
'
,
'
put
'
:
'
update
'
,
'
patch
'
:
'
partial_update
'
,
'
delete
'
:
'
destroy
'
},
**
kwargs
),
name
=
'
{}-detail
'
.
format
(
model
)),
url
(
detail_url
,
cls
.
as_view
({
'
get
'
:
'
retrieve
'
,
'
put
'
:
'
update
'
,
'
patch
'
:
'
partial_update
'
,
'
delete
'
:
'
destroy
'
},
**
kwargs
),
name
=
'
{}-detail
'
.
format
(
model
)),
]
]
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
...
...
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