agenda_culturel/src/scripts/create_reference_locations.py
2025-03-02 16:29:17 +01:00

29 lines
762 B
Python

import json
import os
from django.contrib.gis.geos import Point
from agenda_culturel.models import ReferenceLocation
def run():
input_file = os.path.dirname(__file__) + os.path.sep + "communes.json"
data = []
with open(input_file, "r") as file:
data = json.load(file)
# remove all locations
ReferenceLocation.objects.all().delete()
objs = [
ReferenceLocation(
location=Point(c["geo_point_2d"]["lon"], c["geo_point_2d"]["lat"]),
name=c["com_name"],
main=c["main"] if "main" in c else 0,
suggested_distance=c["suggested"] if "suggested" in c else None,
)
for c in data
]
objs = ReferenceLocation.objects.bulk_create(objs, ignore_conflicts=True)