Propositions: move all queries into views.py
This commit is contained in:
parent
8a74d543d4
commit
c0c155fc94
5 changed files with 15 additions and 18 deletions
|
@ -1,5 +1,4 @@
|
|||
{% extends "gestion/base.html" %}
|
||||
{% load getresponse %}
|
||||
|
||||
{% block titre %}Propositions de morceau{% endblock %}
|
||||
|
||||
|
@ -17,6 +16,7 @@
|
|||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
{% for p in propositions %}
|
||||
<tr class="prop">
|
||||
|
@ -29,8 +29,12 @@
|
|||
<td>{{ p.nb_no }}</td>
|
||||
<td><a href="{% url "propositions:oui" p.id %}">Oui</a></td>
|
||||
<td><a href="{% url "propositions:non" p.id %}">Non</a></td>
|
||||
{% getresponse request.user p %}
|
||||
<td>{% if p.user == request.user.profile or request.user.profile.is_chef %}<a class="supprimer" href="{% url "propositions:delete" p.id %}">Supprimer</a>{% endif %}</td>
|
||||
<td>{% if p.user_answer %}Vous avez voté {{ p.user_answer }}{% endif %}</td>
|
||||
<td>
|
||||
{% if p.user == request.user or request.user.profile.is_chef %}
|
||||
<a class="supprimer" href="{% url "propositions:delete" p.id %}">Supprimer</a>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
<td>{% if reponse %}Vous avez voté {{ reponse }}{%endif%}</td>
|
|
@ -1,13 +0,0 @@
|
|||
from django import template
|
||||
from propositions.models import Answer
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.inclusion_tag("propositions/reponse.html")
|
||||
def getresponse(user, prop):
|
||||
try:
|
||||
answer = Answer.objects.values_list("answer").get(proposition=prop, user=user)
|
||||
return {"reponse": answer[0]}
|
||||
except Answer.DoesNotExist:
|
||||
return {}
|
|
@ -2,7 +2,7 @@ from django.shortcuts import redirect, get_object_or_404
|
|||
from django.urls import reverse_lazy
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||
from django.db.models import Count, Q
|
||||
from django.db.models import Count, OuterRef, Q, Subquery
|
||||
from django.views.generic import CreateView, DeleteView, ListView
|
||||
from django.http import HttpResponseRedirect
|
||||
|
||||
|
@ -28,10 +28,17 @@ class PropositionList(LoginRequiredMixin, ListView):
|
|||
model = Proposition
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
user_answers = (
|
||||
Answer.objects
|
||||
.filter(proposition=OuterRef("id"), user=user)
|
||||
.values_list("answer", flat=True)
|
||||
)
|
||||
return (
|
||||
Proposition.objects
|
||||
.annotate(nb_yes=Count("answer", filter=Q(answer__answer=Answer.YES)))
|
||||
.annotate(nb_no=Count("answer", filter=Q(answer__answer=Answer.NO)))
|
||||
.annotate(user_answer=Subquery(user_answers[:1]))
|
||||
.order_by("-nb_yes", "nb_no", "name")
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue