diff --git a/seminar/forms.py b/seminar/forms.py index f26d11fa..45d091e8 100644 --- a/seminar/forms.py +++ b/seminar/forms.py @@ -347,7 +347,8 @@ class OdevzdavatkoTabulkaFiltrForm(forms.Form): # TODO: Typy problémů (problémy, úlohy, ostatní, všechny)? Jen některá řešení (obodovaná/neobodovaná, víc řešitelů, ...)? - def gen_terminy(): + @classmethod + def gen_terminy(cls): import datetime from time import strftime @@ -390,11 +391,44 @@ 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], + 'reseni_do': terminy[-1], + } + return initial + + def __init__(self, *args, **kwargs): + if 'initial' not in kwargs: + 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, initial=RESITELE_RELEVANTNI) - problemy = forms.ChoiceField(choices=PROBLEMY_CHOICES, initial=PROBLEMY_MOJE) + resitele = forms.ChoiceField(choices=RESITELE_CHOICES) + problemy = forms.ChoiceField(choices=PROBLEMY_CHOICES) - # choices jako parametr Select widgetu neumí brát callable, jen iterable, takže si pro jednoduchost můžu rovnou uložit výsledek sem... - terminy = gen_terminy() - reseni_od = forms.DateField(input_formats=[DATE_FORMAT], widget=forms.Select(choices=terminy), initial=terminy[-2]) - reseni_do = forms.DateField(input_formats=[DATE_FORMAT], widget=forms.Select(choices=terminy), initial=terminy[-1]) + reseni_od = forms.DateField(input_formats=[DATE_FORMAT]) + reseni_do = forms.DateField(input_formats=[DATE_FORMAT]) diff --git a/seminar/templates/seminar/orgorozcestnik.html b/seminar/templates/seminar/orgorozcestnik.html index 4e5fbc78..0a70899a 100644 --- a/seminar/templates/seminar/orgorozcestnik.html +++ b/seminar/templates/seminar/orgorozcestnik.html @@ -5,7 +5,7 @@

Informace, komunikace