# Generated by Django 4.1.8 on 2023-04-05 09:30

import uuid

import django.db.models.deletion
import wbcore.contrib.io.models
from django.db import migrations, models


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ("django_celery_beat", "0018_improve_crontab_helptext"),
        ("contenttypes", "0002_remove_content_type_name"),
    ]

    operations = [
        migrations.CreateModel(
            name="DataBackend",
            fields=[
                (
                    "id",
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
                ),
                ("title", models.CharField(max_length=255)),
                (
                    "save_data_in_import_source",
                    models.BooleanField(
                        default=True, help_text="If true, save the data in the import_source json field"
                    ),
                ),
                (
                    "passive_only",
                    models.BooleanField(
                        default=True,
                        help_text="If True, this data backend is allowed to be called only from the import source.",
                    ),
                ),
                ("backend_class_path", models.CharField(max_length=512)),
                ("backend_class_name", models.CharField(default="DataBackend", max_length=128)),
            ],
            options={
                "verbose_name": "Data Backend",
                "verbose_name_plural": "Data Backends",
            },
        ),
        migrations.CreateModel(
            name="ImportCredential",
            fields=[
                (
                    "id",
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
                ),
                ("key", models.CharField(max_length=255)),
                (
                    "type",
                    models.CharField(
                        choices=[
                            ("CREDENTIAL", "Credential"),
                            ("AUTHENTICATION_TOKEN", "Authentication Token"),
                            ("CERTIFICATE", "Certificate"),
                        ],
                        default="CREDENTIAL",
                        max_length=64,
                    ),
                ),
                ("username", models.CharField(blank=True, max_length=255, null=True)),
                ("password", models.CharField(blank=True, max_length=255, null=True)),
                ("authentication_token", models.CharField(blank=True, max_length=2048, null=True)),
                (
                    "certificate_pem",
                    models.FileField(
                        blank=True,
                        help_text="We are expecting a .cert file",
                        max_length=256,
                        null=True,
                        upload_to="io/import_credential/certificates",
                        validators=[wbcore.contrib.io.models.validate_pem_file],
                    ),
                ),
                (
                    "certificate_key",
                    models.FileField(
                        blank=True,
                        help_text="We are expecting a .key file",
                        max_length=256,
                        null=True,
                        upload_to="io/import_credential/certificates",
                        validators=[wbcore.contrib.io.models.validate_key_file],
                    ),
                ),
                ("additional_resources", models.JSONField(blank=True, default=dict, null=True)),
                ("validity_start", models.DateTimeField(blank=True, null=True)),
                ("validity_end", models.DateTimeField(blank=True, null=True)),
            ],
            options={
                "verbose_name": "Import Credential",
                "verbose_name_plural": "Import Credentials",
            },
        ),
        migrations.CreateModel(
            name="ParserHandler",
            fields=[
                (
                    "id",
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
                ),
                ("parser", models.CharField(max_length=255)),
                ("handler", models.CharField(max_length=255)),
                (
                    "allow_file_type",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("text/csv", "CSV"),
                            ("application/json", "JSON"),
                            ("application/vnd.ms-excel", "XLS"),
                            ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "XLSX"),
                        ],
                        max_length=68,
                        null=True,
                    ),
                ),
            ],
            options={
                "verbose_name": "Parser-Handler",
                "verbose_name_plural": "Parsers-Handlers",
                "unique_together": {("parser", "handler")},
            },
        ),
        migrations.CreateModel(
            name="Provider",
            fields=[
                (
                    "id",
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
                ),
                ("title", models.CharField(max_length=255)),
                ("key", models.CharField(max_length=255, unique=True)),
            ],
            options={
                "verbose_name": "Provider",
                "verbose_name_plural": "Providers",
            },
        ),
        migrations.CreateModel(
            name="Source",
            fields=[
                (
                    "id",
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
                ),
                ("title", models.CharField(max_length=128)),
                ("uuid", models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
                ("is_active", models.BooleanField(default=True)),
                ("connection_parameters", models.JSONField(blank=True, default=dict, null=True)),
                ("import_parameters", models.JSONField(blank=True, default=dict, null=True)),
                ("import_timedelta_interval", models.IntegerField(default=0)),
                (
                    "credentials",
                    models.ManyToManyField(blank=True, related_name="sources", to="io.importcredential"),
                ),
                (
                    "crontab",
                    models.ForeignKey(
                        blank=True,
                        help_text="Crontab Schedule to run the task on.  Set only one schedule type, leave the others null.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="django_celery_beat.crontabschedule",
                        verbose_name="Crontab Schedule",
                    ),
                ),
                (
                    "data_backend",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="io.databackend",
                        verbose_name="Data Backend",
                    ),
                ),
                (
                    "parser_handler",
                    models.ManyToManyField(
                        related_name="sources", to="io.parserhandler", verbose_name="Parser/Handler"
                    ),
                ),
                (
                    "periodic_task",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="sources",
                        to="django_celery_beat.periodictask",
                        verbose_name="Periodic Task",
                    ),
                ),
            ],
            options={
                "verbose_name": "Source",
                "verbose_name_plural": "Sources",
            },
        ),
        migrations.CreateModel(
            name="ImportSource",
            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"),
                            ("IMPORTED", "Imported"),
                            ("ERROR", "Error"),
                            ("IGNORED", "Ignore"),
                        ],
                        default="PENDING",
                        max_length=16,
                    ),
                ),
                (
                    "file",
                    models.FileField(blank=True, max_length=256, null=True, upload_to="io/import_source/files"),
                ),
                ("data", models.JSONField(blank=True, default=dict, null=True)),
                (
                    "save_data",
                    models.BooleanField(default=True, help_text="If True, will save the raw data in a field"),
                ),
                ("progress_index", models.PositiveIntegerField(default=0)),
                ("log", models.TextField(blank=True, null=True)),
                ("origin", models.CharField(blank=True, max_length=255, null=True)),
                (
                    "parser_handler",
                    models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="io.parserhandler"),
                ),
                (
                    "source",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="io.source",
                        verbose_name="Source",
                    ),
                ),
            ],
            options={
                "verbose_name": "Import Source",
                "verbose_name_plural": "Import Sources",
            },
        ),
        migrations.CreateModel(
            name="ImportedObjectProviderRelationship",
            fields=[
                (
                    "id",
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
                ),
                ("computed_str", models.CharField(blank=True, max_length=512, null=True, verbose_name="Name")),
                ("object_id", models.PositiveIntegerField()),
                ("provider_identifier", models.CharField(max_length=255)),
                (
                    "content_type",
                    models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="contenttypes.contenttype"),
                ),
                (
                    "provider",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="imported_object_relationships",
                        to="io.provider",
                    ),
                ),
            ],
            options={
                "verbose_name": "Content object Provider Identifier relationship",
                "verbose_name_plural": "Content object Provider Identifier relationships",
            },
        ),
        migrations.AddField(
            model_name="databackend",
            name="provider",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to="io.provider",
                verbose_name="Provider",
            ),
        ),
        migrations.AddIndex(
            model_name="importedobjectproviderrelationship",
            index=models.Index(
                fields=["content_type", "object_id", "provider"], name="io_imported_content_18b6a8_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="importedobjectproviderrelationship",
            index=models.Index(
                fields=["content_type", "provider_identifier", "provider"],
                name="io_imported_content_3da41e_idx",
            ),
        ),
        migrations.AlterUniqueTogether(
            name="importedobjectproviderrelationship",
            unique_together={
                ("content_type", "provider_identifier", "provider"),
                ("content_type", "object_id", "provider"),
            },
        ),
    ]
