From 39dc2d8e3251c9818dee47b19886d04a334e1f8b Mon Sep 17 00:00:00 2001
From: Pavel 'LEdoian' Turinsky <ledoian@matfyz.cz>
Date: Wed, 6 May 2020 23:07:04 +0200
Subject: [PATCH] =?UTF-8?q?Implementovan=C3=BD=20ArchivTemataView?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 seminar/templates/seminar/archiv/temata.html | 18 ++++++---------
 seminar/views/views_all.py                   | 24 +++++++++++++++++++-
 2 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/seminar/templates/seminar/archiv/temata.html b/seminar/templates/seminar/archiv/temata.html
index ba2a6206..fc7d87e8 100644
--- a/seminar/templates/seminar/archiv/temata.html
+++ b/seminar/templates/seminar/archiv/temata.html
@@ -7,17 +7,13 @@
     {% endblock %}{% endblock%}
   </h1>
 
-  {% for tema in object_list %}
-  {% with tema.cislo_zadani.rocnik.rocnik as rocnik %}
-    {% ifchanged rocnik %}
-      {% if not forloop.first %}</ul>{% endif %}
-      <h2>{{ rocnik }}. ročník</h2>
-      <ul>
-    {% endifchanged %}
-        <li>
-          <a href="{{ tema.verejne_url }}">{{ tema.kod_v_rocniku }}: {{ tema.nazev }}</a>
-  {% endwith %}
+  {% for rocnik, temata in rocniky.items %}
+    <h2>Ročník {{ rocnik }}</h2>
+    <ul>
+    {% for tema in temata %}
+    <li><a href="{{ tema.verejne_utl }}"> {{ tema.kod_v_rocniku }}: {{ tema.nazev }} </a></li>
+    {% endfor %}
+    </ul>
   {% endfor %}
-  </ul>
 
 {% endblock content %}
diff --git a/seminar/views/views_all.py b/seminar/views/views_all.py
index 95eb07d1..79de03e1 100644
--- a/seminar/views/views_all.py
+++ b/seminar/views/views_all.py
@@ -27,6 +27,7 @@ import seminar.forms as f
 from datetime import timedelta, date, datetime
 from django.utils import timezone
 from itertools import groupby
+from collections import OrderedDict
 import tempfile
 import subprocess
 import shutil
@@ -777,7 +778,28 @@ 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('cislo_zadani__rocnik').order_by('-cislo_zadani__rocnik__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)
+		return ctx
 
 ### Generovani vysledkovky