gadventures / django-fsm-admin

Mixin and template tags to integrate django-fsm transitions into the django admin.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Show/hide transition buttons accordin to permission

mord4z opened this issue · comments

I need a way to access the transition's permission param to show/hide the button in template, any clue on how to do?
I believe that instead of displaying all buttons and treat permissions in model or view, it would be more elegant to hide those who do not have the necessary permissions on the template

Good idea! I don't know off the top of my head, but the Transition object gives you access to permissions (https://github.com/kmmbvnr/django-fsm/blob/master/django_fsm/__init__.py#L68)

Between that and _fsm_get_transitions (https://github.com/gadventures/django-fsm-admin/blob/master/fsm_admin/mixins.py#L48), should allow for some permission checking. The problem might be how to get a reference to the current user.

Maybe we can get the current user via session or via request?

I'm trying to use the request template processor, here's what I've done til now:
Added 'django.core.context_processors.request' to TEMPLATE_CONTEXT_PROCESSORS
Changed a bit the fsm_submit_row method, added these line:
request = context['request']
And passed one more param to _fsm_get_transitions:
transitions = model_admin.fsm_get_transitions(original,request)
On mixing the code is like:
user = request.user
transitions_func = 'get_available_user
{0}_transitions'.format(self.fsm_field)
transitions = getattr(obj, transitions_func)(user) if obj else []

Need more tests here!

Now I have a problem: the transition don't be automatic saved with the new version

@mord4z Is the mixin first in your inheritence chain?

Sorry, my mistake, I override the save_model method and forgot to pass super. The code is working now, I'll try another pull request.

Made a new pull request