Skip to content

Commit

Permalink
NEW: Excluded transitions added.
Browse files Browse the repository at this point in the history
  • Loading branch information
peykar committed Apr 12, 2022
1 parent e9a0b1c commit 3a41c6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ class ArticleViewSet(

If `request` parameter is defined as one of transition callable parameters, then request object will be passed to the transition callable.

### Excluded transitions
To exclude some transitions to be exposed, add transition name to `excluded_transitions` attribute.

```python
class ArticleViewSet(
get_drf_fsm_mixin(Article),
viewsets.ModelViewSet,
):
queryset = Article.objects.all()
excluded_transitions = ["foo_transition"]
```

### Customized response

To have customied response, add transition name to `return_result_of` attribute and return your desired response from your transittion callable.
Expand Down
6 changes: 5 additions & 1 deletion djangorestframework_fsm/viewset_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def transition_action(self, request, *args, **kwargs):
if not has_transition_perm(transition_method, self.request.user):
raise exceptions.PermissionDenied

if transition_name in self.excluded_transitions:
raise exceptions.PermissionDenied

if hasattr(self, 'get_{0}_kwargs'.format(transition_name)):
transition_kwargs = getattr(self, 'get_{0}_kwargs'.format(transition_name))()
else:
Expand Down Expand Up @@ -68,6 +71,7 @@ def get_drf_fsm_mixin(Model, fieldname='state'):
class Mixin(object):
save_after_transition = True
return_result_of = []
excluded_transitions = []

@action(methods=['GET'], detail=True, url_name='possible-transitions', url_path='possible-transitions')
def possible_transitions(self, request, *args, **kwargs):
Expand All @@ -77,7 +81,7 @@ def possible_transitions(self, request, *args, **kwargs):
'transitions': [
trans.name.replace('_', '-')
for trans in getattr(instance, 'get_available_{}_transitions'.format(fieldname))()
if trans.has_perm(instance, request.user)
if trans.has_perm(instance, request.user) and (trans.name not in self.excluded_transitions)
]
},
)
Expand Down

0 comments on commit 3a41c6f

Please sign in to comment.