How to set redirect route for django viewflow end?

184 views Asked by At

How can I specify custom url where the user is redirected when flow ends in flow.End()?

I tried to pass custom views with overridden get_success_url() to End node, but it was not working.

flows.py


end = flow.End(
    cancel_view=views.WeightTicketEndCancelView,
    detail_view=views.WeightTicketEndDetailView,
    undo_view=views.WeightTicketEndUndoView,
    perform_view=views.WeightTicketEndPerformView,
    view_or_class=views.WeightTicketEndView,
)

views.py

class WeightTicketEndPerformView(PerformTaskView):

    def get_success_url(self):
        return "/test/"

3

There are 3 answers

1
yuvraj On

idk much about viewflows but to redirect, you need to import redirect

from django.shortcuts import render, redirect

and then, to redirect, do:

return redirect('your_url_or_url_name')
0
Martin Javorka On

Thank you kmmbvnr for your explanation where I need to update get_success_url.

The issue was, that get the desired url after the latest user task was executed. Specifically I want to redirect user from one flow to the first task of the other.

I solved the issue with storing the next_url in the process model:



# flows.py
class FirstFlow(Flow):
    process_class = FirstFlowProcess

    start = flow.Start(CreateProcessView).Next(this.start_second_flow)

    start_second_flow = (
        flow.View(CustomProcessView, fields=["start_second_flow"])
        .Next(this.check_start_second_flow)
    )

    check_start_second_flow = (
        flow.If(lambda activation: activation.process.start_second_flow)
        .Then(this.create_second_flow)
        .Else(this.end)
    )

    create_second_flow = flow.Handler(this.init_second_flow).Next(this.end)

    end = flow.End()

    def init_second_flow(self, activation):
        second_flow_activation = SecondFlow.start.run()
        activation.process.next_url = get_first_task_url(activation=second_flow_activation)
        activation.process.save()


class SecondFlow(Flow):

    start = flow.StartFunction(this.start_second_process).Next(this.next_task)

    next_task = (flow...)

    @method_decorator(flow.flow_start_func)
    def start_second_process(self, activation):
        activation.prepare()
        activation.done()
        return activation

def get_first_task_url(activation):
    first_task = activation.process.active_tasks().first()
    return first_task.flow_task.get_task_url(
        first_task,
        url_type='guess',
        user=User.objects.filter(is_superuser=True).order_by("?")[0],
        namespace="app_name:flow",
    )


# models.py
class FirstFlowProcess(Process):
    start_second_flow = models.BooleanField(default=False)
    next_url = models.CharField(max_length=1000, blank=True, null=True)


# views.py
class CustomProcessView(FlowMixin, generic.CreateView):
    template_name = "template_name.html"
    form_class = forms.FormClass
    
    def get_success_url(self):
        self.activation.process.refresh_from_db()  # update changes from the database
        if self.activation.process.next_url:
            return self.activation.process.next_url
        else:
            return super(CustomProcessView, self).get_success_url()

    def form_valid(self, form):
        self.activation_done()
        return redirect(self.get_success_url())


0
kmmbvnr On

End node just completes the flow. A user never been redirected to the end node.

You need to change `get_success_url of the the latest user task view