Segment tokens by camps.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-27 12:33:14 +01:00
parent 96978f4223
commit 707eeb9190
5 changed files with 41 additions and 29 deletions

View file

@ -1,6 +1,7 @@
{% extends 'profile_base.html' %} {% extends 'profile_base.html' %}
{% load static from staticfiles %} {% load static from staticfiles %}
{% load commonmark %}$ {% load commonmark %}$
{% load token_tags %}
{% block title %} {% block title %}
Your Secret Tokens | {{ block.super }} Your Secret Tokens | {{ block.super }}
@ -12,44 +13,42 @@ Your Secret Tokens | {{ block.super }}
<h4>Your Secret Tokens</h4> <h4>Your Secret Tokens</h4>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<p class="lead">You have found the following secret tokens in the BornHack Secret Token Game:</p> <p class="lead">You have found the following secret tokens in the BornHack Secret Token Game:</p>
<table class="table"> <table class="table">
<thead> <tbody>
{% for token in object_list %}
{% ifchanged token.camp %}
<tr>
<td colspan="4"><h4>{{ token.camp.title }}</h4></td>
</tr>
<tr> <tr>
<th>Camp</th>
<th>Category</th> <th>Category</th>
<th>Token</th> <th>Token</th>
<th>Description</th> <th>Description</th>
<th>Found</th> <th>Found</th>
</tr> </tr>
</thead> {% endifchanged %}
<tbody>
{% for tokenfind in object_list %}
<tr> <tr>
<td>{{ tokenfind.token.camp.title }}</td> <td>{{ token.category }}</td>
<td>{{ tokenfind.token.category }}</td>
<td>{{ tokenfind.token.token }}</td> {% with token|found_by_user:user as user_has_found_token %}
<td>{{ tokenfind.token.description }}</td> {% if user_has_found_token %}
<td>{{ tokenfind.created }}</td> <td>{{ token.token }}</td>
</tr> <td>{{ token.description }}</td>
{% endfor %} <td>{{ user_has_found_token }}</td>
{% for unfound in unfound_list %} {% else %}
<tr>
<td>{{ unfound.camp.title }}</td>
<td>{{ unfound.category }}</td>
<td>-</td> <td>-</td>
<td>-</td> <td>-</td>
<td>-</td> <td>-</td>
{% endif %}
{% endwith %}
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
{% if not unfound_list %}
<p class="lead">Congratulations! You've found all tokens we have created right now.</p>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}

View file

@ -26,6 +26,9 @@ class Token(CampRelatedModel):
def __str__(self): def __str__(self):
return '%s (%s)' % (self.description, self.camp) return '%s (%s)' % (self.description, self.camp)
class Meta:
ordering = ["camp"]
class TokenFind(CampRelatedModel): class TokenFind(CampRelatedModel):
class Meta: class Meta:

View file

View file

@ -0,0 +1,15 @@
from django import template
from tokens.models import TokenFind
register = template.Library()
@register.filter
def found_by_user(token, user):
try:
tokenfind = TokenFind.objects.get(token=token, user=user)
return tokenfind.created
except TokenFind.DoesNotExist:
return False

View file

@ -20,18 +20,13 @@ class TokenDetailView(LoginRequiredMixin, DetailView):
class TokenFindListView(LoginRequiredMixin, ListView): class TokenFindListView(LoginRequiredMixin, ListView):
""" model = Token
This class is meant to be extended in other apps like `profiles`.
"""
model = TokenFind
template_name = "tokens/tokenfind_list.html" template_name = "tokens/tokenfind_list.html"
def get_queryset(self):
return TokenFind.objects.filter(user=self.request.user)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
# find the tokens the user still needs to find # find the tokens the user still needs to find
context['unfound_list'] = Token.objects.all().exclude(id__in=TokenFind.objects.filter(user=self.request.user).values_list('token__id', flat=True)) tokenfinds = TokenFind.objects.filter(user=self.request.user).values_list('token__id', flat=True)
context['unfound_list'] = Token.objects.all().exclude(id__in=tokenfinds)
return context return context