Refaktorizace ArchivTemataView
Já jsem si říkal, že to musí jít jednodušeji.
This commit is contained in:
		
							parent
							
								
									730a7dfe5a
								
							
						
					
					
						commit
						cc17d5680b
					
				
					 1 changed files with 3 additions and 17 deletions
				
			
		|  | @ -778,27 +778,13 @@ class CisloView(generic.DetailView): | |||
| class ArchivTemataView(generic.ListView): | ||||
| 	model = Problem | ||||
| 	template_name = 'seminar/archiv/temata.html' | ||||
| 	queryset = Tema.objects.filter(stav=Problem.STAV_ZADANY).select_related('rocnik').order_by('-rocnik', 'kod') | ||||
| 	queryset = Tema.objects.filter(stav=Problem.STAV_ZADANY).select_related('rocnik').order_by('rocnik', 'kod') | ||||
| 
 | ||||
| 	def get_context_data(self, *args, **kwargs): | ||||
| 		ctx = super().get_context_data(*args, **kwargs) | ||||
| 		# Najdeme, jaké ročníky mají témata | ||||
| 		# TODO: Tohle by se rovnou mohlo tahat z databáze, ne? | ||||
| 		#       Navíc už asi nepotřebuji ten select_related, že? | ||||
| 		rocniky_set = set() | ||||
| 		for tema in ctx['object_list']: | ||||
| 			rocniky_set.add(tema.rocnik) | ||||
| 		rocniky = list(rocniky_set) | ||||
| 		rocniky.sort(key=lambda r: int(r.rocnik), reverse=True) | ||||
| 
 | ||||
| 		# Přiřadíme témátka k ročníkům | ||||
| 		# TODO: Asi taky jde udělat nějak elegantněji, ne? | ||||
| 		ctx['rocniky'] = OrderedDict() | ||||
| 		for rocnik in rocniky: | ||||
| 			ctx['rocniky'][rocnik] = [] | ||||
| 			for tema in ctx['object_list']: | ||||
| 				if tema.rocnik == rocnik: | ||||
| 					ctx['rocniky'][rocnik].append(tema) | ||||
| 		for rocnik, temata in groupby(ctx['object_list'], lambda tema: tema.rocnik): | ||||
| 			ctx['rocniky'][rocnik] = list(temata) | ||||
| 		return ctx | ||||
| 
 | ||||
| ### Generovani vysledkovky | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Pavel 'LEdoian' Turinsky
						Pavel 'LEdoian' Turinsky