# Generated by Django 3.2.15 on 2022-08-24 08:02

import django.db.models.deletion
import djangocms_text_ckeditor.fields
from django.conf import settings
from django.db import migrations, models


def set_content_type(apps, schema_editor):
    """Set the content type for the existing material."""
    from academic_community.uploaded_material.models import (
        mark_old_file_for_deletion,
    )

    Material = apps.get_model("uploaded_material", "Material")
    for material in Material.objects.all():
        mark_old_file_for_deletion(sender=Material, instance=material)
        material.save()


class Migration(migrations.Migration):

    dependencies = [
        ("auth", "0012_alter_user_first_name_max_length"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("uploaded_material", "0004_auto_20220406_1002"),
    ]

    operations = [
        migrations.CreateModel(
            name="CommunityMaterial",
            fields=[
                (
                    "material_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="uploaded_material.material",
                    ),
                ),
                (
                    "group_change_permission",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Groups with write permission (this does not include read permission!)",
                        related_name="communitymaterial_write",
                        to="auth.Group",
                    ),
                ),
                (
                    "group_view_permission",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Groups with read permission",
                        related_name="communitymaterial_read",
                        to="auth.Group",
                    ),
                ),
                (
                    "user_change_permission",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Users with explicit write permission (this does not include read permission!)",
                        related_name="communitymaterial_write",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "user_view_permission",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Users with explicit read permission",
                        related_name="communitymaterial_read",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            bases=("uploaded_material.material",),
        ),
        migrations.CreateModel(
            name="MaterialKeyword",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Name of the keyword", max_length=100
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="material",
            name="keywords",
            field=models.ManyToManyField(
                blank=True,
                help_text="Keywords for this material.",
                to="uploaded_material.MaterialKeyword",
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="content_type",
            field=models.CharField(
                blank=True,
                help_text="Content type of the material. If not set, it will be taken automatically for non-external files",
                max_length=200,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="description",
            field=djangocms_text_ckeditor.fields.HTMLField(
                blank=True,
                help_text="Further description on the material.",
                max_length=20000,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="md5sum",
            field=models.CharField(
                blank=True,
                help_text="The md5-Checksum of the uploaded file. If you leave this empty, it will be computed from the uploaded file.",
                max_length=32,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="sha256sum",
            field=models.CharField(
                blank=True,
                help_text="The sha256-Checksum of the uploaded file. If you leave this empty, it will be computed from the uploaded file.",
                max_length=64,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="material",
            name="file_size",
            field=models.PositiveBigIntegerField(
                blank=True,
                help_text="Size of the uploaded file. If you leave this empty, it will be computed from the uploaded file.",
                null=True,
            ),
        ),
        migrations.RunPython(set_content_type, migrations.RunPython.noop),
    ]
