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