# Generated by Django 3.0.3 on 2020-03-03 21:45

import uuid

from django.db import migrations, models


def create_self_signed(apps, schema_editor):
    CertificateKeyPair = apps.get_model("passbook_crypto", "CertificateKeyPair")
    db_alias = schema_editor.connection.alias
    from passbook.crypto.builder import CertificateBuilder

    builder = CertificateBuilder()
    builder.build()
    CertificateKeyPair.objects.using(db_alias).create(
        name="passbook Self-signed Certificate",
        certificate_data=builder.certificate,
        key_data=builder.private_key,
    )


class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="CertificateKeyPair",
            fields=[
                ("created", models.DateTimeField(auto_now_add=True)),
                ("last_updated", models.DateTimeField(auto_now=True)),
                (
                    "uuid",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("name", models.TextField()),
                ("certificate_data", models.TextField()),
                ("key_data", models.TextField(blank=True, default="")),
            ],
            options={
                "verbose_name": "Certificate-Key Pair",
                "verbose_name_plural": "Certificate-Key Pairs",
            },
        ),
        migrations.RunPython(create_self_signed),
        migrations.AlterField(
            model_name="certificatekeypair",
            name="certificate_data",
            field=models.TextField(help_text="PEM-encoded Certificate data"),
        ),
        migrations.AlterField(
            model_name="certificatekeypair",
            name="key_data",
            field=models.TextField(
                blank=True,
                default="",
                help_text="Optional Private Key. If this is set, you can use this keypair for encryption.",
            ),
        ),
    ]