# Generated by Django 3.2.8 on 2022-02-10 18:12

from django.conf import settings
from django.contrib.admin.models import CHANGE
from django.db import migrations
from django.db.models import Q


def rename_typed_texts(apps, schema_editor):
    # rename all "typed texts" to "unpublished editions"
    # partially adapted from mep-django
    Source = apps.get_model("footnotes", "Source")
    ContentType = apps.get_model("contenttypes", "ContentType")
    LogEntry = apps.get_model("admin", "LogEntry")
    source_contenttype = ContentType.objects.get(app_label="footnotes", model="source")
    User = apps.get_model("auth", "User")
    (user, _) = User.objects.get_or_create(
        username=settings.SCRIPT_USERNAME, is_staff=False, is_active=False
    )  # generic user for script execution (should already be created)

    # get all sources called "typed texts"
    typed_texts = Source.objects.filter(
        Q(title_en="typed texts") | Q(title="typed texts")
    )
    # preserve as a list so queryset doesn't erase our set of records
    update_pks = list(typed_texts.values_list("pk", flat=True))
    # do a bulk update to replace the titles
    typed_texts.update(title="unpublished editions", title_en="unpublished editions")
    # get fresh copies of the updated records and create log entries
    for source in Source.objects.filter(pk__in=update_pks):
        # log action
        LogEntry.objects.log_action(
            user_id=user.id,
            content_type_id=source_contenttype.pk,
            object_id=source.pk,
            object_repr=str(source),
            change_message='changed title "typed texts" to "unpublished editions"',
            action_flag=CHANGE,
        )


class Migration(migrations.Migration):

    dependencies = [
        ("admin", "0003_logentry_add_action_flag_choices"),  # for LogEntry
        ("footnotes", "0015_add_footnote_location_pp"),
    ]

    operations = [
        migrations.RunPython(
            rename_typed_texts, reverse_code=migrations.RunPython.noop
        ),
    ]

# Generated by Django 3.2.8 on 2022-02-10 18:12

from django.conf import settings
from django.contrib.admin.models import CHANGE
from django.db import migrations
from django.db.models import Q


def rename_typed_texts(apps, schema_editor):
    # rename all "typed texts" to "unpublished editions"
    # partially adapted from mep-django
    Source = apps.get_model("footnotes", "Source")
    ContentType = apps.get_model("contenttypes", "ContentType")
    LogEntry = apps.get_model("admin", "LogEntry")
    source_contenttype = ContentType.objects.get(app_label="footnotes", model="source")
    User = apps.get_model("auth", "User")
    (user, _) = User.objects.get_or_create(
        username=settings.SCRIPT_USERNAME, is_staff=False, is_active=False
    )  # generic user for script execution (should already be created)

    # get all sources called "typed texts"
    typed_texts = Source.objects.filter(
        Q(title_en="typed texts") | Q(title="typed texts")
    )
    # preserve as a list so queryset doesn't erase our set of records
    update_pks = list(typed_texts.values_list("pk", flat=True))
    # do a bulk update to replace the titles
    typed_texts.update(title="unpublished editions", title_en="unpublished editions")
    # get fresh copies of the updated records and create log entries
    for source in Source.objects.filter(pk__in=update_pks):
        # log action
        LogEntry.objects.log_action(
            user_id=user.id,
            content_type_id=source_contenttype.pk,
            object_id=source.pk,
            object_repr=str(source),
            change_message='changed title "typed texts" to "unpublished editions"',
            action_flag=CHANGE,
        )


class Migration(migrations.Migration):

    dependencies = [
        ("admin", "0003_logentry_add_action_flag_choices"),  # for LogEntry
        ("footnotes", "0015_add_footnote_location_pp"),
    ]

    operations = [
        migrations.RunPython(
            rename_typed_texts, reverse_code=migrations.RunPython.noop
        ),
    ]

# Generated by Django 3.2.8 on 2022-02-10 18:12 # rename all "typed texts" to "unpublished editions" # partially adapted from mep-django # generic user for script execution (should already be created) # get all sources called "typed texts" # preserve as a list so queryset doesn't erase our set of records # do a bulk update to replace the titles # get fresh copies of the updated records and create log entries # log action # for LogEntry