try out django
[mlpccg-meta.git] / meta / views.py
diff --git a/meta/views.py b/meta/views.py
new file mode 100644 (file)
index 0000000..fc1a7af
--- /dev/null
@@ -0,0 +1,27 @@
+from django.shortcuts import render
+from meta.models import TournamentModel
+from mlpccg.CardDb import CARDDB
+from mlpccg.DeckList import DeckList
+from mlpccg.Clustering import Clustering
+
+def index(request):
+    tournament_models = TournamentModel.objects.all().order_by('-date')
+    tournament_data = []
+    placements = []
+    for tournament in tournament_models:
+        tournament_records = tournament.recordmodel_set.all().order_by('placement')
+        tournament_decks = []
+        for record in tournament_records:
+            decklist = DeckList(name=record.decklist.name, url=record.decklist.url)
+            tournament_decks += [decklist]
+            if len(decklist.cards) > 0:
+                placements += [{'decklist': decklist, 'placement': record.placement}]
+
+        tournament_data += [{'tournament': tournament, 'decklists': tournament_decks}]
+
+    clustering = Clustering(records=placements)
+    ranking = [(int(avg), label, [decklist for decklist in clustering.clusters[label]]) for avg, label in clustering.ranking()]
+
+    return render(request, 'index.html', {
+        'ranking': ranking,
+        'tournaments': tournament_data})