-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
91 lines (74 loc) · 3.45 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from django.core.urlresolvers import reverse_lazy
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect
from django.views import generic
from django.contrib.auth import login, logout
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
# from django.db.models import Count
from play.models import Player, Game
class SignUpView(generic.CreateView):
model = User
template_name = "accounts/signup.html"
form_class = UserCreationForm
success_url = reverse_lazy("login")
class LoginView(generic.FormView):
template_name = "accounts/login.html"
form_class = AuthenticationForm
success_url = reverse_lazy("home")
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated():
return HttpResponseRedirect(self.get_success_url())
else:
return super(LoginView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
login(self.request, form.get_user())
return super(LoginView, self).form_valid(form)
class LogoutView(generic.RedirectView):
pattern_name = "home"
def get(self, request, *args, **kwargs):
logout(request)
return super(LogoutView, self).get(request, *args, **kwargs)
class HomePageView(generic.TemplateView):
template_name = "home.html"
def get(self, request, *args, **kwargs):
if request.user.is_authenticated():
try:
player = self.request.user.player
self.continue_game = player.game_set.latest().active
# no games played yet
except ObjectDoesNotExist:
self.continue_game = False
else:
self.continue_game = False
return super(HomePageView, self).get(request, *args, **kwargs)
def get_context_data(self):
context = super(HomePageView, self).get_context_data()
context["continue_game"] = self.continue_game
# the first solution below is optimal as far as the query
# is concerned but if a player has not won/lost/drawn any game yet
# then he does not appear in the results, making the lists shorter
# and messing things up
#players_won = Player.objects.filter(
# game__won_by=Game.PLAYER).annotate(nwon=Count('game'))
#players_lost = Player.objects.filter(
# game__won_by=Game.COMPUTER).annotate(nlost=Count('game'))
#players_draw = Player.objects.filter(
# game__won_by=Game.DRAW).annotate(ndraw=Count('game'))
#hall_fame = []
#for pwon, plost, pdraw in zip(players_won, players_lost, players_draw):
# hall_fame.append((pwon.user.username,
# pwon.nwon, plost.nlost, pdraw.ndraw))
# so, the best thing to do is to scan each player for his games
players = Player.objects.all()
hall_fame = []
for p in players:
nwon = p.game_set.filter(won_by=Game.PLAYER).count()
nlost = p.game_set.filter(won_by=Game.COMPUTER).count()
ndraw = p.game_set.filter(won_by=Game.DRAW).count()
player_stats = (p.user.username, nwon, nlost, ndraw)
hall_fame.append(player_stats)
# sort the players by nwon #of games won
hall_fame.sort(key=lambda h: h[1], reverse=True)
context["hall_fame"] = enumerate(hall_fame, start=1)
return context