# Generated by Django 4.2.9 on 2024-10-10 20:35

from django.db import migrations
from django.contrib.gis.geos import Point

def change_coord_format(apps, schema_editor):
    Place = apps.get_model("agenda_culturel", "Place")
    places = Place.objects.values("location", "location_pt").all()

    for p in places:
        l = p.location.split(',')
        if len(l) == 2:
            p.location_pt = Point(float(l[1]), float(l[0]))
        else:
            p.location_pt = Point(3.08333, 45.783329)
        p.save(update_fields=["location_pt"])

def reverse_coord_format(apps, schema_editor):
    Place = apps.get_model("agenda_culturel", "Place")
    places = Place.objects.values("location", "location_pt").all()

    for p in places:
        p.location = ','.join([p.location_pt[1], p.location_pt[0]])
        p.save(update_fields=["location"])
    


class Migration(migrations.Migration):

    dependencies = [
        ('agenda_culturel', '0080_place_location_pt'),
    ]

    operations = [
        migrations.RunPython(change_coord_format, reverse_code=reverse_coord_format),
    ]


