From e46cba7a06613192ba920b20046300ebd735489c Mon Sep 17 00:00:00 2001 From: Sli Date: Tue, 18 Feb 2025 20:10:51 +0100 Subject: [PATCH] Move all user picture logic to sas --- core/tests/test_files.py | 34 ------------------- core/urls.py | 4 --- core/views/user.py | 12 +------ sas/api.py | 2 +- .../bundled/sas}/user/pictures-index.ts | 0 sas/templates/sas/album.jinja | 4 +-- .../templates/sas}/download_pictures.jinja | 0 .../templates/sas}/user_pictures.jinja | 4 +-- sas/tests/test_views.py | 34 +++++++++++++++++++ sas/urls.py | 4 +++ sas/views.py | 11 ++++++ 11 files changed, 55 insertions(+), 54 deletions(-) rename {core/static/bundled => sas/static/bundled/sas}/user/pictures-index.ts (100%) rename {core/templates/core => sas/templates/sas}/download_pictures.jinja (100%) rename {core/templates/core => sas/templates/sas}/user_pictures.jinja (90%) diff --git a/core/tests/test_files.py b/core/tests/test_files.py index 998ceab5..2f86507a 100644 --- a/core/tests/test_files.py +++ b/core/tests/test_files.py @@ -64,40 +64,6 @@ class TestImageAccess: assert not picture.is_owned_by(user) -@pytest.mark.django_db -class TestUserPicture: - def test_anonymous_user_unauthorized(self, client): - """An anonymous user shouldn't have access to an user's photo page.""" - response = client.get( - reverse( - "core:user_pictures", - kwargs={"user_id": User.objects.get(username="sli").pk}, - ) - ) - assert response.status_code == 403 - - @pytest.mark.parametrize( - ("username", "status"), - [ - ("guy", 403), - ("root", 200), - ("skia", 200), - ("sli", 200), - ], - ) - def test_page_is_working(self, client, username, status): - """Only user that subscribed (or admins) should be able to see the page.""" - # Test for simple user - client.force_login(User.objects.get(username=username)) - response = client.get( - reverse( - "core:user_pictures", - kwargs={"user_id": User.objects.get(username="sli").pk}, - ) - ) - assert response.status_code == status - - # TODO: many tests on the pages: # - renaming a page # - changing a page's parent --> check that page's children's full_name diff --git a/core/urls.py b/core/urls.py index a3b4f7d8..5a758d4e 100644 --- a/core/urls.py +++ b/core/urls.py @@ -68,7 +68,6 @@ from core.views import ( UserGodfathersView, UserListView, UserMiniView, - UserPicturesView, UserPreferencesView, UserStatsView, UserToolsView, @@ -143,9 +142,6 @@ urlpatterns = [ path("user/", UserListView.as_view(), name="user_list"), path("user//mini/", UserMiniView.as_view(), name="user_profile_mini"), path("user//", UserView.as_view(), name="user_profile"), - path( - "user//pictures/", UserPicturesView.as_view(), name="user_pictures" - ), path( "user//godfathers/", UserGodfathersView.as_view(), diff --git a/core/views/user.py b/core/views/user.py index d742a6f5..8e7b092c 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -200,7 +200,7 @@ class UserTabsMixin(TabedViewMixin): "name": _("Family"), }, { - "url": reverse("core:user_pictures", kwargs={"user_id": user.id}), + "url": reverse("sas:user_pictures", kwargs={"user_id": user.id}), "slug": "pictures", "name": _("Pictures"), }, @@ -297,16 +297,6 @@ class UserView(UserTabsMixin, CanViewMixin, DetailView): return kwargs -class UserPicturesView(UserTabsMixin, CanViewMixin, DetailView): - """Display a user's pictures.""" - - model = User - pk_url_kwarg = "user_id" - context_object_name = "profile" - template_name = "core/user_pictures.jinja" - current_tab = "pictures" - - def delete_user_godfather(request, user_id, godfather_id, is_father): user_is_admin = request.user.is_root or request.user.is_board_member if user_id != request.user.id and not user_is_admin: diff --git a/sas/api.py b/sas/api.py index 96bafb87..11355de5 100644 --- a/sas/api.py +++ b/sas/api.py @@ -104,7 +104,7 @@ class PicturesController(ControllerBase): viewed=False, type="NEW_PICTURES", defaults={ - "url": reverse("core:user_pictures", kwargs={"user_id": u.id}) + "url": reverse("sas:user_pictures", kwargs={"user_id": u.id}) }, ) diff --git a/core/static/bundled/user/pictures-index.ts b/sas/static/bundled/sas/user/pictures-index.ts similarity index 100% rename from core/static/bundled/user/pictures-index.ts rename to sas/static/bundled/sas/user/pictures-index.ts diff --git a/sas/templates/sas/album.jinja b/sas/templates/sas/album.jinja index 07c0836b..fa2592e8 100644 --- a/sas/templates/sas/album.jinja +++ b/sas/templates/sas/album.jinja @@ -1,6 +1,6 @@ {% extends "core/base.jinja" %} {% from 'core/macros.jinja' import paginate_alpine %} -{% from "core/download_pictures.jinja" import download_button %} +{% from "sas/download_pictures.jinja" import download_button %} {%- block additional_css -%} @@ -8,7 +8,7 @@ {%- block additional_js -%} - + {%- endblock -%} {% block title %} diff --git a/core/templates/core/download_pictures.jinja b/sas/templates/sas/download_pictures.jinja similarity index 100% rename from core/templates/core/download_pictures.jinja rename to sas/templates/sas/download_pictures.jinja diff --git a/core/templates/core/user_pictures.jinja b/sas/templates/sas/user_pictures.jinja similarity index 90% rename from core/templates/core/user_pictures.jinja rename to sas/templates/sas/user_pictures.jinja index c51d2f0e..1d7655b2 100644 --- a/core/templates/core/user_pictures.jinja +++ b/sas/templates/sas/user_pictures.jinja @@ -1,12 +1,12 @@ {% extends "core/base.jinja" %} -{% from "core/download_pictures.jinja" import download_button %} +{% from "sas/download_pictures.jinja" import download_button %} {%- block additional_css -%} {%- endblock -%} {% block additional_js %} - + {% endblock %} {% block title %} diff --git a/sas/tests/test_views.py b/sas/tests/test_views.py index ff8dd21d..a689c326 100644 --- a/sas/tests/test_views.py +++ b/sas/tests/test_views.py @@ -171,3 +171,37 @@ class TestSasModeration(TestCase): "Vous avez déjà déposé une demande de retrait pour cette photo.", res.content.decode(), ) + + +@pytest.mark.django_db +class TestUserPicture: + def test_anonymous_user_unauthorized(self, client): + """An anonymous user shouldn't have access to an user's photo page.""" + response = client.get( + reverse( + "sas:user_pictures", + kwargs={"user_id": User.objects.get(username="sli").pk}, + ) + ) + assert response.status_code == 403 + + @pytest.mark.parametrize( + ("username", "status"), + [ + ("guy", 403), + ("root", 200), + ("skia", 200), + ("sli", 200), + ], + ) + def test_page_is_working(self, client, username, status): + """Only user that subscribed (or admins) should be able to see the page.""" + # Test for simple user + client.force_login(User.objects.get(username=username)) + response = client.get( + reverse( + "sas:user_pictures", + kwargs={"user_id": User.objects.get(username="sli").pk}, + ) + ) + assert response.status_code == status diff --git a/sas/urls.py b/sas/urls.py index 01bc2384..5fb57ccf 100644 --- a/sas/urls.py +++ b/sas/urls.py @@ -24,6 +24,7 @@ from sas.views import ( PictureEditView, PictureView, SASMainView, + UserPicturesView, send_album, send_compressed, send_pict, @@ -55,4 +56,7 @@ urlpatterns = [ name="download_compressed", ), path("picture//download/thumb/", send_thumb, name="download_thumb"), + path( + "user//pictures/", UserPicturesView.as_view(), name="user_pictures" + ), ] diff --git a/sas/views.py b/sas/views.py index 7c8c8ea2..7d7740cb 100644 --- a/sas/views.py +++ b/sas/views.py @@ -26,6 +26,7 @@ from django.views.generic.edit import FormMixin, FormView, UpdateView from core.auth.mixins import CanEditMixin, CanViewMixin from core.models import SithFile, User from core.views.files import FileView, send_file +from core.views.user import UserTabsMixin from sas.forms import ( AlbumEditForm, PictureEditForm, @@ -193,6 +194,16 @@ class AlbumView(CanViewMixin, DetailView, FormMixin): return kwargs +class UserPicturesView(UserTabsMixin, CanViewMixin, DetailView): + """Display a user's pictures.""" + + model = User + pk_url_kwarg = "user_id" + context_object_name = "profile" + template_name = "sas/user_pictures.jinja" + current_tab = "pictures" + + # Admin views