Added played games to profile page, paginated played games and challenges

This commit is contained in:
root 2019-11-28 01:59:15 +01:00
parent 3b6f10431f
commit 777694378f
2 changed files with 95 additions and 19 deletions

View File

@ -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',
@ -338,7 +365,3 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView):
except:
pass
return context
# handle reports differently if in a challenge (skip with score of 30000 instead of finding a random one)

View File

@ -26,7 +26,43 @@
<div class="col-lg-10 offset-lg-1 content">
<h3 class="section-header">Played Games</h3>
<hr>
<p>Coming soon</p>
{% if played_paginator %}
<table>
<tr>
<th style="width:70%">Game</th>
<th style="width:15%">Score</th>
<th style="width:15%"></th>
</tr>
{% for game in played_paginator %}
<tr>
<td style="width:60%">
{% if game.challenge %}{{game.challenge.name}}{% else %}Random Game{% endif %}
</td>
<td>
{{game.score|intcomma}}
</td>
<td>
<a class="btn btn-primary btn-block" href="{% url 'game:end-recap-view' game_pk=game.pk %}">View</a>
</td>
</tr>
{% endfor %}
</table>
{% if played_is_paginated %}
<ul class="pagination">
{% if played_paginator.has_previous %}
<li class="page-item"><a class="page-link" href="?played=1&challenges={{challenges_paginator.number}}">First</a></li>
<li class="page-item"><a class="page-link" href="?played={{ played_paginator.previous_page_number }}&challenges={{challenges_paginator.number}}">{{ played_paginator.previous_page_number }}</a></li>
{% endif %}
<li class="page-item active"><a class="page-link" href="#">{{ played_paginator.number }}</a></li>
{% if played_paginator.has_next %}
<li class="page-item"><a class="page-link" href="?played={{ played_paginator.next_page_number }}&challenges={{challenges_paginator.number}}">{{ played_paginator.next_page_number }}</a></li>
<li class="page-item"><a class="page-link" href="?played={{ played_paginator.paginator.num_pages }}&challenges={{challenges_paginator.number}}">Last</a></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<p>You haven't played any games yet, what are you waiting for?</p>
{% endif %}
</div>
</div>
</div>
@ -35,20 +71,37 @@
<div class="col-lg-10 offset-lg-1 content">
<h3 class="section-header">Your Contributions</h3>
<hr>
<table>
<tr>
<th>Challenge Name</th>
<th>Average Score</th>
<th></th>
</tr>
{% for challenge in user_challenges %}
{% if challenges_paginator %}
<table>
<tr>
<td>{{challenge.name}}</td>
<td>{{challenge.average|intcomma}}</td>
<td><a class="btn btn-warning btn-block" href="{% url 'game:edit-challenge' pk=challenge.pk %}" role="button">Edit</a></td>
<th style="width:70%">Challenge Name</th>
<th style="width:15%">Average Score</th>
<th style="width:15%"></th>
</tr>
{% endfor %}
</table>
{% for challenge in challenges_paginator %}
<tr>
<td>{{challenge.name}}</td>
<td>{{challenge.average|intcomma}}</td>
<td><a class="btn btn-warning btn-block" href="{% url 'game:edit-challenge' pk=challenge.pk %}" role="button">Edit</a></td>
</tr>
{% endfor %}
</table>
{% if challenges_is_paginated %}
<ul class="pagination">
{% if challenges_paginator.has_previous %}
<li class="page-item"><a class="page-link" href="?challenges=1&played={{played_paginator.number}}">First</a></li>
<li class="page-item"><a class="page-link" href="?challenges={{ challenges_paginator.previous_page_number }}&played={{played_paginator.number}}">{{ challenges_paginator.previous_page_number }}</a></li>
{% endif %}
<li class="page-item active"><a class="page-link" href="#">{{ challenges_paginator.number }}</a></li>
{% if challenges_paginator.has_next %}
<li class="page-item"><a class="page-link" href="?challenges={{ challenges_paginator.next_page_number }}&played={{played_paginator.number}}">{{ challenges_paginator.next_page_number }}</a></li>
<li class="page-item"><a class="page-link" href="?challenges={{ challenges_paginator.paginator.num_pages }}&played={{played_paginator.number}}">Last</a></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<p>You haven't added any challenges, try adding one.</p>
{% endif %}
<hr>
<a class="btn btn-primary btn-lg" href="{% url 'game:create-challenge' %}">Create a New Challenge</a>
</div>