Skip to content

Commit

Permalink
Merge pull request #1139 from CodingPirates/person-municipality-forei…
Browse files Browse the repository at this point in the history
…gn-key

[#1113] Ændre kommune felt for Person til relation
  • Loading branch information
rasmusselsmark authored Dec 29, 2024
2 parents e786852 + f1e015d commit 8592a32
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/DEPLOYING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ You can develop and test locally as described in the [CONTRIBUTING guide](CONTRI
1. In the screenshot above, PR `#699` has already been deployed. Click the `Open app` button to open the running site, in this case <https://medlemssystem-pr-699.herokuapp.com/> (will be a unique url for each pull request)
1. In order to get data populated, we need to populate the database, so open `More -> Run console`
![Heroku pull request](images/heroku-pull-request.png)
1. Run `./manage.py import_municipalities members/management/commands/municipalities.csv` to import municipalities
1. Run `./manage.py get_live_data` to get the public live data populated
1. Run `./manage.py shell` to create members in the system. When the shell prompt is ready, type in

Expand Down
1 change: 1 addition & 0 deletions members/admin/municipality_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

class MunicipalityAdmin(admin.ModelAdmin):
list_display = ("name", "address", "zipcode", "city", "dawa_id")
search_fields = ("name", "address", "zipcode", "city")
5 changes: 5 additions & 0 deletions members/admin/person_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PersonParticipantListFilter,
PersonWaitinglistListFilter,
VolunteerListFilter,
MunicipalityFilter,
)

from .inlines import (
Expand Down Expand Up @@ -45,6 +46,7 @@ class PersonAdmin(admin.ModelAdmin):
"membertype",
"gender",
VolunteerListFilter,
MunicipalityFilter,
PersonWaitinglistListFilter,
PersonInvitedListFilter,
PersonParticipantListFilter,
Expand All @@ -53,6 +55,7 @@ class PersonAdmin(admin.ModelAdmin):
PersonParticipantLastYearListFilter,
)
search_fields = ("name", "family__email", "notes")
autocomplete_fields = ["municipality"]
actions = [
AdminActions.invite_many_to_activity_action,
"export_emaillist",
Expand Down Expand Up @@ -95,6 +98,7 @@ def get_fieldsets(self, request, person=None):
"city",
"zipcode",
"placename",
"municipality",
"email",
"phone",
"family",
Expand Down Expand Up @@ -137,6 +141,7 @@ def get_readonly_fields(self, request, obj=None):
"city",
"zipcode",
"placename",
"municipality",
"email",
"phone",
"family",
Expand Down
21 changes: 21 additions & 0 deletions members/admin/person_admin_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.utils import timezone

from members.models import Activity, AdminUserInformation
from members.models.municipality import Municipality


class PersonParticipantCurrentYearListFilter(admin.SimpleListFilter):
Expand Down Expand Up @@ -185,3 +186,23 @@ def queryset(self, request, queryset):
return queryset.filter(
volunteer__department__pk=self.value(), volunteer__removed__isnull=True
)


class MunicipalityFilter(admin.SimpleListFilter):
title = "Kommune"
parameter_name = "municipality"

def lookups(self, request, model_admin):
municipalities = [("none", "Ingen kommune")]
for municipality in Municipality.objects.all().order_by("name"):
municipalities.append((str(municipality.pk), municipality.name))

return municipalities

def queryset(self, request, queryset):
if self.value() == "none":
return queryset.filter(municipality__isnull=True).distinct()
elif self.value() is None:
return queryset
else:
return queryset.filter(municipality_id=self.value())
61 changes: 61 additions & 0 deletions members/management/commands/set_person_municipality.py
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")
)
19 changes: 19 additions & 0 deletions members/migrations/0059_remove_person_municipality.py
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",
),
]
25 changes: 25 additions & 0 deletions members/migrations/0060_person_municipality.py
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",
),
),
]
13 changes: 13 additions & 0 deletions members/migrations/0061_merge_20241229_1052.py
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 = []
13 changes: 13 additions & 0 deletions members/migrations/0063_merge_20241229_1529.py
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 = []
9 changes: 8 additions & 1 deletion members/models/person.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.utils import timezone
from django.conf import settings
from members.models.municipality import Municipality
from members.utils.address import format_address
from urllib.parse import quote_plus
import requests
Expand Down Expand Up @@ -60,7 +61,13 @@ class Meta:
floor = models.CharField("Etage", max_length=10, blank=True)
door = models.CharField("Dør", max_length=5, blank=True)
dawa_id = models.CharField("DAWA id", max_length=200, blank=True)
municipality = models.CharField("Kommune", max_length=100, blank=True, null=True)
municipality = municipality = models.ForeignKey(
Municipality,
on_delete=models.RESTRICT,
blank=True,
null=True, # allow blank/null values since we don't have addresses for all persons
default="",
)
longitude = models.DecimalField(
"Længdegrad", blank=True, null=True, max_digits=9, decimal_places=6
)
Expand Down
14 changes: 14 additions & 0 deletions members/tests/factories/municipality_factory.py
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")
3 changes: 2 additions & 1 deletion members/tests/factories/person_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from factory import Faker, SubFactory, SelfAttribute
from factory.django import DjangoModelFactory
from members.tests.factories.factory_helpers import TIMEZONE, LOCALE
from members.tests.factories.municipality_factory import MunicipalityFactory
from members.tests.factories.providers import DanishProvider
from factory.fuzzy import FuzzyChoice
from members.models import Person
Expand All @@ -26,7 +27,7 @@ class Meta:
floor = Faker("floor")
door = Faker("door")
dawa_id = Faker("uuid4")
municipality = Faker("municipality")
municipality = SubFactory(MunicipalityFactory)
longitude = Faker("longitude")
latitude = Faker("latitude")
updated_dtm = Faker("date_time", tzinfo=TIMEZONE)
Expand Down

0 comments on commit 8592a32

Please sign in to comment.