From 0eeaf1ce214b595249a8bd1d6d9460459a1dfb55 Mon Sep 17 00:00:00 2001 From: Sli Date: Fri, 9 Aug 2024 17:33:07 +0200 Subject: [PATCH 1/4] 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 @@
+
-
- - -
+ + +
{% endblock content %} From 9188c28ee7924cfa65d8fbad8a1e6b078cc548ed Mon Sep 17 00:00:00 2001 From: Sli Date: Fri, 16 Aug 2024 22:52:20 +0200 Subject: [PATCH 4/4] Remove intersect --- core/static/core/js/alpine/intersect.min.js | 1 - .../core/js/{alpine => }/alpinejs.min.js | 0 core/templates/core/base.jinja | 2 +- core/templates/core/user_pictures.jinja | 49 +++++++++---------- 4 files changed, 24 insertions(+), 28 deletions(-) delete mode 100644 core/static/core/js/alpine/intersect.min.js rename core/static/core/js/{alpine => }/alpinejs.min.js (100%) diff --git a/core/static/core/js/alpine/intersect.min.js b/core/static/core/js/alpine/intersect.min.js deleted file mode 100644 index 2342257f..00000000 --- a/core/static/core/js/alpine/intersect.min.js +++ /dev/null @@ -1 +0,0 @@ -(()=>{function o(e){e.directive("intersect",e.skipDuringClone((t,{value:i,expression:l,modifiers:n},{evaluateLater:r,cleanup:c})=>{let s=r(l),a={rootMargin:x(n),threshold:f(n)},u=new IntersectionObserver(d=>{d.forEach(h=>{h.isIntersecting!==(i==="leave")&&(s(),n.includes("once")&&u.disconnect())})},a);u.observe(t),c(()=>{u.disconnect()})}))}function f(e){if(e.includes("full"))return .99;if(e.includes("half"))return .5;if(!e.includes("threshold"))return 0;let t=e[e.indexOf("threshold")+1];return t==="100"?1:t==="0"?0:Number(`.${t}`)}function p(e){let t=e.match(/^(-?[0-9]+)(px|%)?$/);return t?t[1]+(t[2]||"px"):void 0}function x(e){let t="margin",i="0px 0px 0px 0px",l=e.indexOf(t);if(l===-1)return i;let n=[];for(let r=1;r<5;r++)n.push(p(e[l+r]||""));return n=n.filter(r=>r!==void 0),n.length?n.join(" ").trim():i}document.addEventListener("alpine:init",()=>{window.Alpine.plugin(o)});})(); diff --git a/core/static/core/js/alpine/alpinejs.min.js b/core/static/core/js/alpinejs.min.js similarity index 100% rename from core/static/core/js/alpine/alpinejs.min.js rename to core/static/core/js/alpinejs.min.js diff --git a/core/templates/core/base.jinja b/core/templates/core/base.jinja index 1704c08e..22b1ef7d 100644 --- a/core/templates/core/base.jinja +++ b/core/templates/core/base.jinja @@ -29,7 +29,7 @@ {% block additional_js %}{% endblock %} {# Alpine JS must be loaded after scripts that use it. #} - + {% endblock %} diff --git a/core/templates/core/user_pictures.jinja b/core/templates/core/user_pictures.jinja index 18527a3c..bd5f1d44 100644 --- a/core/templates/core/user_pictures.jinja +++ b/core/templates/core/user_pictures.jinja @@ -10,7 +10,6 @@ window.showSaveFilePicker = showSaveFilePicker; /* Export function to normal javascript */ - {% endblock %} {% block title %} @@ -33,31 +32,29 @@ {% endif %} -