From 22747c120615d8a81e929f885ae24df752d4c40c Mon Sep 17 00:00:00 2001 From: Patrick Mayr Date: Fri, 17 Jun 2022 09:35:59 +0000 Subject: [PATCH] simplify Posts Search Form --- fet2020/posts/forms.py | 4 +++ fet2020/posts/views.py | 45 ++++++------------------------ fet2020/templates/posts/index.html | 13 +++++---- 3 files changed, 19 insertions(+), 43 deletions(-) diff --git a/fet2020/posts/forms.py b/fet2020/posts/forms.py index 34727578..dc5b0099 100644 --- a/fet2020/posts/forms.py +++ b/fet2020/posts/forms.py @@ -2,6 +2,7 @@ from ckeditor_uploader.widgets import CKEditorUploadingWidget from taggit.models import Tag from django import forms +from django.forms.widgets import CheckboxInput from django.utils import timezone from django.utils.dates import MONTHS from django.utils.translation import gettext_lazy as _ @@ -170,6 +171,9 @@ class PostSearchForm(forms.Form): year = forms.ChoiceField(label="Jahr", choices=year_choices, required=False) month = forms.ChoiceField(label="Monat", choices=month_choices, required=False) + compact_view = forms.BooleanField(label="Kompakte Ansicht", required=False, widget=CheckboxInput) + fet_meeting_only = forms.BooleanField(label="nur FET Sitzungen", required=False, widget=CheckboxInput) + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # to get the self.fields set diff --git a/fet2020/posts/views.py b/fet2020/posts/views.py index 27549498..53dc3d24 100644 --- a/fet2020/posts/views.py +++ b/fet2020/posts/views.py @@ -22,44 +22,17 @@ logger = logging.getLogger(__name__) def index(request): posts = None - - compact_view = None - fet_meeting_only = None - public_only = not request.user.is_authenticated if request.method == "POST": - if "btn_input" in request.POST: - form = PostSearchForm(request.POST) - - if "compact_view" in request.POST.getlist("checkbox"): - compact_view = True - - if "fet_meeting_only" in request.POST.getlist("checkbox"): - fet_meeting_only = True - - if form.is_valid(): - month = form.cleaned_data["month"] - year = form.cleaned_data["year"] - - if month == "": - month = None - - if year == "": - year = None - month = None - - if not year and month: - messages.info( - request, "Es kann nicht nur nach einem Monat gesucht werden." - ) - - posts = Post.objects.date_filtered_list( - public_only, - year, - month, - fet_meeting_only, - ) + form = PostSearchForm(request.POST) + if form.is_valid(): + posts = Post.objects.date_filtered_list( + public_only, + form.cleaned_data["year"], + form.cleaned_data["month"], + form.cleaned_data["fet_meeting_only"], + ) else: last_year = Post.objects.get_queryset().first() if last_year: @@ -83,8 +56,6 @@ def index(request): context = { "formset": form, - "compact_view": compact_view, - "fet_meeting_only": fet_meeting_only, "posts": posts, } diff --git a/fet2020/templates/posts/index.html b/fet2020/templates/posts/index.html index 12f39a23..fccc1d7d 100644 --- a/fet2020/templates/posts/index.html +++ b/fet2020/templates/posts/index.html @@ -45,7 +45,7 @@

Fehler:

-
{{message}}
+
{{ message }}
{% endfor %} @@ -89,13 +89,14 @@ + @@ -116,7 +117,7 @@
- {% if compact_view %} + {% if formset.compact_view.value %} {% for post in posts %} {% include 'posts/partials/_posts_hero_compact.html' %} {% endfor %}