From 321e5e3ff573876f183a599460e12c93bb8b0536 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sun, 15 Sep 2019 16:00:50 +0200 Subject: [PATCH 1/5] forum: enhance performances on toggle_favorite --- forum/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forum/views.py b/forum/views.py index 5ffdfea8..49cee753 100644 --- a/forum/views.py +++ b/forum/views.py @@ -280,7 +280,7 @@ class ForumTopicSubscribeView(CanViewMixin, SingleObjectMixin, RedirectView): def get(self, request, *args, **kwargs): self.object = self.get_object() - if request.user in self.object.subscribed_users.all(): + if self.object.subscribed_users.filter(id=request.user.id).exists(): self.object.subscribed_users.remove(request.user) else: self.object.subscribed_users.add(request.user) From 1f86827e46df0d8c88955876890d0dc8ab70bd36 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sun, 15 Sep 2019 16:22:13 +0200 Subject: [PATCH 2/5] core: improve performances on not found images --- core/views/files.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/views/files.py b/core/views/files.py index 60902cfe..683a6f1e 100644 --- a/core/views/files.py +++ b/core/views/files.py @@ -23,7 +23,7 @@ # # This file contains all the views that concern the page model -from django.shortcuts import redirect +from django.shortcuts import redirect, get_object_or_404 from django.views.generic import ListView, DetailView, TemplateView from django.views.generic.edit import UpdateView, FormMixin, DeleteView from django.views.generic.detail import SingleObjectMixin @@ -51,9 +51,7 @@ def send_file(request, file_id, file_class=SithFile, file_attr="file"): memory at once. The FileWrapper will turn the file object into an iterator for chunks of 8KB. """ - f = file_class.objects.filter(id=file_id).first() - if f is None or not f.file: - return not_found(request) + f = get_object_or_404(file_class, id=file_id) if not ( can_view(f, request.user) or ( From 8330e1eaf2b20f2a4342b747c4b56bf894a9d7d3 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sun, 15 Sep 2019 16:43:17 +0200 Subject: [PATCH 3/5] sas: simplify and optimize permissions for SAS images --- sas/models.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/sas/models.py b/sas/models.py index a92d41c6..12651e91 100644 --- a/sas/models.py +++ b/sas/models.py @@ -71,16 +71,11 @@ class Picture(SithFile): return False def can_be_edited_by(self, user): - # file = SithFile.objects.filter(id=self.id).first() - return user.is_in_group( - settings.SITH_GROUP_SAS_ADMIN_ID - ) # or user.can_edit(file) + return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID) def can_be_viewed_by(self, user): - # file = SithFile.objects.filter(id=self.id).first() - return self.can_be_edited_by(user) or ( - self.is_in_sas and self.is_moderated and user.was_subscribed - ) # or user.can_view(file) + # SAS pictures are visible to old subscribers + return self.is_in_sas and self.is_moderated and user.was_subscribed def get_download_url(self): return reverse("sas:download", kwargs={"picture_id": self.id}) From b8ad2d4835bc17a7932d7d2eb737e22303c67dac Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Mon, 16 Sep 2019 01:26:20 +0200 Subject: [PATCH 4/5] sas: add pagination on AlbumView --- core/templates/core/macros.jinja | 20 +++++++++++--------- sas/templates/sas/album.jinja | 5 ++++- sas/views.py | 18 +++++++++++++++++- sith/settings.py | 1 + 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index 36b9ed05..d9dd68c2 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -114,22 +114,24 @@ {% endmacro %} {% macro paginate(page_obj, paginator) %} + {% if page_obj.has_previous() or page_obj.has_next() %} {% if page_obj.has_previous() %} {% trans %}Previous{% endtrans %} {% else %} {% trans %}Previous{% endtrans %} {% endif %} - {% for i in paginator.page_range %} - {% if page_obj.number == i %} - {{ i }} ({% trans %}current{% endtrans %}) + {% for i in paginator.page_range %} + {% if page_obj.number == i %} + {{ i }} ({% trans %}current{% endtrans %}) + {% else %} + {{ i }} + {% endif %} + {% endfor %} + {% if page_obj.has_next() %} + {% trans %}Next{% endtrans %} {% else %} - {{ i }} + {% trans %}Next{% endtrans %} {% endif %} - {% endfor %} - {% if page_obj.has_next() %} - {% trans %}Next{% endtrans %} - {% else %} - {% trans %}Next{% endtrans %} {% endif %} {% endmacro %} diff --git a/sas/templates/sas/album.jinja b/sas/templates/sas/album.jinja index 38cd2431..6958adc6 100644 --- a/sas/templates/sas/album.jinja +++ b/sas/templates/sas/album.jinja @@ -1,4 +1,5 @@ {% extends "core/base.jinja" %} +{% from "core/macros.jinja" import paginate %} {% block title %} {% trans %}SAS{% endtrans %} @@ -62,7 +63,7 @@ {% endfor %}
- {% for p in album.children_pictures.order_by('id') %} + {% for p in pictures %}
{% if edit_mode %} @@ -77,6 +78,8 @@
{% endfor %}
+
+ {{ paginate(pictures, paginator) }} {% if edit_mode %} {% endif %} diff --git a/sas/views.py b/sas/views.py index 859b4a7f..6cbdc319 100644 --- a/sas/views.py +++ b/sas/views.py @@ -23,7 +23,7 @@ # from django.shortcuts import redirect -from django.http import HttpResponse +from django.http import HttpResponse, Http404 from django.core.urlresolvers import reverse_lazy, reverse from core.views.forms import SelectDate from django.views.generic import DetailView, TemplateView @@ -32,6 +32,7 @@ from django.utils.translation import ugettext_lazy as _ from django.conf import settings from django import forms from django.core.exceptions import PermissionDenied +from django.core.paginator import Paginator, InvalidPage from ajax_select import make_ajax_field from ajax_select.fields import AutoCompleteSelectMultipleField @@ -252,6 +253,14 @@ class AlbumView(CanViewMixin, DetailView, FormMixin): form_class = SASForm pk_url_kwarg = "album_id" template_name = "sas/album.jinja" + paginate_by = settings.SITH_SAS_IMAGES_PER_PAGE + + def dispatch(self, request, *args, **kwargs): + try: + self.asked_page = int(request.GET.get("page", 1)) + except ValueError: + raise Http404 + return super(AlbumView, self).dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): self.form = self.get_form() @@ -291,6 +300,13 @@ class AlbumView(CanViewMixin, DetailView, FormMixin): def get_context_data(self, **kwargs): kwargs = super(AlbumView, self).get_context_data(**kwargs) + kwargs["paginator"] = Paginator( + self.object.children_pictures.order_by("id"), self.paginate_by + ) + try: + kwargs["pictures"] = kwargs["paginator"].page(self.asked_page) + except InvalidPage: + raise Http404 kwargs["form"] = self.form kwargs["clipboard"] = SithFile.objects.filter( id__in=self.request.session["clipboard"] diff --git a/sith/settings.py b/sith/settings.py index 33c45087..50f95924 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -338,6 +338,7 @@ SITH_FORUM_PAGE_LENGTH = 30 # SAS variables SITH_SAS_ROOT_DIR_ID = 4 +SITH_SAS_IMAGES_PER_PAGE = 30 SITH_BOARD_SUFFIX = "-bureau" SITH_MEMBER_SUFFIX = "-membres" From 298499c749235703d0366d3212daa8b1aec8683e Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Mon, 16 Sep 2019 02:14:57 +0200 Subject: [PATCH 5/5] sas: add cache on Picture permissions to avoid hitting the bdd too much --- sas/models.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sas/models.py b/sas/models.py index 12651e91..2a691f38 100644 --- a/sas/models.py +++ b/sas/models.py @@ -24,6 +24,7 @@ from django.db import models from django.core.urlresolvers import reverse +from django.core.cache import cache from django.conf import settings from django.utils.translation import ugettext_lazy as _ from django.utils import timezone @@ -75,7 +76,15 @@ class Picture(SithFile): def can_be_viewed_by(self, user): # SAS pictures are visible to old subscribers - return self.is_in_sas and self.is_moderated and user.was_subscribed + # Result is cached 4s for this user + if user.is_anonymous: + return False + perm = cache.get("%d_can_view_pictures" % (user.id), False) + if perm: + return perm + perm = self.is_in_sas and self.is_moderated and user.was_subscribed + cache.set("%d_can_view_pictures" % (user.id), perm, timeout=4) + return perm def get_download_url(self): return reverse("sas:download", kwargs={"picture_id": self.id})