# Generated by Django 4.1.8 on 2023-04-28 14:57
import uuid

import django.core.serializers.json
import django.db.models.deletion
import wbcore.contrib.icons.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ("directory", "0004_entry_is_draft_entry"),
        ("contenttypes", "0002_remove_content_type_name"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("authentication", "0005_user_external_calendar_settings"),
    ]

    operations = [
        migrations.CreateModel(
            name="Workflow",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=128, unique=True, verbose_name="Name")),
                (
                    "single_instance_execution",
                    models.BooleanField(
                        default=False,
                        help_text="Indicates wether the workflow is restricted to only one running process per workflow or attached model instance",
                        verbose_name="Single Instance Execution",
                    ),
                ),
                (
                    "status_field",
                    models.CharField(
                        blank=True,
                        help_text="The status field name in the corresponding model that needs to be updated by the workflow",
                        max_length=64,
                        null=True,
                        verbose_name="Status Field",
                    ),
                ),
                (
                    "model",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="contenttypes.contenttype",
                        verbose_name="Model",
                    ),
                ),
                (
                    "graph",
                    models.ImageField(blank=True, null=True, upload_to="", verbose_name="Graph"),
                ),
            ],
            options={
                "verbose_name": "Workflow",
                "verbose_name_plural": "Workflows",
            },
        ),
        migrations.AddField(
            model_name="workflow",
            name="preserve_instance",
            field=models.BooleanField(
                default=False,
                help_text="This will preserve the attached instance's state in a JSON field when starting the workflow. You then have the option to write this data back onto the instance in a finish step, effectively preserving the instance's state throughout the workflow.",
                verbose_name="Preserve Instance",
            ),
        ),
        migrations.CreateModel(
            name="Step",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=128, verbose_name="Name")),
                ("code", models.PositiveIntegerField(verbose_name="Code")),
                (
                    "status",
                    models.CharField(
                        blank=True,
                        help_text="The status that will be set in the attached model upon transitioning to this step",
                        max_length=64,
                        null=True,
                        verbose_name="Status",
                    ),
                ),
                (
                    "workflow",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="workflow.workflow",
                        verbose_name="Workflow",
                        related_name="associated_steps",
                    ),
                ),
                (
                    "step_type",
                    models.CharField(
                        choices=[
                            ("StartStep", "Start Step"),
                            ("UserStep", "User Step"),
                            ("DecisionStep", "Decision Step"),
                            ("SplitStep", "Split Step"),
                            ("JoinStep", "Join Step"),
                            ("EmailStep", "Email Step"),
                            ("ScriptStep", "Script Step"),
                            ("FinishStep", "Finish Step"),
                        ],
                        default="",
                        max_length=64,
                        verbose_name="Step Type",
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        blank=True,
                        help_text="Define which permission is needed to be able to view this step being executed.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="related_steps",
                        to="authentication.permission",
                        verbose_name="Permission",
                    ),
                ),
            ],
            options={
                "verbose_name": "Step",
                "verbose_name_plural": "Steps",
            },
        ),
        migrations.AddConstraint(
            model_name="step",
            constraint=models.UniqueConstraint(fields=("name", "workflow"), name="unique_name_workflow"),
        ),
        migrations.AddConstraint(
            model_name="step",
            constraint=models.UniqueConstraint(fields=("code", "workflow"), name="unique_code_workflow"),
        ),
        migrations.CreateModel(
            name="Transition",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=128, verbose_name="Name")),
            ],
            options={
                "verbose_name": "Transition",
                "verbose_name_plural": "Transitions",
            },
        ),
        migrations.AddField(
            model_name="transition",
            name="from_step",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="outgoing_transitions",
                to="workflow.step",
                verbose_name="From",
            ),
        ),
        migrations.AddField(
            model_name="transition",
            name="to_step",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="incoming_transitions",
                to="workflow.step",
                verbose_name="To",
            ),
        ),
        migrations.AddField(
            model_name="transition",
            name="icon",
            field=wbcore.contrib.icons.models.IconField(default="start", max_length=128, verbose_name="Icon"),
        ),
        migrations.AddConstraint(
            model_name="transition",
            constraint=models.UniqueConstraint(fields=("name", "to_step"), name="unique_name_to_step"),
        ),
        migrations.CreateModel(
            name="Condition",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "attribute_name",
                    models.CharField(
                        help_text="The name of the workflow data or attached model field whose value should be checked",
                        max_length=64,
                        verbose_name="Attribute Name",
                    ),
                ),
                (
                    "operator",
                    models.CharField(
                        choices=[
                            ("<", "Less Than"),
                            ("<=", "Less Than or Equal"),
                            ("==", "Equal"),
                            (">=", "Greater Than or Equal"),
                            (">", "Greater Than"),
                        ],
                        max_length=64,
                        verbose_name="Operator",
                    ),
                ),
                (
                    "negate_operator",
                    models.BooleanField(default=False, verbose_name="Negate Operator"),
                ),
                (
                    "expected_value",
                    models.CharField(
                        help_text="The expected attribute's value",
                        max_length=64,
                        verbose_name="Expected Value",
                    ),
                ),
                (
                    "transition",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="associated_conditions",
                        to="workflow.transition",
                        verbose_name="Transition",
                    ),
                ),
            ],
            options={
                "verbose_name": "Condition",
                "verbose_name_plural": "Conditions",
            },
        ),
        migrations.AddConstraint(
            model_name="condition",
            constraint=models.UniqueConstraint(
                fields=("transition", "attribute_name", "operator", "negate_operator", "expected_value"),
                name="unique_transition_attribute_name_operator_negate_expected_value",
            ),
        ),
        migrations.CreateModel(
            name="StartStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
            ],
            options={
                "verbose_name": "Start Step",
                "verbose_name_plural": "Start Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.CreateModel(
            name="UserStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
                ("notify_user", models.BooleanField(default=True, verbose_name="Notify User")),
                (
                    "assignee",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="assigned_workflow_steps",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Assignee",
                    ),
                ),
                (
                    "group",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="assigned_workflow_steps",
                        to="authentication.group",
                        verbose_name="Group",
                    ),
                ),
            ],
            options={
                "verbose_name": "User Step",
                "verbose_name_plural": "User Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.AddField(
            model_name="userstep",
            name="kwargs",
            field=models.JSONField(blank=True, null=True, verbose_name="Kwargs"),
        ),
        migrations.AddField(
            model_name="userstep",
            name="assignee_method",
            field=models.CharField(
                blank=True,
                choices=[
                    ("manager_of_instance_assignee", "Instance Assignee's Manager"),
                    ("weighted_random", "Weighted Random Group Member"),
                    ("random_group_member", "Random Group Member"),
                    ("activity_assignee", "Activity Assignee"),
                ],
                max_length=64,
                null=True,
                verbose_name="Assignee Method",
            ),
        ),
        migrations.AddConstraint(
            model_name="userstep",
            constraint=models.CheckConstraint(
                check=models.Q(("assignee__isnull", False), ("group__isnull", False), _negated=True),
                name="check_not_both_assignee_group",
            ),
        ),
        migrations.CreateModel(
            name="Process",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name="UUID"
                    ),
                ),
                ("started", models.DateTimeField(auto_now_add=True, verbose_name="Started")),
                ("finished", models.DateTimeField(blank=True, null=True, verbose_name="Finished", editable=False)),
                (
                    "instance_id",
                    models.CharField(blank=True, editable=False, max_length=128, null=True, verbose_name="Instance"),
                ),
                (
                    "content_type",
                    models.ForeignKey(
                        blank=True,
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="contenttypes.contenttype",
                        verbose_name="Content Type",
                    ),
                ),
                (
                    "workflow",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        to="workflow.workflow",
                        verbose_name="Workflow",
                        editable=False,
                    ),
                ),
            ],
            options={
                "verbose_name": "Process",
                "verbose_name_plural": "Processes",
            },
        ),
        migrations.AddField(
            model_name="process",
            name="preserved_instance",
            field=models.JSONField(
                blank=True,
                encoder=django.core.serializers.json.DjangoJSONEncoder,
                null=True,
                verbose_name="Preserved Instance",
            ),
        ),
        migrations.AddField(
            model_name="process",
            name="state",
            field=models.CharField(
                choices=[("Active", "Active"), ("Finished", "Finished"), ("Failed", "Failed")],
                default="Active",
                editable=False,
                help_text="The current state of this process. Can be one of: 'Active', 'Finished' or 'Failed'",
                max_length=64,
                verbose_name="State",
            ),
        ),
        migrations.CreateModel(
            name="ProcessStep",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name="UUID"
                    ),
                ),
                ("started", models.DateTimeField(auto_now_add=True, verbose_name="Started")),
                ("finished", models.DateTimeField(blank=True, editable=False, null=True, verbose_name="Finished")),
                (
                    "state",
                    models.CharField(
                        choices=[
                            ("Active", "Active"),
                            ("Finished", "Finished"),
                            ("Waiting", "Waiting"),
                            ("Failed", "Failed"),
                            ("Canceled", "Canceled"),
                        ],
                        editable=False,
                        help_text="The current state of this process step. Can be one of: 'Active', 'Finished', 'Waiting', 'Failed' or 'Canceled'",
                        max_length=64,
                        verbose_name="State",
                        default="Active",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        blank=True,
                        editable=False,
                        help_text="The status that will be set in the attached model upon transitioning to this step",
                        max_length=64,
                        null=True,
                        verbose_name="Status",
                    ),
                ),
                (
                    "assignee",
                    models.ForeignKey(
                        blank=True,
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="assigned_process_steps",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Assignee",
                    ),
                ),
                (
                    "group",
                    models.ForeignKey(
                        blank=True,
                        editable=False,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="assigned_process_steps",
                        to="authentication.group",
                        verbose_name="Group",
                    ),
                ),
                (
                    "permission",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="related_process_steps",
                        to="authentication.permission",
                        verbose_name="Permission",
                        editable=False,
                        help_text="The permission needed to be able to view this step being executed.",
                    ),
                ),
                (
                    "process",
                    models.ForeignKey(
                        editable=False,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="process_steps",
                        to="workflow.process",
                        verbose_name="Process",
                    ),
                ),
                (
                    "step",
                    models.ForeignKey(
                        editable=False,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="process_steps",
                        to="workflow.step",
                        verbose_name="Step",
                    ),
                ),
                (
                    "error_message",
                    models.CharField(
                        blank=True,
                        editable=False,
                        help_text="An error message will be displayed when this step fails",
                        max_length=128,
                        null=True,
                        verbose_name="Error Message",
                    ),
                ),
            ],
            options={"verbose_name": "Process Step", "verbose_name_plural": "Process Steps"},
        ),
        migrations.CreateModel(
            name="Display",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("grid_template_areas", models.JSONField(verbose_name="Grid Fields")),
                ("name", models.CharField(max_length=128, unique=True, verbose_name="Name")),
            ],
            options={
                "verbose_name": "Display",
                "verbose_name_plural": "Displays",
            },
        ),
        migrations.AddField(
            model_name="userstep",
            name="display",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="user_steps",
                to="workflow.display",
                verbose_name="Display",
            ),
        ),
        migrations.CreateModel(
            name="DecisionStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
            ],
            options={"verbose_name": "Decision Step", "verbose_name_plural": "Decision Steps"},
            bases=("workflow.step",),
        ),
        migrations.CreateModel(
            name="SplitStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
            ],
            options={
                "verbose_name": "Split Step",
                "verbose_name_plural": "Split Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.CreateModel(
            name="FinishStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
            ],
            options={
                "verbose_name": "Finish Step",
                "verbose_name_plural": "Finish Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.AddField(
            model_name="finishstep",
            name="write_preserved_instance",
            field=models.BooleanField(
                default=False,
                help_text="Writes the instance data backup onto the instance restoring its state to the beginning of the workflow.",
                verbose_name="Write Preserved Instance",
            ),
        ),
        migrations.AlterField(
            model_name="step",
            name="step_type",
            field=models.CharField(
                choices=[
                    ("UserStep", "User Step"),
                    ("DecisionStep", "Decision Step"),
                    ("SplitStep", "Split Step"),
                    ("JoinStep", "Join Step"),
                    ("EmailStep", "Email Step"),
                    ("ScriptStep", "Script Step"),
                    ("FinishStep", "Finish Step"),
                ],
                default="",
                max_length=64,
                verbose_name="Step Type",
            ),
        ),
        migrations.CreateModel(
            name="JoinStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
                (
                    "wait_for_all",
                    models.BooleanField(
                        default=True,
                        help_text="If False, will cancel all other incoming process steps as soon the first reaches this step.",
                        verbose_name="Wait For All",
                    ),
                ),
            ],
            options={
                "verbose_name": "Join Step",
                "verbose_name_plural": "Join Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.CreateModel(
            name="ScriptStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
                ("script", models.TextField(verbose_name="Script")),
            ],
            options={
                "verbose_name": "Script Step",
                "verbose_name_plural": "Script Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.CreateModel(
            name="EmailStep",
            fields=[
                (
                    "step_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="workflow.step",
                    ),
                ),
                (
                    "template",
                    models.FileField(
                        max_length=256, upload_to="workflow/templates/Test_Templates.txt", verbose_name="Template"
                    ),
                ),
                ("subject", models.CharField(max_length=128, verbose_name="Subject")),
                (
                    "to",
                    models.ManyToManyField(
                        related_name="workflow_step_receiver", to="directory.emailcontact", verbose_name="To"
                    ),
                ),
                (
                    "cc",
                    models.ManyToManyField(
                        related_name="workflow_steps_cc", to="directory.emailcontact", verbose_name="CC", blank=True
                    ),
                ),
                (
                    "bcc",
                    models.ManyToManyField(
                        related_name="workflow_steps_bcc", to="directory.emailcontact", verbose_name="BCC", blank=True
                    ),
                ),
            ],
            options={
                "verbose_name": "Email Step",
                "verbose_name_plural": "Email Steps",
            },
            bases=("workflow.step",),
        ),
        migrations.CreateModel(
            name="Data",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("label", models.CharField(max_length=64, verbose_name="Label")),
                ("help_text", models.CharField(blank=True, max_length=128, null=True, verbose_name="Help Text")),
                (
                    "data_type",
                    models.CharField(
                        choices=[
                            ("Text", "Text"),
                            ("Number", "Number"),
                            ("Date", "Date"),
                            ("Date Time", "Date Time"),
                            ("Boolean", "Boolean"),
                        ],
                        default="Text",
                        max_length=64,
                        verbose_name="Data Type",
                    ),
                ),
                ("required", models.BooleanField(verbose_name="Required")),
                ("default", models.CharField(max_length=128, verbose_name="Default", blank=True, null=True)),
                (
                    "workflow",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="attached_data",
                        to="workflow.workflow",
                        verbose_name="Workflow",
                    ),
                ),
            ],
            options={
                "verbose_name": "Data",
                "verbose_name_plural": "Data",
            },
        ),
        migrations.AddConstraint(
            model_name="data",
            constraint=models.UniqueConstraint(fields=("label", "workflow"), name="unique_label_workflow"),
        ),
        migrations.CreateModel(
            name="DataValue",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("value", models.CharField(max_length=64, verbose_name="Value", blank=True, null=True)),
                (
                    "data",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="values",
                        to="workflow.data",
                        verbose_name="Data",
                    ),
                ),
                (
                    "process",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="data_values",
                        to="workflow.process",
                        verbose_name="Process",
                    ),
                ),
            ],
            options={
                "verbose_name": "Data Value",
                "verbose_name_plural": "Data Values",
            },
        ),
    ]
