Search code examples
pythondjangodjango-viewflow

Saving process data on another table in the database


I'm working on a process for my company. Doing this i have found it necesary to store data that is being inserted in the proccess. My process is working fine but instead of saving in my other model, it keeps on saving the data on the table that corresponds to the process and not my other model.

models.py:

from __future__ import unicode_literals
from datetime import datetime
from django.db import models
from viewflow.models import Process
from django.contrib.auth.models import User


class DeliveryProcess(Process):
    #### Asignador ###
    nombre_del_entregable= models.CharField(max_length=150)
    fecha_inicio=models.DateField(auto_now_add=False, default=datetime.now)
    fecha_de_terminacion=models.DateField(auto_now=False, default=datetime.now)
    ejecutor=models.ForeignKey(User,on_delete=models.CASCADE, null=True, related_name='+')
    observaciones_asignador=models.TextField()

    ##### Ejecutor #####
    echa_de_ejecucion=models.DateField(auto_now_add=False, default=datetime.now)
    oberservaciones_ejecutor=models.TextField()
    finalizado= models.BooleanField(default=False)
    revisor=models.ForeignKey(User,on_delete=models.CASCADE, null=True, related_name='+')
    aprobacion_final= models.BooleanField(default=False)
    anadir_revisor= models.BooleanField(default=False)

    ##### Revisor #####
    #    aprobacion_final= models.BooleanField(default=False)
    #    anadir_revisor= models.BooleanField(default=False)
    #    nuevo_revisor= models.ForeignKey(
    #    User,on_delete=models.CASCADE, null=True,  related_name='+')
    #    obserservaciones_revisor= models.TextField()

class Revisiones(models.Model):
    id_revision= models.AutoField(primary_key=True)
    fecha_de_revision=models.DateField(auto_now_add=False, default=datetime.now)
    revisor=models.ForeignKey(User,on_delete=models.CASCADE, null=True, related_name='+')
    aprobacion_final= models.BooleanField(default=False)
    anadir_revisor= models.BooleanField(default=False)
    observaciones_revisor= models.TextField()

flows.py:

from __future__ import unicode_literals
from viewflow import flow
from viewflow.base import this, Flow
from viewflow.flow.views import CreateProcessView, UpdateProcessView
from viewflow.lock import select_for_update_lock
from .models import DeliveryProcess
from viewflow import frontend
from . import views

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

    start = (
        flow.Start(
            CreateProcessView,
            fields=["nombre_del_entregable", "fecha_inicio", 'fecha_de_terminacion', 'ejecutor','observaciones_asignador']
        ).Next(this.ejecutar)
    )

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

    revisor_check = (
        flow.View(
        views.ReView,
        fields=["aprobacion_final",'anadir_revisor',"revisor",'observaciones_revisor', 'fecha_de_revision']
        ).Assign(lambda act: act.process.revisor)
            .Next(this.split)
    )

    split =(
        #If(lambda activation: activation.process.aprobacion_final)
        flow.Switch()
        .Case(this.end, cond=(lambda act: act.process.aprobacion_final))
        .Case(this.revisor_check, cond=(lambda act: act.process.anadir_revisor))
        .Case(this.ejecutar, cond=(not(lambda act: act.process.aprobacion_finale) and (lambda act: act.process.anadir_revisor)))
    )


    end = flow.End()

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

views.py:

from django.shortcuts import render
from django.views import generic
from viewflow.flow.views import CreateProcessView, UpdateProcessView
from .models import Revisiones
from viewflow.decorators import flow_start_view, flow_view
from viewflow.flow.views import StartFlowMixin, FlowViewMixin

class ReView(UpdateProcessView):
    model = Revisiones
    fields = ['revisor', 'observaciones_revisor']

The model I'd like to save my data in is "Revisiones" and the custom view I am using is ReView but dosent save on my other table.


Solution

  • All process views such as CreateProcessView or UpdateProcessView in Django Viewflow is connected to a Process model. This means that the view and the flow can keep track on meta data related to the process that gets added when you inherit from the Process model.

    In this case, you are trying to set a UpdateProcessView to a Revisiones model that does not inherit from Process but is simply just a normal models.Model.

    You are attempting to incorrectly set it by specifying model=Revisiones but if you open up the UpdateProcessView class you will see the following code:

    @property
    def model(self):
        """Process class."""
        return self.activation.flow_class.process_class
    

    As you can see, the model is always set to the Process class of the Flow.

    Instead what you will have to do is set the Revisiones data in other ways. Perhaps you create a handler in your flow that sets this data based on the Process data, or maybe you create override methods for form_valid or other View methods where you write custom logic to store these things.