# Generated by Django 4.2.19 on 2025-03-22 16:34

from django.db import migrations
from ua_parser import parse

from django_logbox.utils import browser_str, device_str, os_str


def _populate_device_os_browser_fields(apps, schema_editor):
    ServerLog = apps.get_model("django_logbox", "ServerLog")
    for serverlog in ServerLog.objects.all():
        parsed_useragent_result = parse(serverlog.user_agent)

        serverlog.device = device_str(parsed_useragent_result)
        serverlog.os = os_str(parsed_useragent_result)
        serverlog.browser = browser_str(parsed_useragent_result)

        serverlog.save()


class Migration(migrations.Migration):
    dependencies = [
        ("django_logbox", "0005_alter_serverlog_options_serverlog_browser_and_more"),
    ]

    operations = [
        migrations.RunPython(_populate_device_os_browser_fields),
    ]
