# Generated by Django 5.0.3 on 2024-04-24 11:44

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


def migrate_status(apps, schema_editor):
    ImportSource = apps.get_model("io", "ImportSource")
    ImportSource.objects.filter(status="IMPORTED").update(status="PROCESSED")


class Migration(migrations.Migration):
    dependencies = [
        ("contenttypes", "0002_remove_content_type_name"),
        ("io", "0003_auto_20240103_1000"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.RunSQL(sql="SET CONSTRAINTS ALL IMMEDIATE;"),
        migrations.RunPython(migrate_status),
        migrations.RunSQL(sql="SET CONSTRAINTS ALL DEFERRED;"),
        migrations.AlterField(
            model_name="importsource",
            name="status",
            field=models.CharField(
                choices=[
                    ("PENDING", "Pending"),
                    ("PROCESSED", "Processed"),
                    ("WARNING", "Warning"),
                    ("ERROR", "Error"),
                    ("IGNORED", "Ignore"),
                ],
                default="PENDING",
                max_length=16,
            ),
        ),
        migrations.AddField(
            model_name="importsource",
            name="errors_log",
            field=models.TextField(blank=True, null=True),
        ),
        migrations.CreateModel(
            name="ExportSource",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("last_updated", models.DateTimeField(auto_now=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("PENDING", "Pending"),
                            ("PROCESSED", "Processed"),
                            ("WARNING", "Warning"),
                            ("ERROR", "Error"),
                            ("IGNORED", "Ignore"),
                        ],
                        default="PENDING",
                        max_length=16,
                    ),
                ),
                ("log", models.TextField(blank=True, null=True)),
                ("origin", models.CharField(blank=True, max_length=255, null=True)),
                ("file", models.FileField(blank=True, max_length=256, null=True, upload_to="io/export_source/files")),
                (
                    "format",
                    models.IntegerField(
                        choices=[
                            (0, "CSV"),
                            (1, "XLS"),
                            (2, "XLSX"),
                            (3, "TSV"),
                            (4, "ODS"),
                            (5, "JSON"),
                            (6, "YAML"),
                            (7, "HTML"),
                        ],
                        default=0,
                        verbose_name="Format of file to be exported",
                    ),
                ),
                (
                    "resource_path",
                    models.CharField(default="", max_length=255, verbose_name="Resource path to use when exporting"),
                ),
                ("resource_kwargs", models.JSONField(blank=True, default=dict)),
                ("query_str", models.TextField(verbose_name="SQL query to be executed")),
                (
                    "query_params",
                    models.JSONField(
                        blank=True,
                        default=list,
                        encoder=django.core.serializers.json.DjangoJSONEncoder,
                        verbose_name="SQL query parameters to be used with the sql query",
                    ),
                ),
                (
                    "content_type",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="contenttypes.contenttype",
                        verbose_name="Export job Content Type",
                    ),
                ),
                (
                    "creator",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Creator",
                    ),
                ),
            ],
            options={
                "verbose_name": "Export Source",
                "verbose_name_plural": "Export Sources",
                "notification_types": [
                    (
                        "io.export_done",
                        "File Export has finished",
                        "Notifies user when their submitted export files is done and available",
                        True,
                        True,
                        True,
                    )
                ],
            },
        ),
    ]
