From bc40b9274432f8d5ed6102c84ddea137c07fddcb Mon Sep 17 00:00:00 2001 From: thomas girod Date: Tue, 3 Sep 2024 20:15:37 +0200 Subject: [PATCH] completely ajaxify the picture page --- core/models.py | 4 +- core/static/core/js/script.js | 32 ++ core/templates/core/user_pictures.jinja | 44 +-- locale/fr/LC_MESSAGES/django.po | 429 ++++++++++++------------ locale/fr/LC_MESSAGES/djangojs.po | 5 +- sas/api.py | 21 +- sas/schemas.py | 3 +- sas/static/sas/css/picture.scss | 67 +--- sas/static/sas/js/picture.js | 167 +++++++++ sas/static/sas/js/relation.js | 52 --- sas/templates/sas/picture.jinja | 247 +++++++------- sas/views.py | 14 +- 12 files changed, 605 insertions(+), 480 deletions(-) create mode 100644 sas/static/sas/js/picture.js delete mode 100644 sas/static/sas/js/relation.js diff --git a/core/models.py b/core/models.py index 0100c900..a0c8c794 100644 --- a/core/models.py +++ b/core/models.py @@ -991,8 +991,8 @@ class SithFile(models.Model): return user.is_board_member if user.is_com_admin: return True - if self.is_in_sas and user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID): - return True + if self.is_in_sas: + return user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID) return user.id == self.owner_id def can_be_viewed_by(self, user): diff --git a/core/static/core/js/script.js b/core/static/core/js/script.js index 70455c17..5aea1d29 100644 --- a/core/static/core/js/script.js +++ b/core/static/core/js/script.js @@ -107,3 +107,35 @@ function update_query_string(key, value, action = History.REPLACE, url = null) { return url; } + + +/** + * Given a paginated endpoint, fetch all the items of this endpoint, + * performing multiple API calls if necessary. + * @param {string} url The paginated endpoint to fetch + * @return {Promise} + */ +async function fetch_paginated(url) { + const max_per_page = 199; + const paginated_url = new URL(url, document.location.origin); + paginated_url.searchParams.set("page_size", max_per_page.toString()); + paginated_url.searchParams.set("page", "1"); + + let first_page = (await ( await fetch(paginated_url)).json()); + let results = first_page.results; + + const nb_pictures = first_page.count + const nb_pages = Math.ceil(nb_pictures / max_per_page); + + if (nb_pages > 1) { + let promises = []; + for (let i = 2; i <= nb_pages; i++) { + paginated_url.searchParams.set("page", i.toString()); + promises.push( + fetch(paginated_url).then(res => res.json().then(json => json.results)) + ); + } + results.push(...await Promise.all(promises)) + } + return results; +} diff --git a/core/templates/core/user_pictures.jinja b/core/templates/core/user_pictures.jinja index 4c393635..62acaf56 100644 --- a/core/templates/core/user_pictures.jinja +++ b/core/templates/core/user_pictures.jinja @@ -65,17 +65,29 @@ {{ super() }} + {%- endblock -%} {% block title %} @@ -15,152 +15,157 @@ {% from "sas/macros.jinja" import print_path %} {% block content %} - - SAS / {{ print_path(picture.parent) }} {{ picture.get_display_name() }} - +
+ + SAS / {{ print_path(album) }} + -
+
-
-

{{ picture.get_display_name() }}

-

{{ picture.parent.children.filter(id__lte=picture.id).count() }} - / {{ picture.parent.children.count() }}

-
-
- - {% if not picture.is_moderated %} - {% set next = picture.get_next() %} - {% if not next %} - {% set next = url('sas:moderation') %} - {% else %} - {% set next = next.get_absolute_url() + "#pict" %} - {% endif %} - -
-
- {% if picture.asked_for_removal %} - {% trans %}Asked for removal{% endtrans %} - {% else %} -   - {% endif %} -
-
- - {% trans %}Moderate{% endtrans %} - - - {% trans %}Delete{% endtrans %} - -
+
+

+

- {% endif %} +
-
-
- -
- {{ picture.get_display_name() }} -
- -
-
-
{% trans %}Infos{% endtrans %}
+ - {% if picture.moderator %} +
+
+ +
+ +
+ +
+
+
{% trans %}Infos{% endtrans %}
+
- {% trans %}Moderator: {% endtrans %} - {{ picture.moderator.get_short_name() }} + {% trans %}Date: {% endtrans %} + +
- {% endif %} +
+ {% trans %}Owner: {% endtrans %} + +
+
-
- -
-
-
{% endblock %} {% block script %} {{ super() }} diff --git a/sas/views.py b/sas/views.py index 641301fd..c066d63d 100644 --- a/sas/views.py +++ b/sas/views.py @@ -146,17 +146,9 @@ class PictureView(CanViewMixin, DetailView): return super().get(request, *args, **kwargs) def get_context_data(self, **kwargs): - kwargs = super().get_context_data(**kwargs) - pictures_qs = Picture.objects.filter( - parent_id=self.object.parent_id - ).viewable_by(self.request.user) - kwargs["next_pict"] = ( - pictures_qs.filter(id__gt=self.object.id).order_by("id").first() - ) - kwargs["previous_pict"] = ( - pictures_qs.filter(id__lt=self.object.id).order_by("-id").first() - ) - return kwargs + return super().get_context_data(**kwargs) | { + "album": Album.objects.get(children=self.object) + } def send_album(request, album_id):