-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1139 from CodingPirates/person-municipality-forei…
…gn-key [#1113] Ændre kommune felt for Person til relation
- Loading branch information
Showing
12 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from django.core.management.base import BaseCommand | ||
import requests | ||
from members.models import Municipality, Person | ||
|
||
# run locally: | ||
# docker compose run web ./manage.py set_person_municipality | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Set the municipality for all persons based on their address" | ||
|
||
def handle(self, *args, **options): | ||
# Iterate over all items in Person model | ||
for person in Person.objects.all(): | ||
# First reset municipality before migrating data | ||
person.municipality = None | ||
person.save() | ||
|
||
# Call the API to get municipality id | ||
url = f"https://api.dataforsyningen.dk/adresser?id={person.dawa_id}" | ||
response = requests.get(url) | ||
|
||
if response.status_code == 200: | ||
data = response.json() | ||
|
||
if not data: | ||
self.stdout.write( | ||
self.style.ERROR( | ||
f"No DAWA data found for person: {person.name} - {url}" | ||
) | ||
) | ||
continue | ||
|
||
municipality_id = data[0]["adgangsadresse"]["kommune"]["kode"] | ||
|
||
if municipality_id: | ||
try: | ||
# Look up the found municipality id from Municipality model | ||
municipality = Municipality.objects.get(dawa_id=municipality_id) | ||
# Set the municipality for the person | ||
person.municipality = municipality | ||
person.save() | ||
self.stdout.write( | ||
f"Set municipality for person: {person.name} to {municipality.name}" | ||
) | ||
except Municipality.DoesNotExist: | ||
self.stdout.write( | ||
self.style.ERROR( | ||
f"Municipality with dawa_id {municipality_id} does not exist." | ||
) | ||
) | ||
else: | ||
self.stdout.write( | ||
self.style.ERROR( | ||
f"Failed to fetch data for person: {person.name} - {url}" | ||
) | ||
) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS("Successfully updated municipality for all persons") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.16 on 2024-11-11 21:18 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
( | ||
"members", | ||
"0058_alter_municipality_options_remove_municipality_email_and_more", | ||
), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="person", | ||
name="municipality", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.2.16 on 2024-11-11 21:35 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("members", "0059_remove_person_municipality"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="person", | ||
name="municipality", | ||
field=models.ForeignKey( | ||
blank=True, | ||
default="", | ||
null=True, | ||
on_delete=django.db.models.deletion.RESTRICT, | ||
to="members.municipality", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by Django 4.2.16 on 2024-12-29 09:52 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("members", "0060_alter_union_options"), | ||
("members", "0060_person_municipality"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by Django 4.2.16 on 2024-12-29 14:29 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("members", "0061_merge_20241229_1052"), | ||
("members", "0062_remove_activity_city_remove_activity_dawa_id_and_more"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from factory import Faker | ||
from factory.django import DjangoModelFactory | ||
from members.models.municipality import Municipality | ||
|
||
|
||
class MunicipalityFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Municipality | ||
|
||
name = Faker("name") | ||
address = Faker("address") | ||
zipcode = Faker("zipcode") | ||
city = Faker("city") | ||
dawa_id = Faker("uuid4") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters