"""Populate post._date field.

Generated by Django 5.2 on 2025-05-03 06:21
"""

from django.db import migrations


def populate__date(apps, schema_editor) -> None:
    """Populate the _date field for all posts.

    This function is used to populate the _date field for all posts
    in the database.

    Args:
        apps: The apps registry.
        schema_editor: The schema editor.
    """
    Post = apps.get_model("djpress", "Post")

    for post in Post.admin_objects.all():
        post._date = post.published_at.date()
        post.save(update_fields=["_date"])


def clear__date(apps, schema_editor) -> None:
    """Clear the _date field for all posts.

    This function is used to clear the _date field for all posts
    in the database.

    Args:
        apps: The apps registry.
        schema_editor: The schema editor.
    """
    Post = apps.get_model("djpress", "Post")

    for post in Post.admin_objects.all():
        post._date = None
        post.save(update_fields=["_date"])


class Migration(migrations.Migration):
    dependencies = [
        ("djpress", "0016_post__date"),
    ]

    operations = [
        migrations.RunPython(populate__date, clear__date),
    ]
