From 0eeaf1ce214b595249a8bd1d6d9460459a1dfb55 Mon Sep 17 00:00:00 2001 From: Sli Date: Fri, 9 Aug 2024 17:33:07 +0200 Subject: [PATCH] Render user picture page with ajax to improve performances --- core/templates/core/user_pictures.jinja | 167 +++++++++++++----------- core/views/user.py | 17 --- sas/templates/sas/album.jinja | 6 +- 3 files changed, 93 insertions(+), 97 deletions(-) diff --git a/core/templates/core/user_pictures.jinja b/core/templates/core/user_pictures.jinja index e39e7c72..554604e0 100644 --- a/core/templates/core/user_pictures.jinja +++ b/core/templates/core/user_pictures.jinja @@ -17,9 +17,9 @@ {% endblock %} {% block content %} -
- {% if user.id == object.id and albums|length > 0 %} -
+
+ {% if user.id == object.id %} +
{% endif %} - {% for album, pictures in albums|items %} -

{{ album }}

-
-
- {% for picture in pictures %} - {% if picture.can_be_viewed_by(user) %} - + +
{% endblock content %} {% block script %} {{ super() }} - {% if user.id == object.id %} - - {% endif %} + return (await Promise.all(promises)).flat() + }, + + + async download_zip(){ + this.in_progress = true; + const bar = this.$refs.progress; + bar.value = 0; + bar.max = this.pictures.length; + + const fileHandle = await window.showSaveFilePicker({ + _preferPolyfill: false, + suggestedName: "{%- trans -%} pictures {%- endtrans -%}.zip", + types: {}, + excludeAcceptAllOption: false, + }) + const zipWriter = new zip.ZipWriter(await fileHandle.createWritable()); + + await Promise.all(this.pictures.map(p => { + const img_name = p.album + "/IMG_" + p.date.replaceAll(/[:\-]/g, "_") + p.name.slice(p.name.lastIndexOf(".")); + return zipWriter.add( + img_name, + new zip.HttpReader(p.full_size_url), + {level: 9, lastModDate: new Date(p.date), onstart: () => bar.value += 1} + ); + })); + + await zipWriter.close(); + this.in_progress = false; + } + })) + }); + {% endblock script %} diff --git a/core/views/user.py b/core/views/user.py index 87a4b6a3..27602d22 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -21,7 +21,6 @@ # Place - Suite 330, Boston, MA 02111-1307, USA. # # -import itertools import logging # This file contains all the views that concern the user model @@ -33,7 +32,6 @@ from django.contrib.auth import login, views from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.mixins import LoginRequiredMixin from django.core.exceptions import PermissionDenied, ValidationError -from django.db.models import F from django.forms import CheckboxSelectMultiple from django.forms.models import modelform_factory from django.http import Http404, HttpResponse @@ -70,7 +68,6 @@ from core.views.forms import ( UserProfileForm, ) from counter.forms import StudentCardForm -from sas.models import Picture from subscription.models import Subscription from trombi.views import UserTrombiForm @@ -312,20 +309,6 @@ class UserPicturesView(UserTabsMixin, CanViewMixin, DetailView): template_name = "core/user_pictures.jinja" current_tab = "pictures" - def get_context_data(self, **kwargs): - kwargs = super().get_context_data(**kwargs) - pictures = list( - Picture.objects.filter(people__user_id=self.object.id) - .order_by("-parent__date", "-date") - .annotate(album=F("parent__name")) - ) - kwargs["nb_pictures"] = len(pictures) - kwargs["albums"] = { - album: list(picts) - for album, picts in itertools.groupby(pictures, lambda i: i.album) - } - return kwargs - def delete_user_godfather(request, user_id, godfather_id, is_father): user_is_admin = request.user.is_root or request.user.is_board_member diff --git a/sas/templates/sas/album.jinja b/sas/templates/sas/album.jinja index fed66e66..b7bfc4c1 100644 --- a/sas/templates/sas/album.jinja +++ b/sas/templates/sas/album.jinja @@ -64,7 +64,11 @@