62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from django.contrib import messages
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
from django.urls import reverse_lazy
|
|
from django.views.generic import ListView, UpdateView
|
|
|
|
from .forms import SearchProfileForm
|
|
from .models import SearchProfile
|
|
from .services import resolve_profile_home_location, save_profile_revision
|
|
|
|
|
|
class ProfileListView(LoginRequiredMixin, ListView):
|
|
model = SearchProfile
|
|
template_name = "profiles/list.html"
|
|
context_object_name = "profiles"
|
|
|
|
def get_queryset(self):
|
|
return SearchProfile.objects.filter(user=self.request.user)
|
|
|
|
|
|
class ProfileUpdateView(LoginRequiredMixin, UpdateView):
|
|
model = SearchProfile
|
|
form_class = SearchProfileForm
|
|
template_name = "profiles/edit.html"
|
|
success_url = reverse_lazy("profiles:list")
|
|
|
|
def get_queryset(self):
|
|
return SearchProfile.objects.filter(user=self.request.user)
|
|
|
|
def form_valid(self, form):
|
|
response = super().form_valid(form)
|
|
location_result = resolve_profile_home_location(self.object)
|
|
if self.object.is_active:
|
|
self.object.activate()
|
|
save_profile_revision(self.object, reason="user_edit")
|
|
if self.object.home_postal_code and location_result.ambiguous:
|
|
messages.warning(
|
|
self.request,
|
|
"Zoekprofiel opgeslagen. Deze postcode omvat meerdere gemeenten; afstand blijft "
|
|
"onbekend tot de lokale geodata eenduidig is.",
|
|
)
|
|
elif self.object.home_postal_code and location_result.location is None:
|
|
messages.warning(
|
|
self.request,
|
|
"Zoekprofiel opgeslagen. De postcode is bewaard; lokale geodata ontbreekt nog "
|
|
"voor een afstandsberekening.",
|
|
)
|
|
else:
|
|
messages.success(self.request, "Zoekprofiel opgeslagen.")
|
|
return response
|
|
|
|
|
|
def activate_profile(request, pk: int):
|
|
if request.method != "POST":
|
|
return redirect("profiles:list")
|
|
profile = get_object_or_404(SearchProfile, pk=pk, user=request.user)
|
|
profile.activate()
|
|
messages.success(request, f"{profile.name} is nu het actieve profiel.")
|
|
return redirect("profiles:list")
|