Merge branch 'odevzdavatko_old_but_fixed' into data_migrations
This commit is contained in:
		
						commit
						5233d1e7ee
					
				
					 2 changed files with 56 additions and 16 deletions
				
			
		|  | @ -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 | ||||
| 
 | ||||
| 	# 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) | ||||
| 	@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") | ||||
| 				] | ||||
| 
 | ||||
| 	# 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]) | ||||
| 	@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) | ||||
| 	problemy = forms.ChoiceField(choices=PROBLEMY_CHOICES) | ||||
| 	 | ||||
| 	reseni_od = forms.DateField(input_formats=[DATE_FORMAT]) | ||||
| 	reseni_do = forms.DateField(input_formats=[DATE_FORMAT]) | ||||
|  |  | |||
|  | @ -57,17 +57,19 @@ class TabulkaOdevzdanychReseniView(ListView): | |||
| 			reseni_od = fcd["reseni_od"] | ||||
| 			reseni_do = fcd["reseni_do"] | ||||
| 		else: | ||||
| 			resitele = FiltrForm.get_initial_for_field(FormFiltr.resitele, "resitele") | ||||
| 			problemy = FiltrForm.get_initial_for_field(FormFiltr.problemy, "problemy") | ||||
| 			resitele_od = FiltrForm.get_initial_for_field(FormFiltr.resitele_od, "resitele_od") | ||||
| 			resitele_do = FiltrForm.get_initial_for_field(FormFiltr.resitele_do, "resitele_do") | ||||
| 			initial = FiltrForm.gen_initial() | ||||
| 			resitele = initial['resitele'] | ||||
| 			problemy = initial['problemy'] | ||||
| 			reseni_od = initial['reseni_od'][0] | ||||
| 			reseni_do = initial['reseni_do'][0] | ||||
| 			 | ||||
| 
 | ||||
| 		# Filtrujeme! | ||||
| 		aktualni_rocnik = m.Nastaveni.get_solo().aktualni_rocnik	# .get_solo() vrátí tu jedinou instanci | ||||
| 		self.chteni_resitele = resitele	# Zapamatování pro get_context_data | ||||
| 		if resitele == FiltrForm.RESITELE_RELEVANTNI: | ||||
| 			logger.warning("Někdo chtěl v tabulce jen relevantní řešitele a měl smůlu :-(") | ||||
| 			resitele = FiltrForm.RESITELE_LETOSNI # Fall-through | ||||
| 			# TODO: Zkontrolovat, že resi_v_rocniku vrací QuerySet (jinak asi bude žrát spoustu zdrojů zbytečně) | ||||
| 			self.resitele = resi_v_rocniku(aktualni_rocnik)	# Prvotní sada, pokud nebude mít body, odstraní se v get_context_data | ||||
| 		elif resitele == FiltrForm.RESITELE_LETOSNI: | ||||
| 			self.resitele = resi_v_rocniku(aktualni_rocnik) | ||||
| 
 | ||||
|  | @ -118,16 +120,20 @@ class TabulkaOdevzdanychReseniView(ListView): | |||
| 				pridej_reseni(hodnoceni.problem, resitel, hodnoceni.body, hodnoceni.reseni.cas_doruceni) | ||||
| 
 | ||||
| 		hodnoty = [] | ||||
| 		resitele_do_tabulky = [] | ||||
| 		for resitel in self.resitele: | ||||
| 			dostal_body = False | ||||
| 			resiteluv_radek = [] | ||||
| 			for problem in self.problemy: | ||||
| 				if problem in tabulka and resitel in tabulka[problem]: | ||||
| 					resiteluv_radek.append(tabulka[problem][resitel]) | ||||
| 					dostal_body = True | ||||
| 				else: | ||||
| 					resiteluv_radek.append(None) | ||||
| 			hodnoty.append(resiteluv_radek) | ||||
| 		ctx['radky'] = list(zip(self.resitele, hodnoty)) | ||||
| 
 | ||||
| 			if self.chteni_resitele != FiltrForm.RESITELE_RELEVANTNI or dostal_body: | ||||
| 				hodnoty.append(resiteluv_radek) | ||||
| 				resitele_do_tabulky.append(resitel) | ||||
| 		ctx['radky'] = list(zip(resitele_do_tabulky, hodnoty)) | ||||
| 		ctx['filtr'] = FiltrForm(initial=self.request.GET) | ||||
| 		# Pro použití hacku na automatické {{form.media}} v template: | ||||
| 		ctx['form'] = ctx['filtr'] | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Pavel "LEdoian" Turinsky
						Pavel "LEdoian" Turinsky