diff --git a/src/profiles/templates/tokens/tokenfind_list.html b/src/profiles/templates/tokens/tokenfind_list.html index 05e6e464..c07adc58 100644 --- a/src/profiles/templates/tokens/tokenfind_list.html +++ b/src/profiles/templates/tokens/tokenfind_list.html @@ -1,6 +1,7 @@ {% extends 'profile_base.html' %} {% load static from staticfiles %} {% load commonmark %}$ +{% load token_tags %} {% block title %} Your Secret Tokens | {{ block.super }} @@ -12,44 +13,42 @@ Your Secret Tokens | {{ block.super }}

Your Secret Tokens

+

You have found the following secret tokens in the BornHack Secret Token Game:

- + + {% for token in object_list %} + {% ifchanged token.camp %} + + + - - - - {% for tokenfind in object_list %} + {% endifchanged %} - - - - - - - {% endfor %} - {% for unfound in unfound_list %} - - - + + + {% with token|found_by_user:user as user_has_found_token %} + {% if user_has_found_token %} + + + + {% else %} + {% endif %} + {% endwith %} + {% endfor %}

{{ token.camp.title }}

Camp Category Token Description Found
{{ tokenfind.token.camp.title }}{{ tokenfind.token.category }}{{ tokenfind.token.token }}{{ tokenfind.token.description }}{{ tokenfind.created }}
{{ unfound.camp.title }}{{ unfound.category }}{{ token.category }}{{ token.token }}{{ token.description }}{{ user_has_found_token }}- - -
- - {% if not unfound_list %} -

Congratulations! You've found all tokens we have created right now.

- {% endif %} -
{% endblock %} diff --git a/src/tokens/models.py b/src/tokens/models.py index f54cc59b..337d9584 100644 --- a/src/tokens/models.py +++ b/src/tokens/models.py @@ -26,6 +26,9 @@ class Token(CampRelatedModel): def __str__(self): return '%s (%s)' % (self.description, self.camp) + class Meta: + ordering = ["camp"] + class TokenFind(CampRelatedModel): class Meta: diff --git a/src/tokens/templatetags/__init__.py b/src/tokens/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tokens/templatetags/token_tags.py b/src/tokens/templatetags/token_tags.py new file mode 100644 index 00000000..630c22fa --- /dev/null +++ b/src/tokens/templatetags/token_tags.py @@ -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 + diff --git a/src/tokens/views.py b/src/tokens/views.py index 1fda527a..6143c233 100644 --- a/src/tokens/views.py +++ b/src/tokens/views.py @@ -20,18 +20,13 @@ class TokenDetailView(LoginRequiredMixin, DetailView): class TokenFindListView(LoginRequiredMixin, ListView): - """ - This class is meant to be extended in other apps like `profiles`. - """ - model = TokenFind + model = Token template_name = "tokens/tokenfind_list.html" - def get_queryset(self): - return TokenFind.objects.filter(user=self.request.user) - def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # 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