From 9ea00655d4d2779e27caabe5ad13a79fc459aef8 Mon Sep 17 00:00:00 2001 From: moanos Date: Tue, 24 Dec 2024 10:02:08 +0100 Subject: [PATCH] refactor: Use integer choice for search --- src/fellchensammlung/forms.py | 8 ++------ src/fellchensammlung/models.py | 7 +++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index 3f640bf..78e956d 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -1,7 +1,7 @@ from django import forms from .models import AdoptionNotice, Animal, Image, ReportAdoptionNotice, ReportComment, ModerationAction, User, Species, \ - Comment, SexChoicesWithAll + Comment, SexChoicesWithAll, DistanceChoices from django_registration.forms import RegistrationForm from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field, Hidden @@ -181,12 +181,8 @@ class CustomRegistrationForm(RegistrationForm): self.helper.add_input(Submit('submit', _('Registrieren'), css_class="btn")) -def _get_distances(): - return {i: i for i in [20, 50, 100, 200, 500]} - - class AdoptionNoticeSearchForm(forms.Form): location = forms.CharField(max_length=20, label=_("Stadt"), required=False) - max_distance = forms.ChoiceField(choices=_get_distances, label=_("Max. Distanz")) + max_distance = forms.ChoiceField(choices=DistanceChoices, initial=DistanceChoices.ONE_HUNDRED, label=_("Suchradius")) sex = forms.ChoiceField(choices=SexChoicesWithAll, label=_("Geschlecht"), required=False, initial=SexChoicesWithAll.ALL) diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 9d9d14b..d44ca3a 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -539,6 +539,13 @@ class Animal(models.Model): """Returns the url to access a detailed page for the animal.""" return reverse('animal-detail', args=[str(self.id)]) +class DistanceChoices(models.IntegerChoices): + TWENTY = 20, '20 km' + FIFTY = 50, '50 km' + ONE_HUNDRED = 100, '100 km' + TWO_HUNDRED = 200, '200 km' + FIVE_HUNDRED = 500, '500 km' + class Rule(models.Model): """