Multiple Objects
symbiosdotwiki opened this issue · comments
Isa commented
It would be great if there was a mixin to allow for transitions to multiple objects. I wrote up simple extension to the mixin that allows for this, but it's not very generic for generation of buttons. This is useful for creating ListViews where a user can transition multiple objects.
Isa commented
class MultipleFSMTransitionMixin(FSMTransitionMixin):
def _get_requested_transition(self, request):
"""
Extracts the name of the transition requested by user
"""
posts = []
for key in request.POST.keys():
if key.startswith(self.fsm_input_prefix):
fsm_input = key.split('-')
posts.append(
(fsm_input[1], fsm_input[2], fsm_input[3])
)
return posts
def fsm_post(self, request, *args, **kwargs):
objects = []
transitions = self._get_requested_transition(request)
for fsm_field, transition, obj_id in transitions:
if transition:
obj = self.model.objects.get(id=obj_id)
self._do_transition(transition, request, obj, None, fsm_field)
obj.save()
objects.append((obj, transition))
return objects
def button_name(self, transition, label):
if hasattr(transition, 'custom') and 'button_name' in transition.custom:
return transition.custom['button_name']
else:
# Make the function name the button title, but prettier
return '{0} {1}'.format(transition.name.replace('_',' '), label).title()
Allan George commented
I'm using just an admin action, in your admin:
def actionname(modeladmin, request, queryset):
for obj in queryset:
obj.dotransition(by=request.user)
obj.save()
actionname.short_description = "short description here"
In yout ModelAdmin class:
actions = [actionname,]
with this you can select multiple rows on admin and apply transition.