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
0d1923da
Commit
0d1923da
authored
6 years ago
by
Sylvain Le Bon
Browse files
Options
Downloads
Patches
Plain Diff
feature: auto author (fix
#12
)
parent
b1a63670
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
__init__.py
+1
-1
1 addition, 1 deletion
__init__.py
views.py
+56
-51
56 additions, 51 deletions
views.py
with
57 additions
and
52 deletions
__init__.py
+
1
−
1
View file @
0d1923da
from
django.db.models
import
options
from
django.db.models
import
options
options
.
DEFAULT_NAMES
+=
(
'
rdf_type
'
,)
options
.
DEFAULT_NAMES
+=
(
'
rdf_type
'
,
'
auto_author
'
)
This diff is collapsed.
Click to expand it.
views.py
+
56
−
51
View file @
0d1923da
...
@@ -26,59 +26,10 @@ class NoCSRFAuthentication(SessionAuthentication):
...
@@ -26,59 +26,10 @@ class NoCSRFAuthentication(SessionAuthentication):
def
enforce_csrf
(
self
,
request
):
def
enforce_csrf
(
self
,
request
):
return
return
class
LDPViewSetGenerator
(
ModelViewSet
):
class
LDPViewSet
(
ModelViewSet
):
model
=
None
fields
=
None
exclude
=
None
parent_model
=
None
nested_field
=
None
nested_related_name
=
None
list_actions
=
{
'
get
'
:
'
list
'
,
'
post
'
:
'
create
'
}
detail_actions
=
{
'
get
'
:
'
retrieve
'
,
'
put
'
:
'
update
'
,
'
patch
'
:
'
partial_update
'
,
'
delete
'
:
'
destroy
'
}
renderer_classes
=
(
JSONLDRenderer
,
)
parser_classes
=
(
JSONLDParser
,
)
authentication_classes
=
(
NoCSRFAuthentication
,)
def
__init__
(
self
,
**
kwargs
):
super
().
__init__
(
**
kwargs
)
model_name
=
self
.
model
.
_meta
.
object_name
.
lower
()
lookup_field
=
get_resolver
().
reverse_dict
[
model_name
+
'
-detail
'
][
0
][
0
][
1
][
0
]
meta_args
=
{
'
model
'
:
self
.
model
,
'
extra_kwargs
'
:
{
'
@id
'
:
{
'
lookup_field
'
:
lookup_field
}}}
if
self
.
fields
:
meta_args
[
'
fields
'
]
=
self
.
fields
else
:
meta_args
[
'
exclude
'
]
=
self
.
exclude
or
()
meta_class
=
type
(
'
Meta
'
,
(),
meta_args
)
self
.
serializer_class
=
type
(
LDPSerializer
)(
model_name
+
'
Serializer
'
,
(
LDPSerializer
,),
{
'
Meta
'
:
meta_class
})
def
get_parent
(
self
):
return
self
.
parent_model
.
objects
.
get
(
id
=
self
.
kwargs
[
self
.
lookup_field
])
def
perform_create
(
self
,
serializer
):
if
self
.
parent_model
:
serializer
.
validated_data
[
self
.
nested_related_name
]
=
self
.
get_parent
()
return
super
().
perform_create
(
serializer
)
def
get_queryset
(
self
,
*
args
,
**
kwargs
):
if
self
.
parent_model
:
return
getattr
(
self
.
get_parent
(),
self
.
nested_field
).
all
()
if
self
.
model
:
return
self
.
model
.
objects
.
all
()
else
:
return
super
(
LDPView
,
self
).
get_queryset
(
*
args
,
**
kwargs
)
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
response
=
super
(
LDPViewSet
,
self
).
dispatch
(
request
,
*
args
,
**
kwargs
)
response
[
"
Access-Control-Allow-Origin
"
]
=
request
.
META
.
get
(
'
HTTP_ORIGIN
'
)
response
[
"
Access-Control-Allow-Methods
"
]
=
"
POST,PUT
"
response
[
"
Access-Control-Allow-Headers
"
]
=
"
Content-Type, if-match
"
response
[
"
Access-Control-Allow-Credentials
"
]
=
'
true
'
response
[
"
Accept-Post
"
]
=
"
application/ld+json
"
return
response
@classonlymethod
@classonlymethod
def
get_model
(
cls
,
**
kwargs
):
def
get_model
(
cls
,
**
kwargs
):
'''
gets the model in the arguments or in the viewset definition
'''
model
=
kwargs
.
get
(
'
model
'
)
or
cls
.
model
model
=
kwargs
.
get
(
'
model
'
)
or
cls
.
model
if
isinstance
(
model
,
str
):
if
isinstance
(
model
,
str
):
model
=
apps
.
get_model
(
model
)
model
=
apps
.
get_model
(
model
)
...
@@ -86,6 +37,7 @@ class LDPViewSet(ModelViewSet):
...
@@ -86,6 +37,7 @@ class LDPViewSet(ModelViewSet):
@classonlymethod
@classonlymethod
def
get_detail_url
(
cls
,
lookup_field
=
None
,
base_url
=
''
,
**
kwargs
):
def
get_detail_url
(
cls
,
lookup_field
=
None
,
base_url
=
''
,
**
kwargs
):
'''
builds the detail url based on the lookup_field
'''
lookup_field
=
lookup_field
or
kwargs
.
get
(
'
lookup_field
'
)
or
cls
.
lookup_field
lookup_field
=
lookup_field
or
kwargs
.
get
(
'
lookup_field
'
)
or
cls
.
lookup_field
if
lookup_field
and
lookup_field
!=
'
pk
'
:
if
lookup_field
and
lookup_field
!=
'
pk
'
:
return
r
'
{}(?P<{}>[\w-]+)/
'
.
format
(
base_url
,
lookup_field
)
return
r
'
{}(?P<{}>[\w-]+)/
'
.
format
(
base_url
,
lookup_field
)
...
@@ -126,3 +78,56 @@ class LDPViewSet(ModelViewSet):
...
@@ -126,3 +78,56 @@ class LDPViewSet(ModelViewSet):
url
(
r
'
^$
'
,
cls
.
as_view
(
cls
.
list_actions
,
**
kwargs
),
name
=
'
{}-list
'
.
format
(
model_name
)),
url
(
r
'
^$
'
,
cls
.
as_view
(
cls
.
list_actions
,
**
kwargs
),
name
=
'
{}-list
'
.
format
(
model_name
)),
url
(
detail_url
+
'
$
'
,
cls
.
as_view
(
cls
.
detail_actions
,
**
kwargs
),
name
=
'
{}-detail
'
.
format
(
model_name
)),
url
(
detail_url
+
'
$
'
,
cls
.
as_view
(
cls
.
detail_actions
,
**
kwargs
),
name
=
'
{}-detail
'
.
format
(
model_name
)),
]
+
cls
.
get_nested_urls
(
detail_url
,
model_name
,
**
kwargs
))
]
+
cls
.
get_nested_urls
(
detail_url
,
model_name
,
**
kwargs
))
class
LDPViewSet
(
LDPViewSetGenerator
):
model
=
None
fields
=
None
exclude
=
None
parent_model
=
None
nested_field
=
None
nested_related_name
=
None
list_actions
=
{
'
get
'
:
'
list
'
,
'
post
'
:
'
create
'
}
detail_actions
=
{
'
get
'
:
'
retrieve
'
,
'
put
'
:
'
update
'
,
'
patch
'
:
'
partial_update
'
,
'
delete
'
:
'
destroy
'
}
renderer_classes
=
(
JSONLDRenderer
,
)
parser_classes
=
(
JSONLDParser
,
)
authentication_classes
=
(
NoCSRFAuthentication
,)
def
__init__
(
self
,
**
kwargs
):
super
().
__init__
(
**
kwargs
)
model_name
=
self
.
model
.
_meta
.
object_name
.
lower
()
lookup_field
=
get_resolver
().
reverse_dict
[
model_name
+
'
-detail
'
][
0
][
0
][
1
][
0
]
meta_args
=
{
'
model
'
:
self
.
model
,
'
extra_kwargs
'
:
{
'
@id
'
:
{
'
lookup_field
'
:
lookup_field
}}}
if
self
.
fields
:
meta_args
[
'
fields
'
]
=
self
.
fields
else
:
meta_args
[
'
exclude
'
]
=
self
.
exclude
or
()
meta_class
=
type
(
'
Meta
'
,
(),
meta_args
)
self
.
serializer_class
=
type
(
LDPSerializer
)(
model_name
+
'
Serializer
'
,
(
LDPSerializer
,),
{
'
Meta
'
:
meta_class
})
def
get_parent
(
self
):
return
self
.
parent_model
.
objects
.
get
(
id
=
self
.
kwargs
[
self
.
lookup_field
])
def
perform_create
(
self
,
serializer
):
create_args
=
{}
if
self
.
parent_model
:
create_args
[
self
.
nested_related_name
]
=
self
.
get_parent
()
if
hasattr
(
self
.
model
.
_meta
,
'
auto_author
'
):
create_args
[
self
.
model
.
_meta
.
auto_author
]
=
self
.
request
.
user
serializer
.
save
(
**
create_args
)
def
get_queryset
(
self
,
*
args
,
**
kwargs
):
if
self
.
parent_model
:
return
getattr
(
self
.
get_parent
(),
self
.
nested_field
).
all
()
if
self
.
model
:
return
self
.
model
.
objects
.
all
()
else
:
return
super
(
LDPView
,
self
).
get_queryset
(
*
args
,
**
kwargs
)
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
response
=
super
(
LDPViewSet
,
self
).
dispatch
(
request
,
*
args
,
**
kwargs
)
response
[
"
Access-Control-Allow-Origin
"
]
=
request
.
META
.
get
(
'
HTTP_ORIGIN
'
)
response
[
"
Access-Control-Allow-Methods
"
]
=
"
POST,PUT
"
response
[
"
Access-Control-Allow-Headers
"
]
=
"
Content-Type, if-match
"
response
[
"
Access-Control-Allow-Credentials
"
]
=
'
true
'
response
[
"
Accept-Post
"
]
=
"
application/ld+json
"
return
response
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