How to create a django ViewFlow process programmatically

前端 未结 3 1507
离开以前
离开以前 2020-12-02 21:26

Synopsis

I\'m developing a web application to learn Django (python 3.4 & Django 1.6.10). The web app has complex and often updated workflows. I decided

3条回答
  •  旧巷少年郎
    2020-12-02 21:58

    I needed to be able to manually or programmatically start a flow instance. The model I ended up with, based on the above reference to StartFunction looks like this:

    class MyRunFlow(flow.Flow):
        process_class = Run
    
        start = flow.Start(ProcessCreate, fields=['schedule']). \
            Permission(auto_create=True). \
            Next(this.wait_data_collect_start)
        start2 = flow.StartFunction(process_create). \
            Next(this.wait_data_collect_start)
    

    Note the important point is that process_create has the Process object and this code must programmatically set up the same fields that the manual form submission does via the fields specification to ProcessCreate:

    @flow_start_func
    def process_create(activation: FuncActivation, **kwargs):
        #
        # Update the database record.
        #
        db_sch = Schedule.objects.get(id=kwargs['schedule'])
        activation.process.schedule = db_sch # <<<< Same fields as ProcessCreate
        activation.process.save()
        #
        # Go!
        #
        activation.prepare()
        activation.done()
        return activation
    

    Note that the activation subclass inside the flow_start_func is FuncActivation, which has the prepare() and save() methods. The kwargs come from the call to run, which goes something like:

    start_node = 
    activation = start_node.run(schedule=self.id)
    

提交回复
热议问题