From 777694378f8f42cb27082dc20b49f039d8f01e1c Mon Sep 17 00:00:00 2001 From: root Date: Thu, 28 Nov 2019 01:59:15 +0100 Subject: [PATCH] Added played games to profile page, paginated played games and challenges --- geogame/main/views.py | 35 ++++++++++--- geogame/templates/main/profile.html | 79 ++++++++++++++++++++++++----- 2 files changed, 95 insertions(+), 19 deletions(-) diff --git a/geogame/main/views.py b/geogame/main/views.py index f10758b..37ab5a7 100644 --- a/geogame/main/views.py +++ b/geogame/main/views.py @@ -7,6 +7,7 @@ from django.views.generic.detail import DetailView from django.views import View from django.contrib import messages from django.urls import reverse_lazy +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from braces import views @@ -51,7 +52,30 @@ class ProfilePageView(views.LoginRequiredMixin, TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) user = self.request.user - context['user_challenges'] = Challenge.objects.filter(user=user) + games = Game.objects.filter(user=user, active=False).order_by('-id') + + paginator = Paginator(games, 10) + page = self.request.GET.get('played') + try: + played_paginator = paginator.page(page) + except PageNotAnInteger: + played_paginator = paginator.page(1) + except EmptyPage: + played_paginator = paginator.page(paginator.num_pages) + context['played_is_paginated'] = played_paginator.has_other_pages() + context['played_paginator'] = played_paginator + + challenges = Challenge.objects.filter(user=user).order_by('-id') + paginator = Paginator(challenges, 10) + page = self.request.GET.get('challenges') + try: + challenges_paginator = paginator.page(page) + except PageNotAnInteger: + challenges_paginator = paginator.page(1) + except EmptyPage: + challenges_paginator = paginator.page(paginator.num_pages) + context['challenges_is_paginated'] = challenges_paginator.has_other_pages() + context['challenges_paginator'] = challenges_paginator return context @@ -197,6 +221,9 @@ class RoundView(views.UserPassesTestMixin, UpdateView): self.object.guess_lat = 0 self.object.guess_lng = 0 self.object.save() + #add to the score and save against the game object, makes for easier score retrieval later + self.object.game.score = self.object.game.score + self.object.result + self.object.game.save() return redirect( reverse_lazy( 'game:round-recap-view', @@ -337,8 +364,4 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView): context['all_average'] = game.challenge.average except: pass - return context - - - -# handle reports differently if in a challenge (skip with score of 30000 instead of finding a random one) \ No newline at end of file + return context \ No newline at end of file diff --git a/geogame/templates/main/profile.html b/geogame/templates/main/profile.html index b14a67f..8c02600 100644 --- a/geogame/templates/main/profile.html +++ b/geogame/templates/main/profile.html @@ -26,7 +26,43 @@

Played Games


-

Coming soon

+ {% if played_paginator %} + + + + + + + {% for game in played_paginator %} + + + + + + {% endfor %} +
GameScore
+ {% if game.challenge %}{{game.challenge.name}}{% else %}Random Game{% endif %} + + {{game.score|intcomma}} + + View +
+ {% if played_is_paginated %} + + {% endif %} + {% else %} +

You haven't played any games yet, what are you waiting for?

+ {% endif %}
@@ -35,20 +71,37 @@

Your Contributions


- - - - - - - {% for challenge in user_challenges %} + {% if challenges_paginator %} +
Challenge NameAverage Score
- - - + + + - {% endfor %} -
{{challenge.name}}{{challenge.average|intcomma}}EditChallenge NameAverage Score
+ {% for challenge in challenges_paginator %} + + {{challenge.name}} + {{challenge.average|intcomma}} + Edit + + {% endfor %} + + {% if challenges_is_paginated %} + + {% endif %} + {% else %} +

You haven't added any challenges, try adding one.

+ {% endif %}
Create a New Challenge