Beispiel #1
0
    def save(self):
        started = self.validated_data['started']
        steps = self.validated_data.get('steps', [])
        completed = self.validated_data.get('completed', False)

        # look for previous run with this contact and flow
        run = (FlowRun.objects.filter(
            org=self.org,
            contact=self.contact_obj,
            flow=self.flow_obj,
            created_on=started,
            is_active=True).order_by('-modified_on').first())

        if not run or run.submitted_by != self.submitted_by_obj:
            run = FlowRun.create(self.flow_obj,
                                 self.contact_obj,
                                 created_on=started,
                                 submitted_by=self.submitted_by_obj)

        step_objs = [
            FlowStep.from_json(step, self.flow_obj, run) for step in steps
        ]

        if completed:
            final_step = step_objs[len(step_objs) - 1] if step_objs else None
            completed_on = steps[len(steps) -
                                 1]['arrived_on'] if steps else None

            run.set_completed(final_step, completed_on=completed_on)
        else:
            run.save(update_fields=('modified_on', ))

        return run
Beispiel #2
0
    def _create_runs(self, count, flow, contacts):
        """
        Creates the given number of flow runs
        """
        runs = []
        for c in range(0, count):
            contact = contacts[c % len(contacts)]
            runs.append(FlowRun.create(flow, contact.pk, db_insert=False))
        FlowRun.objects.bulk_create(runs)

        # add a step to each run
        steps = []
        for run in FlowRun.objects.all():
            steps.append(
                FlowStep(run=run,
                         contact=run.contact,
                         step_type='R',
                         step_uuid=flow.entry_uuid,
                         arrived_on=timezone.now()))
        FlowStep.objects.bulk_create(steps)

        return runs