# Generated by Django 3.2.2 on 2021-05-27 08:41
import re
from urllib.parse import urlparse

from django.conf import settings
from django.db import migrations


def update_file_urls(apps, schema_editor):
    AttributeValue = apps.get_model("attribute", "AttributeValue")
    values = AttributeValue.objects.filter(file_url__isnull=False)
    for value in values.iterator():
        value.file_url = re.sub(
            f"^{settings.MEDIA_URL}", "", urlparse(value.file_url).path
        )
        value.save(update_fields=["file_url"])


class Migration(migrations.Migration):

    dependencies = [
        ("attribute", "0010_auto_20210412_0736"),
    ]

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