Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions address/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from rest_framework.exceptions import ParseError
from rest_framework.viewsets import ReadOnlyModelViewSet

from address.models import Street

from ..models import Address, Municipality, PostalCodeArea
from .serializers import (
AddressSerializer,
Expand Down Expand Up @@ -149,7 +151,12 @@
)
)
class AddressViewSet(ReadOnlyModelViewSet):
queryset = Address.objects.filter(pk__gte=0).order_by("pk")
queryset = Address.objects.order_by("pk").prefetch_related(
"street__translations",
"postal_code_area__translations",
"municipality__translations",
)

serializer_class = AddressSerializer

def get_queryset(self) -> QuerySet:
Expand All @@ -169,9 +176,12 @@ def _filter_by_street_name(self, addresses: QuerySet) -> QuerySet:
street_name = self.request.query_params.get("streetname")
if street_name is None:
return addresses
return addresses.filter(
street__translations__name__iexact=street_name
).distinct()
street_ids = list(
Street.objects.filter(translations__name__iexact=street_name).values_list(
"id", flat=True
)
)
return addresses.filter(street_id__in=street_ids)

def _filter_by_street_number(self, addresses: QuerySet) -> QuerySet:
street_number = self.request.query_params.get("streetnumber")
Expand All @@ -191,15 +201,23 @@ def _filter_by_municipality(self, addresses: QuerySet) -> QuerySet:
municipality = self.request.query_params.get("municipality")
if municipality is None:
return addresses
return addresses.filter(
municipality__translations__name__iexact=municipality
).distinct()
municipality_ids = list(
Municipality.objects.filter(
translations__name__iexact=municipality
).values_list("id", flat=True)
)
return addresses.filter(municipality_id__in=municipality_ids)

def _filter_by_municipality_code(self, addresses: QuerySet) -> QuerySet:
municipality_code = self.request.query_params.get("municipalitycode")
if municipality_code is None:
return addresses
return addresses.filter(municipality__code__iexact=municipality_code).distinct()
municipality_ids = list(
Municipality.objects.filter(code=municipality_code).values_list(
"id", flat=True
)
)
return addresses.filter(municipality_id__in=municipality_ids)

def _filter_by_postal_code(self, addresses: QuerySet) -> QuerySet:
postal_code = self.request.query_params.get("postalcode")
Expand Down Expand Up @@ -264,7 +282,7 @@ def _filter_by_location(self, addresses: QuerySet) -> QuerySet:
retrieve=extend_schema(parameters=_area_parameters),
)
class PostalCodeAreaViewSet(ReadOnlyModelViewSet):
queryset = PostalCodeArea.objects.filter(pk__gte=0).order_by("pk")
queryset = PostalCodeArea.objects.order_by("pk").prefetch_related("translations")
serializer_class = PostalCodeAreaSerializer

def get_queryset(self) -> QuerySet:
Expand Down Expand Up @@ -307,5 +325,5 @@ def _filter_by_post_office(self, areas: QuerySet) -> QuerySet:
retrieve=extend_schema(parameters=_area_parameters),
)
class MunicipalityViewSet(ReadOnlyModelViewSet):
queryset = Municipality.objects.filter(pk__gte=0).order_by("pk")
queryset = Municipality.objects.order_by("pk").prefetch_related("translations")
serializer_class = MunicipalitySerializer
28 changes: 28 additions & 0 deletions address/migrations/0008_address_idx_address_municipality_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 6.0 on 2026-01-23 15:38

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("address", "0007_postalcodearea_municipality"),
]

operations = [
migrations.AddIndex(
model_name="address",
index=models.Index(
fields=["municipality", "id"],
include=(
"street",
"number",
"number_end",
"letter",
"postal_code_area",
"location",
"modified_at",
),
name="idx_address_municipality_id",
),
),
]
17 changes: 17 additions & 0 deletions address/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ def __str__(self) -> str:

class Meta:
verbose_name_plural = _("Addresses")
indexes = [
# This index speeds up address?municipality and address?municipalitycode
# by 10x
models.Index(
name="idx_address_municipality_id",
fields=["municipality", "id"],
include=[
"street",
"number",
"number_end",
"letter",
"postal_code_area",
"location",
"modified_at",
],
)
]
8 changes: 8 additions & 0 deletions geo_search/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

env = Env(
DEBUG=(bool, False),
DEBUG_TOOLBAR=(bool, False),
SECRET_KEY=(str, "temp_key"),
ALLOWED_HOSTS=(list, []),
STATIC_ROOT=(str, str(BASE_DIR / "static")),
Expand All @@ -47,6 +48,8 @@
Env.read_env(env_path)

DEBUG = env.bool("DEBUG")
DEBUG_TOOLBAR = env.bool("DEBUG_TOOLBAR")

SECRET_KEY = env.str("SECRET_KEY")
if DEBUG and not SECRET_KEY:
SECRET_KEY = "secret-for-debugging-only"
Expand Down Expand Up @@ -112,6 +115,11 @@ def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
"whitenoise.middleware.WhiteNoiseMiddleware",
]

if DEBUG and DEBUG_TOOLBAR:
INSTALLED_APPS.append("debug_toolbar")
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
INTERNAL_IPS = ["127.0.0.1"]

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
Expand Down
6 changes: 6 additions & 0 deletions geo_search/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse
from django.urls import include, path
Expand Down Expand Up @@ -45,3 +46,8 @@ def readiness(*args, **kwargs) -> HttpResponse:


urlpatterns += [path("healthz", healthz), path("readiness", readiness)]

if settings.DEBUG and settings.DEBUG_TOOLBAR:
from debug_toolbar.toolbar import debug_toolbar_urls

urlpatterns += debug_toolbar_urls()
Loading