Search code examples
pythondjangoworkflowdjango-viewflow

Canceling a process Django-ViewFlow


I'm creating an aplication for my company's workflow. Doing so I've arrived to the necesity of having some way to cancel process instances in case the user who created the flow AKA "Asignador" requieres it. I've tried using the "CancelProcessView" But it can only cancel the tasks that are unassigned. Since my flow uses the ".Assign()" method all tasks are assigned automatically and hence are uncancelable.

Flows.py

    @frontend.register
    class Delivery_flow(Flow):
        process_class = DeliveryProcess

        iniciar = (
            flow.Start(
                CreateProcessView,
                fields=["nombre_del_entregable", 'url_del_proyecto', 'Asignador', 'ejecutor',
                'tipo_de_flujo','observaciones_asignador',
                "fecha_inicio", 'fecha_de_terminacion'],
                task_description='Asignacion de entregable'
            ).Next(this.split_editar_ejecutar)
        )

        split_editar_ejecutar = (
            flow.Split()
            .Next(this.editar_proceso)
            .Next(this.ejecutar)
        )

        editar_proceso = (
            flow.View(
                UpdateProcessView,
                    fields=[ "nombre_del_entregable", "fecha_de_terminacion",
                    "ejecutor", 'observaciones_asignador', 'url_del_proyecto', 'estado_del_proyecto'],
                    task_description="Tarea para editar algun campo"
                    ).Assign(lambda act: act.process.Asignador
                    ).Next(this.split_terminar_editar)

        )

        cancelar_proceso =  (
            flow.View(CancelProcessView,
                     ).Assign(lambda act: act.process.Asignador)
        )


        split_terminar_editar = (
            flow.If(lambda act: act.process.estado_del_proyecto)
            .Then(this.editar_proceso)
            .Else(this.cancelar_proceso)

        )

        ejecutar = (
            flow.View(
                UpdateProcessView,
                    fields=[ "oberservaciones_ejecutor", "fecha_de_ejecucion",
                    "revisor", "finalizado"],
                    task_description="Ejecucion"
                ).Assign(lambda act: act.process.ejecutor
                ).Next(this.revisor_check)
        )

        revisor_check = (
            flow.View(
            views.ReView,
            ).Assign(lambda act: act.process.revisor) #.Assign(lambda act: act.process.nuevo_revisor)
             .Next(this.add_review)
        )

        add_review = (
            flow.View(
            UpdateProcessView,
            fields=['estado_de_aprobacion', 'revisor'],
            task_description='Conclusion sobre el entregable'
            ).Next(this.split)
        )

        split = (
            #If(lambda activation: activation.process.aprobacion_final)
            flow.Switch()
            .Case(this.join, cond=lambda act: act.process.estado_de_aprobacion=='APROBACION_FINAL')
            .Case(this.revisor_check,cond=lambda act: act.process.estado_de_aprobacion=='APROBACION_NUEVO_REVISOR')
            .Case(this.ejecutar, cond=lambda act: act.process.estado_de_aprobacion=='DEVOLVER_EJECUTOR')
        )

        join = (
            flow.Join(wait_all=True)
            .Next(this.finalizacion)
            )

        finalizacion= (
                flow.View(
                UpdateProcessView,
                fields=['fecha_de_terminacion_real', 'comentarios_finales'],
                task_description= 'Estado final'
                ).Assign(lambda act: act.process.revisor
                ).Next(this.end)
            )

        end = flow.End()

        def send_hello_world_request(self, activation):
            print(activation.process.text)

I've also tried using the "AssignTaskView" in a process state prior to the one that needs to be assigned, but it only enters a state that I cant modify and that dosen't move on. How can I properly assign my tasks properly to use the CancelProcess without having to manually unassign and also how does one use such view correctly? Thanks in advance.


Solution

  • Generally, in the BPMN all normal process path should be modeled explicitly. So you need to remodel the process in that way such no canceling process should happen in real life, a process should just when to the end state.

    Canceling process functionality available for the rare cases of process admin work. Since assigned task could be executed by a person at this moment, default viewflow nodes do not allow to cancel assigned tasks. An assigned task should be reassigned first. To allow to cancel assigned nodes, you can change the unassing view (so it would automatically try to unassing a task) or create custom flow.View with activation class that allows canceling in the assigned state.