From 47a332445c42a74c9ebc22c44768de7b94a8e822 Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 7 Aug 2022 16:08:56 +0200 Subject: [PATCH] Add feature to download all of your pictures as a user --- api/urls.py | 3 +- api/views/__init__.py | 1 + api/views/sas.py | 42 ++++++++++++++++ core/templates/core/user_pictures.jinja | 65 +++++++++++++++++++++++++ core/views/files.py | 2 + core/views/user.py | 13 ++--- 6 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 api/views/sas.py diff --git a/api/urls.py b/api/urls.py index c8bdc2b2..14ed5172 100644 --- a/api/urls.py +++ b/api/urls.py @@ -22,7 +22,7 @@ # # -from django.urls import re_path, include +from django.urls import re_path, path, include from api.views import * from rest_framework import routers @@ -54,4 +54,5 @@ urlpatterns = [ re_path(r"^markdown$", RenderMarkdown, name="api_markdown"), re_path(r"^mailings$", FetchMailingLists, name="mailings_fetch"), re_path(r"^uv$", uv_endpoint, name="uv_endpoint"), + path("sas/", all_pictures_of_user_endpoint, name="all_pictures_of_user"), ] diff --git a/api/views/__init__.py b/api/views/__init__.py index 6a3fa8e9..96ee04d1 100644 --- a/api/views/__init__.py +++ b/api/views/__init__.py @@ -78,3 +78,4 @@ from .club import * from .group import * from .launderette import * from .uv import * +from .sas import * diff --git a/api/views/sas.py b/api/views/sas.py new file mode 100644 index 00000000..277ed390 --- /dev/null +++ b/api/views/sas.py @@ -0,0 +1,42 @@ +from typing import List +from rest_framework.decorators import api_view, renderer_classes +from rest_framework.exceptions import PermissionDenied +from rest_framework.generics import get_object_or_404 +from rest_framework.renderers import JSONRenderer +from rest_framework.request import Request +from rest_framework.response import Response + +from core.views import can_edit_prop +from core.models import User +from sas.models import Picture + + +def all_pictures_of_user(user: User) -> List[Picture]: + return [ + relation.picture + for relation in user.pictures.exclude(picture=None) + .order_by("-picture__parent__date", "id") + .select_related("picture__parent") + ] + + +@api_view(["GET"]) +@renderer_classes((JSONRenderer,)) +def all_pictures_of_user_endpoint(request: Request, user: int): + requested_user: User = get_object_or_404(User, pk=user) + if not can_edit_prop(requested_user, request.user): + raise PermissionDenied + + return Response( + [ + { + "name": f"{picture.parent.name} - {picture.name}", + "date": picture.date, + "author": str(picture.owner), + "full_size_url": picture.get_download_url(), + "compressed_url": picture.get_download_compressed_url(), + "thumb_url": picture.get_download_thumb_url(), + } + for picture in all_pictures_of_user(requested_user) + ] + ) diff --git a/core/templates/core/user_pictures.jinja b/core/templates/core/user_pictures.jinja index faaa7b3f..06f90781 100644 --- a/core/templates/core/user_pictures.jinja +++ b/core/templates/core/user_pictures.jinja @@ -6,6 +6,7 @@ {% block content %} {% for a in albums %} +

{{ a.name }}


@@ -20,3 +21,67 @@ {% endfor %} {% endblock %} +{% block script %} + {{ super() }} + +{% endblock %} + diff --git a/core/views/files.py b/core/views/files.py index 09f117a7..dd288102 100644 --- a/core/views/files.py +++ b/core/views/files.py @@ -24,6 +24,7 @@ # This file contains all the views that concern the page model from django.shortcuts import redirect, get_object_or_404 +from django.utils.http import http_date from django.views.generic import ListView, DetailView, TemplateView from django.views.generic.edit import UpdateView, FormMixin, DeleteView from django.views.generic.detail import SingleObjectMixin @@ -68,6 +69,7 @@ def send_file(request, file_id, file_class=SithFile, file_attr="file"): with open(filepath.encode("utf-8"), "rb") as filename: wrapper = FileWrapper(filename) response = HttpResponse(wrapper, content_type=f.mime_type) + response["Last-Modified"] = http_date(f.date.timestamp()) response["Content-Length"] = os.path.getsize(filepath.encode("utf-8")) response["Content-Disposition"] = ('inline; filename="%s"' % f.name).encode( "utf-8" diff --git a/core/views/user.py b/core/views/user.py index ffd867ad..6737bf7d 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -48,6 +48,7 @@ from django.views.generic.dates import YearMixin, MonthMixin from datetime import timedelta, date import logging +from api.views.sas import all_pictures_of_user from core.views import ( CanViewMixin, @@ -325,19 +326,15 @@ class UserPicturesView(UserTabsMixin, CanViewMixin, DetailView): kwargs = super(UserPicturesView, self).get_context_data(**kwargs) kwargs["albums"] = [] kwargs["pictures"] = {} - picture_qs = ( - self.object.pictures.exclude(picture=None) - .order_by("-picture__parent__date", "id") - .select_related("picture__parent") - ) + picture_qs = all_pictures_of_user(self.object) last_album = None - for pict_relation in picture_qs: - album = pict_relation.picture.parent + for picture in picture_qs: + album = picture.parent if album.id != last_album: kwargs["albums"].append(album) kwargs["pictures"][album.id] = [] last_album = album.id - kwargs["pictures"][album.id].append(pict_relation.picture) + kwargs["pictures"][album.id].append(picture) return kwargs