From cf175a50b2ff8a7a6e0f8ee0ebaa7fb115af7e4d Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Tue, 16 Mar 2021 21:37:58 +0100 Subject: [PATCH] =?UTF-8?q?Odevzd=C3=A1v=C3=A1tko:=20skoro=20spr=C3=A1vn?= =?UTF-8?q?=C4=9B=20formul=C3=A1=C5=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/forms.py | 22 +++++++++++++++++----- seminar/views/odevzdavatko.py | 4 ++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/seminar/forms.py b/seminar/forms.py index 5fbd54a3..7018c1e9 100644 --- a/seminar/forms.py +++ b/seminar/forms.py @@ -383,14 +383,25 @@ class OdevzdavatkoTabulkaFiltrForm(forms.Form): return result + @classmethod + def gen_terminy_safe(cls): + "Při prvotních migracích / nasazeních webu neexistuje nastavení, takže to hodí výjimku, kterou musíme požrat..." + try: + return cls.gen_terminy() + except NotImplementedError: + return [ + ('1970-01-01', "Je to rozbitý"), + ('2012-12-12', "Svět skončil v roce 2012") + ] + @classmethod def gen_initial(cls): terminy = cls.gen_terminy() initial = { 'resitele': cls.RESITELE_RELEVANTNI, 'problemy': cls.PROBLEMY_MOJE, - 'reseni_od': terminy[-2][0], - 'reseni_do': terminy[-1][0], + 'reseni_od': terminy[-2], + 'reseni_do': terminy[-1], } return initial @@ -399,16 +410,17 @@ class OdevzdavatkoTabulkaFiltrForm(forms.Form): super().__init__(initial=self.gen_initial(), *args, **kwargs) else: super().__init__(*args, **kwargs) - # choices jako parametr Select widgetu neumí brát callable, jen iterable, takže si pro jednoduchost můžu rovnou uložit výsledek sem... # A "sem" znamená do libovolné metody, protože jinak se jedná o kód, který django spustí při inicializaci a protože potřebujeme databázi, tak by spadnul při vyrábění testdat... self.terminy = self.gen_terminy() + self.fields['reseni_od'].widget = forms.Select(choices=self.gen_terminy_safe()) + self.fields['reseni_od'].initial = self.terminy[-2] + self.fields['reseni_do'].widget = forms.Select(choices=self.gen_terminy_safe()) + self.fields['reseni_do'].initial = self.terminy[-1] # NOTE: Initial definuji pro jednotlivé fieldy, aby to bylo tady a nebylo potřeba to řešit ve views... resitele = forms.ChoiceField(choices=RESITELE_CHOICES) problemy = forms.ChoiceField(choices=PROBLEMY_CHOICES) -# reseni_od = forms.DateField(input_formats=[DATE_FORMAT], widget=forms.Select(choices=self.terminy)) -# reseni_do = forms.DateField(input_formats=[DATE_FORMAT], widget=forms.Select(choices=self.terminy)) reseni_od = forms.DateField(input_formats=[DATE_FORMAT]) reseni_do = forms.DateField(input_formats=[DATE_FORMAT]) diff --git a/seminar/views/odevzdavatko.py b/seminar/views/odevzdavatko.py index 5458a8ca..5c64bc11 100644 --- a/seminar/views/odevzdavatko.py +++ b/seminar/views/odevzdavatko.py @@ -60,8 +60,8 @@ class TabulkaOdevzdanychReseniView(ListView): initial = FiltrForm.gen_initial() resitele = initial['resitele'] problemy = initial['problemy'] - reseni_od = initial['reseni_od'] - reseni_do = initial['reseni_do'] + reseni_od = initial['reseni_od'][0] + reseni_do = initial['reseni_do'][0] # Filtrujeme!