Search code examples
pythondjangoworkflowdjango-viewflow

viewflow.io: How can I present a custom form for gathering input for a specific task?


I would like to use ViewFlow (see http://viewflow.io/) for a user registration process, where I ask for the user's password (and password2 for confirmation). I don't want to store the plain password but I would like to immediately create the user and assign the password, instead of storing that data in the Process.


Solution

  • You don't bound to store all raw data inside the process model. Process model is the root entry for all process related data. Just add ForeignKey to user inside you custom Process model

    from django.contrib.auth.models import User
    from viewflow.models import Process
    
    class NewUserProcess(Process):
        user = models.ForeignKey(User, blank=True, null=True)
    

    For the forms processing, viewflow doesn't add anything new to standard django. As usual you can user django standard class based views or plain functional based views, with any form processing code that you need. Here is the example for functional based views:

    from viewflow import flow
    from viewflow.base import Flow
    from viewflow.flow import flow_start_view
    
    
    class NewUserFlow(Flow):
         registration = flow.StartView(registraton_view).Next(...)
    
    
    @flow_start_view
    def registraton_view(request, activation):
         activation.prepare(request.POST or None, user=request.user)
         form = RegistrationForm(request.POST or None)
    
         if form.is_valid():
             activation.process.user = form.save()
             activation.done()
             return redirect(...)
    
         return render(request, 'registration.html', {
            'form': form,
            'activation': activation,
         })
    

    For the case of class based views, you can specify custom form class as usual:

    from viewflow.flow.views import StartFlowMixin
    
    class RegistrationView(StartFlowMixin, generic.CreateView):
        form_class = RegistrationForm
    
        def activation_done(self, form):
            self.activation.process.user = self.object
            self.activation.done()
    

    In addition, you can check viewflow custom views demo source code - https://github.com/viewflow/cookbook/blob/master/custom_views/demo/bloodtest/views.py