Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
rpteam
Revue de Press
Commits
2c0989b3
Commit
2c0989b3
authored
Jul 09, 2019
by
Okhin
Browse files
There's no need for the custom mixin now
parent
05309fd3
Changes
1
Hide whitespace changes
Inline
Side-by-side
apps/rp/api/mixins.py
deleted
100644 → 0
View file @
05309fd3
from
django_fsm
import
has_transition_perm
,
can_proceed
from
rest_framework.decorators
import
action
from
rest_framework.exceptions
import
PermissionDenied
from
rest_framework.response
import
Response
import
inspect
def
get_transition_viewset_method
(
model
,
transition_name
):
@
action
(
methods
=
[
'post'
],
detail
=
True
)
def
inner_func
(
self
,
request
,
pk
=
None
,
*
args
,
**
kwargs
):
object
=
self
.
get_object
()
transition_method
=
getattr
(
object
,
transition_name
)
if
not
can_proceed
(
transition_method
):
raise
PermissionDenied
if
not
has_transition_perm
(
transition_method
,
request
.
user
):
raise
PermissionDenied
if
'by'
in
inspect
.
getargspec
(
transition_method
).
args
:
transition_method
(
*
args
,
by
=
request
.
user
,
**
kwargs
)
else
:
transition_method
(
*
args
,
**
kwargs
)
if
self
.
save_after_transition
:
object
.
save
()
serializer
=
self
.
get_serializer
(
object
)
return
Response
(
serializer
.
data
)
return
inner_func
def
get_viewset_transition_actions_mixin
(
model
):
"""
Automatically generate methods for Django REST Framework from transition
rules.
"""
instance
=
model
()
class
Mixin
(
object
):
save_after_transition
=
True
transitions
=
instance
.
get_all_status_transitions
()
transition_names
=
set
(
x
.
name
for
x
in
transitions
)
for
transition_name
in
transition_names
:
setattr
(
Mixin
,
transition_name
,
get_transition_viewset_method
(
model
,
transition_name
))
return
Mixin
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment