Commit 2c0989b3 authored by Okhin's avatar Okhin
Browse files

There's no need for the custom mixin now

parent 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)
transition_method(*args, **kwargs)
if self.save_after_transition:
serializer = self.get_serializer(object)
return Response(
return inner_func
def get_viewset_transition_actions_mixin(model):
Automatically generate methods for Django REST Framework from transition
instance = model()
class Mixin(object):
save_after_transition = True
transitions = instance.get_all_status_transitions()
transition_names = set( for x in transitions)
for transition_name in transition_names:
setattr(Mixin, transition_name,
get_transition_viewset_method(model, transition_name))
return Mixin
Supports Markdown
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