1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-07-18 15:20:19 +02:00

Challenge detail View

This commit is contained in:
Ehouarn
2025-07-09 16:33:05 +02:00
parent c7bd733911
commit 6f4fbecdd0
5 changed files with 95 additions and 2 deletions

View File

@ -5,6 +5,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView, UpdateView
from django.utils.translation import gettext_lazy as _
from django_tables2 import SingleTableView
from permission.backends import PermissionBackend
from permission.views import ProtectQuerysetMixin, ProtectedCreateView
from .models import Family, Challenge
@ -54,6 +55,21 @@ class FamilyUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
extra_context = {"title": _('Update family')}
class ChallengeCreateView(ProtectQuerysetMixin, ProtectedCreateView):
"""
Create challenge
"""
model = Challenge
extra_context = {"title": _('Create challenge')}
def get_sample_object(self):
return Challenge(
name="",
description="Sample challenge",
points=0,
)
class ChallengeListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
"""
List all challenges
@ -61,3 +77,35 @@ class ChallengeListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableVie
model = Challenge
table_class = ChallengeTable
extra_context = {"title": _('Challenges list')}
class ChallengeDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
"""
Display details of a challenge
"""
model = Challenge
context_object_name = "challenge"
extra_context = {"title": _('Details of:')}
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
fields = ["name", "description", "points",]
fields = dict([(field, getattr(self.object, field)) for field in fields])
context["fields"] = [(
Challenge._meta.get_field(field).verbose_name.capitalize(),
value) for field, value in fields.items()]
context["obtained"] = getattr(self.object, "obtained")
context["update"] = PermissionBackend.check_perm(self.request, "family.change_challenge")
return context
class ChallengeUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
"""
Update the information of a challenge
"""
model = Challenge
context_object_name = "challenge"
extra_context = {"title": _('Update challenge')}
template_name = 'family/challenge_update.html'