# Generated by Django 4.0.4 on 2022-05-23 07:14

from django.db import migrations, models
import django.db.models.deletion


def init_paperless_tasks(apps, schema_editor):
    PaperlessTask = apps.get_model("documents", "PaperlessTask")
    Task = apps.get_model("django_q", "Task")

    for task in Task.objects.filter(func="documents.tasks.consume_file"):
        if not hasattr(task, "paperlesstask"):
            paperlesstask = PaperlessTask.objects.create(
                attempted_task=task,
                task_id=task.id,
                name=task.name,
                created=task.started,
                started=task.started,
                acknowledged=True,
            )
            task.paperlesstask = paperlesstask
            task.save()


class Migration(migrations.Migration):

    dependencies = [
        ("django_q", "0014_schedule_cluster"),
        ("documents", "1021_webp_thumbnail_conversion"),
    ]

    operations = [
        migrations.CreateModel(
            name="PaperlessTask",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("task_id", models.CharField(max_length=128)),
                ("name", models.CharField(max_length=256)),
                (
                    "created",
                    models.DateTimeField(auto_now=True, verbose_name="created"),
                ),
                (
                    "started",
                    models.DateTimeField(null=True, verbose_name="started"),
                ),
                ("acknowledged", models.BooleanField(default=False)),
                (
                    "attempted_task",
                    models.OneToOneField(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="attempted_task",
                        to="django_q.task",
                    ),
                ),
            ],
        ),
        migrations.RunPython(init_paperless_tasks, migrations.RunPython.noop),
    ]
