Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Objects #51

Open
symbiosdotwiki opened this issue May 16, 2016 · 2 comments
Open

Multiple Objects #51

symbiosdotwiki opened this issue May 16, 2016 · 2 comments

Comments

@symbiosdotwiki
Copy link

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.

@symbiosdotwiki
Copy link
Author

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()

@mord4z
Copy link
Contributor

mord4z commented Apr 17, 2017

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.
sem titulo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants