refactor: Use django form

This commit is contained in:
moanos [he/him] 2024-05-31 13:48:24 +02:00
parent 961c2230a0
commit 8d92539b22
3 changed files with 17 additions and 16 deletions

View File

@ -126,3 +126,12 @@ class ModerationActionForm(forms.ModelForm):
class CustomRegistrationForm(RegistrationForm): class CustomRegistrationForm(RegistrationForm):
class Meta(RegistrationForm.Meta): class Meta(RegistrationForm.Meta):
model = User model = User
def _get_distances():
return {i: i for i in [10, 20, 50, 100, 200, 500]}
class AdoptionNoticeSearchForm(forms.Form):
postcode = forms.CharField(max_length=20, label=_("Postleitzahl"))
max_distance = forms.ChoiceField(choices=_get_distances, label=_("Max. Distanz"))

View File

@ -6,17 +6,7 @@
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="longitude" maxlength="200" id="longitude"> <input type="hidden" name="longitude" maxlength="200" id="longitude">
<input type="hidden" name="latitude" maxlength="200" id="latitude"> <input type="hidden" name="latitude" maxlength="200" id="latitude">
<label for="postcode">{% translate "Postleitzahl" %} {{ search_form.as_p }}
</label>
<input type="text" name="postcode" maxlength="200" class="textinput form-control" required="" id="postcode">
<select name="max_distance" class="select custom-select" required="" id="id_max_distance">
<option value="" selected="">---------</option>
<option value="25">20km</option>
<option value="50">50km</option>
<option value="100">100km</option>
<option value="200">200km</option>
<option value="500">500km</option>
</select>
<input type="submit" value="Search" name="search"> <input type="submit" value="Search" name="search">
</form> </form>
{% include "fellchensammlung/lists/list-adoption-notices.html" %} {% include "fellchensammlung/lists/list-adoption-notices.html" %}

View File

@ -13,7 +13,8 @@ from notfellchen import settings
from fellchensammlung import logger from fellchensammlung import logger
from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \
Member, Location Member, Location
from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, CommentForm, ReportCommentForm, AnimalForm from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, CommentForm, ReportCommentForm, AnimalForm, \
AdoptionNoticeSearchForm
from .models import Language, Announcement from .models import Language, Announcement
from .tools.geo import GeoAPI from .tools.geo import GeoAPI
@ -89,6 +90,7 @@ def animal_detail(request, animal_id):
def search(request): def search(request):
if request.method == 'POST': if request.method == 'POST':
search_form = AdoptionNoticeSearchForm(request.POST)
max_distance = int(request.POST.get('max_distance')) max_distance = int(request.POST.get('max_distance'))
if max_distance == "": if max_distance == "":
max_distance = None max_distance = None
@ -97,12 +99,12 @@ def search(request):
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")
adoption_notices_in_distance = [a for a in latest_adoption_list if a.in_distance(search_position, max_distance)] adoption_notices_in_distance = [a for a in latest_adoption_list if a.in_distance(search_position, max_distance)]
context = {"adoption_notices": adoption_notices_in_distance} context = {"adoption_notices": adoption_notices_in_distance, "search_form": search_form}
return render(request, 'fellchensammlung/search.html', context=context)
else: else:
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")
context = {"adoption_notices": latest_adoption_list} search_form = AdoptionNoticeSearchForm()
return render(request, 'fellchensammlung/search.html', context=context) context = {"adoption_notices": latest_adoption_list, "search_form": search_form}
return render(request, 'fellchensammlung/search.html', context=context)
@login_required @login_required