Example #1
0
    def save(self, commit=True):
        instance = super().save(commit)
        instance.content_type = instance.task.content_type

        if instance.state == WorkflowTaskInstance.State.OPEN and instance.closed_at:
            """
            В случае, если задача имеет статус "OPEN" и укзано время закрытия, то
            меняем статус на "CLOSED" и создаем следующую по порядку задачу.
            """
            instance.state = WorkflowTaskInstance.State.CLOSED
            try:
                templates = WorkflowTask.objects.filter(order__gt=instance.task.order, last_version__isnull=True)
                task_template = templates.order_by('order')[0:1].get()
                now = datetime.datetime.now()

                if task_template.content_type == instance.content_type:
                    object_id = instance.object_id
                else:
                    object_id = None

                task = WorkflowTaskInstance(
                    task=task_template,
                    process=instance.process,
                    performer=get_task_performer(task_template, instance.process),
                    due_date=now + datetime.timedelta(
                        hours=task_template.due_time.hour,
                        minutes=task_template.due_time.minute,
                        seconds=task_template.due_time.second),
                    opened_at=now,
                    content_type=task_template.content_type,
                    object_id=object_id)
                task.save()
            except ObjectDoesNotExist:
                pass

            if instance.task.after_close_command:
                try:
                    management.call_command(instance.task.after_close_command.command,
                                            interactive=False)
                except CommandError:
                    pass
            instance.save()

        return instance
 def save_model(self, request, obj, form, change):
     pk = obj.pk
     super().save_model(request, obj, form, change)
     if not pk:
         """
         Создание и назначение первой задачи при запуске рабочего процесса
         """
         try:
             templates = WorkflowTask.objects.filter(process=obj.process, last_version__isnull=True)
             task_template = templates.order_by('order')[0:1].get()
             now = datetime.datetime.now()
             task = WorkflowTaskInstance(
                 task=task_template,
                 process=obj,
                 performer=get_task_performer(task_template, obj),
                 due_date=now + datetime.timedelta(
                     hours=task_template.due_time.hour,
                     minutes=task_template.due_time.minute,
                     seconds=task_template.due_time.second),
                 opened_at=now,
                 content_type=task_template.content_type)
             task.save()
         except ObjectDoesNotExist:
             pass