Search code examples
djangopython-3.xdjango-viewflow

running flow through code Django-Viewflow


I want to run my process entirely by code. I've managed to start a process but a I can't run the next part of the process. I tried using the "flow.Function" and calling my desired function but i't says

'Function {} should be called with task instance', 'execute'" 

and the documentation on this subject is'nt very clear.

flows.py

@flow.flow_start_func
def create_flow(activation, campos_proceso, **kwargs):
    activation.process.asignador = campos_proceso['asignador']
    activation.process.ejecutor = campos_proceso['ejecutor']
    activation.process.tipo_de_flujo = campos_proceso['tipo_de_flujo']
    activation.process.estado_del_entregable = campos_proceso[
    'estado_del_entregable']
    activation.process.save()
    activation.prepare()
    activation.done()
    return activation

@flow.flow_func
def exec_flow(activation, process_fields, **kwargs):
    activation.process.revisor = process_fields['revisor']
    activation.process.save()
    activation.prepare()
    activation.done()
    return activation

@frontend.register
class Delivery_flow(Flow):
    process_class = DeliveryProcess
    start = flow.StartFunction(create_flow).Next(this.execute)
    execute = flow.Function(exec_flow).Next(this.end)
    end = flow.End()

views.py

def Execute(request): #campos_ejecucion, request):
    campos_ejecucion = {
    'ejecutor':request.user,
    'revisor':request.user,
    'observaciones_ejecutor':'Este es un puente magico',
    'url_ejecucion':'https://www.youtube.com/watch?v=G-yNGb0Q91Y',
    }
    campos_proceso = {
    'revisor':campos_ejecucion['revisor']
    }

    flows.Delivery_flow.execute.run()
    Entregable.objects.crear_entregable()
    return render(request, "Flujo/landing.html")

Solution

  • Generally, running "entirely by code" is the antipattern and should be avoided. Flow class is the set of views bound with URLs, so it's like class-based URL config, you don't need to have one more addition separate view and URL entry.

    For custom views, you can take a look at the cookbook sample - https://github.com/viewflow/cookbook/blob/master/custom_views/demo/bloodtest/views.py

    As for the actual question, you have missed task_loader. Function node should figure out what task is actually executed. You can do it on the flow layer (with task_loader) or directly get the Task model instance and pass it as the function parameter - http://docs.viewflow.io/viewflow_flow_nodes.html#viewflow.flow.Function