Merge pull request #962 from ae-utbm/query-news

Fix N+1 queries on birthdays
This commit is contained in:
thomas girod 2024-12-22 06:32:58 +01:00 committed by GitHub
commit 9ca95774a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 21 deletions

View File

@ -136,25 +136,22 @@ type="EVENT").order_by('dates__start_date') %}
<div id="birthdays"> <div id="birthdays">
<div id="birthdays_title">{% trans %}Birthdays{% endtrans %}</div> <div id="birthdays_title">{% trans %}Birthdays{% endtrans %}</div>
<div id="birthdays_content"> <div id="birthdays_content">
{% if user.is_subscribed %} {%- if user.is_subscribed -%}
{# Cache request for 1 hour #} <ul class="birthdays_year">
{% cache 3600 "birthdays" %} {%- for year, users in birthdays -%}
<ul class="birthdays_year"> <li>
{% for d in birthdays.dates('date_of_birth', 'year', 'DESC') %} {% trans age=timezone.now().year - year %}{{ age }} year old{% endtrans %}
<li> <ul>
{% trans age=timezone.now().year - d.year %}{{ age }} year old{% endtrans %} {%- for u in users -%}
<ul> <li><a href="{{ u.get_absolute_url() }}">{{ u.get_short_name() }}</a></li>
{% for u in birthdays.filter(date_of_birth__year=d.year) %} {%- endfor -%}
<li><a href="{{ u.get_absolute_url() }}">{{ u.get_short_name() }}</a></li> </ul>
{% endfor %} </li>
</ul> {%- endfor -%}
</li> </ul>
{% endfor %} {%- else -%}
</ul>
{% endcache %}
{% else %}
<p>{% trans %}You need an up to date subscription to access this content{% endtrans %}</p> <p>{% trans %}You need an up to date subscription to access this content{% endtrans %}</p>
{% endif %} {%- endif -%}
</div> </div>
</div> </div>
</div> </div>

View File

@ -21,7 +21,7 @@
# Place - Suite 330, Boston, MA 02111-1307, USA. # Place - Suite 330, Boston, MA 02111-1307, USA.
# #
# #
import itertools
from datetime import timedelta from datetime import timedelta
from smtplib import SMTPRecipientsRefused from smtplib import SMTPRecipientsRefused
@ -374,13 +374,14 @@ class NewsListView(CanViewMixin, ListView):
kwargs = super().get_context_data(**kwargs) kwargs = super().get_context_data(**kwargs)
kwargs["NewsDate"] = NewsDate kwargs["NewsDate"] = NewsDate
kwargs["timedelta"] = timedelta kwargs["timedelta"] = timedelta
kwargs["birthdays"] = ( kwargs["birthdays"] = itertools.groupby(
User.objects.filter( User.objects.filter(
date_of_birth__month=localdate().month, date_of_birth__month=localdate().month,
date_of_birth__day=localdate().day, date_of_birth__day=localdate().day,
) )
.filter(role__in=["STUDENT", "FORMER STUDENT"]) .filter(role__in=["STUDENT", "FORMER STUDENT"])
.order_by("-date_of_birth") .order_by("-date_of_birth"),
key=lambda u: u.date_of_birth.year,
) )
return kwargs return kwargs