try out django
[mlpccg-meta.git] / meta / views.py
1 from django.shortcuts import render
2 from meta.models import TournamentModel
3 from mlpccg.CardDb import CARDDB
4 from mlpccg.DeckList import DeckList
5 from mlpccg.Clustering import Clustering
6
7 def index(request):
8 tournament_models = TournamentModel.objects.all().order_by('-date')
9 tournament_data = []
10 placements = []
11 for tournament in tournament_models:
12 tournament_records = tournament.recordmodel_set.all().order_by('placement')
13 tournament_decks = []
14 for record in tournament_records:
15 decklist = DeckList(name=record.decklist.name, url=record.decklist.url)
16 tournament_decks += [decklist]
17 if len(decklist.cards) > 0:
18 placements += [{'decklist': decklist, 'placement': record.placement}]
19
20 tournament_data += [{'tournament': tournament, 'decklists': tournament_decks}]
21
22 clustering = Clustering(records=placements)
23 ranking = [(int(avg), label, [decklist for decklist in clustering.clusters[label]]) for avg, label in clustering.ranking()]
24
25 return render(request, 'index.html', {
26 'ranking': ranking,
27 'tournaments': tournament_data})