Réécrit des vues pour la nouvelle fonctionnalité

On en profite pour rajouter des Mixins pour les perms buro/cof
This commit is contained in:
Ludovic Stephan 2019-06-08 15:04:55 +02:00 committed by Ludovic Stephan
parent b37e7c4c41
commit a67446048e
3 changed files with 34 additions and 13 deletions

View file

@ -23,7 +23,11 @@ urlpatterns = [
views.spectacle, views.spectacle,
name="bda-spectacle", name="bda-spectacle",
), ),
url(r"^spectacles/unpaid/(?P<tirage_id>\d+)$", views.unpaid, name="bda-unpaid"), url(
r"^spectacles/unpaid/(?P<tirage_id>\d+)$",
views.UnpaidParticipants.as_view(),
name="bda-unpaid",
),
url( url(
r"^spectacles/autocomplete$", r"^spectacles/autocomplete$",
views.spectacle_autocomplete, views.spectacle_autocomplete,

View file

@ -41,7 +41,7 @@ from bda.models import (
SpectacleRevente, SpectacleRevente,
Tirage, Tirage,
) )
from gestioncof.decorators import buro_required, cof_required from gestioncof.decorators import BuroRequiredMixin, buro_required, cof_required
from utils.views.autocomplete import Select2QuerySetView from utils.views.autocomplete import Select2QuerySetView
@ -378,7 +378,7 @@ def revente_manage(request, tirage_id):
- Annulation d'une revente après que le tirage a eu lieu - Annulation d'une revente après que le tirage a eu lieu
""" """
tirage = get_object_or_404(Tirage, id=tirage_id) tirage = get_object_or_404(Tirage, id=tirage_id)
participant, created = Participant.objects.get_or_create( participant, created = Participant.objects_paid.get_or_create(
user=request.user, tirage=tirage user=request.user, tirage=tirage
) )
@ -695,12 +695,13 @@ def spectacle(request, tirage_id, spectacle_id):
"username": participant.user.username, "username": participant.user.username,
"email": participant.user.email, "email": participant.user.email,
"given": int(attrib.given), "given": int(attrib.given),
"paid": participant.paid, "paid": True,
"nb_places": 1, "nb_places": 1,
} }
if participant.id in participants: if participant.id in participants:
participants[participant.id]["nb_places"] += 1 participants[participant.id]["nb_places"] += 1
participants[participant.id]["given"] += attrib.given participants[participant.id]["given"] += attrib.given
participants[participant.id]["paid"] &= attrib.paid
else: else:
participants[participant.id] = participant_info participants[participant.id] = participant_info
@ -728,15 +729,14 @@ class SpectacleListView(ListView):
return context return context
@buro_required class UnpaidParticipants(BuroRequiredMixin, ListView):
def unpaid(request, tirage_id): context_object_name = "unpaid"
tirage = get_object_or_404(Tirage, id=tirage_id) template_name = "bda-unpaid.html"
unpaid = (
tirage.participant_set.annotate(nb_attributions=Count("attribution")) def get_queryset(self):
.filter(paid=False, nb_attributions__gt=0) return Participant.objects_paid.filter(
.select_related("user") tirage__id=self.kwargs["tirage_id"], paid=False
) ).select_related("user")
return render(request, "bda-unpaid.html", {"unpaid": unpaid})
@buro_required @buro_required

View file

@ -1,6 +1,7 @@
from functools import wraps from functools import wraps
from django.contrib.auth.decorators import login_required, user_passes_test from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.shortcuts import render from django.shortcuts import render
@ -53,3 +54,19 @@ def buro_required(view_func):
return render(request, "buro-denied.html", status=403) return render(request, "buro-denied.html", status=403)
return login_required(_wrapped_view) return login_required(_wrapped_view)
class CofRequiredMixin(PermissionRequiredMixin):
def has_permission(self):
try:
return self.request.user.profile.is_cof
except AttributeError:
return False
class BuroRequiredMixin(PermissionRequiredMixin):
def has_permission(self):
try:
return self.request.user.profile.is_buro
except AttributeError:
return False