2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
2024-09-23 08:25:27 +00:00
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
2023-04-04 16:39:45 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
2024-10-10 16:53:49 +00:00
|
|
|
from typing import Any
|
2017-04-24 15:51:12 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import PermissionDenied
|
2024-10-10 16:53:49 +00:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseRedirect
|
|
|
|
from django.shortcuts import get_object_or_404
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.views.generic import DetailView, TemplateView
|
|
|
|
from django.views.generic.edit import FormMixin, FormView, UpdateView
|
2016-10-26 17:21:19 +00:00
|
|
|
|
2024-08-01 17:02:29 +00:00
|
|
|
from core.models import SithFile, User
|
2024-06-24 11:07:36 +00:00
|
|
|
from core.views import CanEditMixin, CanViewMixin
|
2024-10-10 09:43:42 +00:00
|
|
|
from core.views.files import FileView, send_file
|
2024-10-10 16:53:49 +00:00
|
|
|
from sas.forms import (
|
|
|
|
AlbumEditForm,
|
|
|
|
PictureEditForm,
|
|
|
|
PictureModerationRequestForm,
|
|
|
|
SASForm,
|
|
|
|
)
|
2024-10-10 09:43:42 +00:00
|
|
|
from sas.models import Album, Picture
|
2016-11-19 16:19:00 +00:00
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-08 00:43:13 +00:00
|
|
|
class SASMainView(FormView):
|
|
|
|
form_class = SASForm
|
2016-10-26 17:21:19 +00:00
|
|
|
template_name = "sas/main.jinja"
|
2018-10-04 19:29:19 +00:00
|
|
|
success_url = reverse_lazy("sas:main")
|
2016-11-08 00:43:13 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.form = self.get_form()
|
|
|
|
parent = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first()
|
2018-10-04 19:29:19 +00:00
|
|
|
files = request.FILES.getlist("images")
|
2016-11-09 08:23:39 +00:00
|
|
|
root = User.objects.filter(username="root").first()
|
2019-10-05 23:34:21 +00:00
|
|
|
if request.user.is_authenticated and request.user.is_in_group(
|
2023-05-02 10:36:59 +00:00
|
|
|
pk=settings.SITH_GROUP_SAS_ADMIN_ID
|
2018-10-04 19:29:19 +00:00
|
|
|
):
|
2016-11-08 00:43:13 +00:00
|
|
|
if self.form.is_valid():
|
2018-10-04 19:29:19 +00:00
|
|
|
self.form.process(
|
|
|
|
parent=parent, owner=root, files=files, automodere=True
|
|
|
|
)
|
2016-11-09 08:23:39 +00:00
|
|
|
if self.form.is_valid():
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().form_valid(self.form)
|
2016-11-09 08:23:39 +00:00
|
|
|
else:
|
|
|
|
self.form.add_error(None, _("You do not have the permission to do that"))
|
2016-11-08 00:43:13 +00:00
|
|
|
return self.form_invalid(self.form)
|
2016-10-26 17:21:19 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
kwargs = super().get_context_data(**kwargs)
|
2024-08-08 11:15:19 +00:00
|
|
|
albums_qs = Album.objects.viewable_by(self.request.user)
|
|
|
|
kwargs["categories"] = list(
|
|
|
|
albums_qs.filter(parent_id=settings.SITH_SAS_ROOT_DIR_ID).order_by("id")
|
|
|
|
)
|
|
|
|
kwargs["latest"] = list(albums_qs.order_by("-id")[:5])
|
2016-10-26 17:21:19 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2024-08-01 17:02:29 +00:00
|
|
|
class PictureView(CanViewMixin, DetailView):
|
2016-10-26 17:21:19 +00:00
|
|
|
model = Picture
|
|
|
|
pk_url_kwarg = "picture_id"
|
|
|
|
template_name = "sas/picture.jinja"
|
|
|
|
|
2016-11-19 16:19:00 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2024-06-27 12:46:43 +00:00
|
|
|
if "rotate_right" in request.GET:
|
2016-11-20 12:39:04 +00:00
|
|
|
self.object.rotate(270)
|
2024-06-27 12:46:43 +00:00
|
|
|
if "rotate_left" in request.GET:
|
2016-11-20 12:39:04 +00:00
|
|
|
self.object.rotate(90)
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
2016-11-19 16:19:00 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-09-03 18:15:37 +00:00
|
|
|
return super().get_context_data(**kwargs) | {
|
|
|
|
"album": Album.objects.get(children=self.object)
|
|
|
|
}
|
2016-11-19 16:19:00 +00:00
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2017-10-06 15:57:35 +00:00
|
|
|
def send_album(request, album_id):
|
|
|
|
return send_file(request, album_id, Album)
|
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-10-26 17:21:19 +00:00
|
|
|
def send_pict(request, picture_id):
|
|
|
|
return send_file(request, picture_id, Picture)
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-20 10:56:33 +00:00
|
|
|
def send_compressed(request, picture_id):
|
|
|
|
return send_file(request, picture_id, Picture, "compressed")
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-20 10:56:33 +00:00
|
|
|
def send_thumb(request, picture_id):
|
|
|
|
return send_file(request, picture_id, Picture, "thumbnail")
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2017-01-05 09:05:13 +00:00
|
|
|
class AlbumUploadView(CanViewMixin, DetailView, FormMixin):
|
|
|
|
model = Album
|
|
|
|
form_class = SASForm
|
|
|
|
pk_url_kwarg = "album_id"
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2017-02-02 22:07:45 +00:00
|
|
|
if not self.object.file:
|
|
|
|
self.object.generate_thumbnail()
|
2017-01-05 09:05:13 +00:00
|
|
|
self.form = self.get_form()
|
|
|
|
parent = SithFile.objects.filter(id=self.object.id).first()
|
2018-10-04 19:29:19 +00:00
|
|
|
files = request.FILES.getlist("images")
|
2019-10-05 23:34:21 +00:00
|
|
|
if request.user.is_authenticated and request.user.is_subscribed:
|
2017-01-05 09:05:13 +00:00
|
|
|
if self.form.is_valid():
|
2018-10-04 19:29:19 +00:00
|
|
|
self.form.process(
|
|
|
|
parent=parent,
|
|
|
|
owner=request.user,
|
|
|
|
files=files,
|
2024-08-08 15:51:33 +00:00
|
|
|
automodere=(
|
|
|
|
request.user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID)
|
|
|
|
or request.user.is_root
|
2018-10-04 19:29:19 +00:00
|
|
|
),
|
|
|
|
)
|
2017-01-05 09:05:13 +00:00
|
|
|
if self.form.is_valid():
|
|
|
|
return HttpResponse(str(self.form.errors), status=200)
|
|
|
|
return HttpResponse(str(self.form.errors), status=500)
|
|
|
|
|
|
|
|
|
2016-11-08 14:12:37 +00:00
|
|
|
class AlbumView(CanViewMixin, DetailView, FormMixin):
|
2016-11-08 00:43:13 +00:00
|
|
|
model = Album
|
2016-11-08 14:12:37 +00:00
|
|
|
form_class = SASForm
|
2016-11-08 00:43:13 +00:00
|
|
|
pk_url_kwarg = "album_id"
|
|
|
|
template_name = "sas/album.jinja"
|
2019-09-15 23:26:20 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
self.asked_page = int(request.GET.get("page", 1))
|
2024-06-27 12:46:43 +00:00
|
|
|
except ValueError as e:
|
|
|
|
raise Http404 from e
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2016-11-08 00:43:13 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.form = self.get_form()
|
2018-10-04 19:29:19 +00:00
|
|
|
if "clipboard" not in request.session.keys():
|
|
|
|
request.session["clipboard"] = []
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
2016-11-08 00:43:13 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2016-12-18 18:08:25 +00:00
|
|
|
if not self.object.file:
|
|
|
|
self.object.generate_thumbnail()
|
2016-11-08 00:43:13 +00:00
|
|
|
self.form = self.get_form()
|
2018-10-04 19:29:19 +00:00
|
|
|
if "clipboard" not in request.session.keys():
|
|
|
|
request.session["clipboard"] = []
|
2017-06-12 08:02:38 +00:00
|
|
|
if request.user.can_edit(self.object): # Handle the copy-paste functions
|
2016-12-13 16:17:58 +00:00
|
|
|
FileView.handle_clipboard(request, self.object)
|
2016-11-08 14:12:37 +00:00
|
|
|
parent = SithFile.objects.filter(id=self.object.id).first()
|
2018-10-04 19:29:19 +00:00
|
|
|
files = request.FILES.getlist("images")
|
2019-10-05 23:34:21 +00:00
|
|
|
if request.user.is_authenticated and request.user.is_subscribed:
|
2016-11-08 00:43:13 +00:00
|
|
|
if self.form.is_valid():
|
2018-10-04 19:29:19 +00:00
|
|
|
self.form.process(
|
|
|
|
parent=parent,
|
|
|
|
owner=request.user,
|
|
|
|
files=files,
|
|
|
|
automodere=request.user.is_in_group(
|
2023-05-02 10:36:59 +00:00
|
|
|
pk=settings.SITH_GROUP_SAS_ADMIN_ID
|
2018-10-04 19:29:19 +00:00
|
|
|
),
|
|
|
|
)
|
2016-11-08 14:12:37 +00:00
|
|
|
if self.form.is_valid():
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().form_valid(self.form)
|
2016-11-08 14:12:37 +00:00
|
|
|
else:
|
2016-11-09 08:23:39 +00:00
|
|
|
self.form.add_error(None, _("You do not have the permission to do that"))
|
2016-11-08 00:43:13 +00:00
|
|
|
return self.form_invalid(self.form)
|
|
|
|
|
2016-11-08 14:12:37 +00:00
|
|
|
def get_success_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("sas:album", kwargs={"album_id": self.object.id})
|
2016-11-08 14:12:37 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
kwargs = super().get_context_data(**kwargs)
|
2018-10-04 19:29:19 +00:00
|
|
|
kwargs["form"] = self.form
|
|
|
|
kwargs["clipboard"] = SithFile.objects.filter(
|
|
|
|
id__in=self.request.session["clipboard"]
|
|
|
|
)
|
2024-08-08 15:51:33 +00:00
|
|
|
kwargs["children_albums"] = list(
|
|
|
|
Album.objects.viewable_by(self.request.user)
|
|
|
|
.filter(parent_id=self.object.id)
|
|
|
|
.order_by("-date")
|
|
|
|
)
|
2016-11-08 14:12:37 +00:00
|
|
|
return kwargs
|
2016-11-08 00:43:13 +00:00
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-11-09 08:23:39 +00:00
|
|
|
# Admin views
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-09 08:23:39 +00:00
|
|
|
class ModerationView(TemplateView):
|
|
|
|
template_name = "sas/moderation.jinja"
|
|
|
|
|
2016-11-19 14:28:21 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2023-05-02 10:36:59 +00:00
|
|
|
if request.user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
2016-11-19 14:28:21 +00:00
|
|
|
raise PermissionDenied
|
|
|
|
|
2016-12-09 14:46:47 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
if "album_id" not in request.POST:
|
|
|
|
raise Http404
|
2023-05-02 10:36:59 +00:00
|
|
|
if request.user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
|
2024-06-27 12:46:43 +00:00
|
|
|
album = get_object_or_404(Album, pk=request.POST["album_id"])
|
|
|
|
if "moderate" in request.POST:
|
|
|
|
album.moderator = request.user
|
|
|
|
album.is_moderated = True
|
|
|
|
album.save()
|
|
|
|
elif "delete" in request.POST:
|
|
|
|
album.delete()
|
|
|
|
return super().get(request, *args, **kwargs)
|
2016-12-09 14:46:47 +00:00
|
|
|
|
2016-11-09 08:23:39 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2024-06-27 12:46:43 +00:00
|
|
|
kwargs = super().get_context_data(**kwargs)
|
2018-10-04 19:29:19 +00:00
|
|
|
kwargs["albums_to_moderate"] = Album.objects.filter(
|
|
|
|
is_moderated=False, is_in_sas=True, is_folder=True
|
|
|
|
).order_by("id")
|
2024-09-09 19:37:28 +00:00
|
|
|
pictures = Picture.objects.filter(is_moderated=False).select_related("parent")
|
|
|
|
kwargs["pictures"] = pictures
|
|
|
|
kwargs["albums"] = [p.parent for p in pictures]
|
2016-11-09 08:23:39 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-30 08:28:22 +00:00
|
|
|
class PictureEditView(CanEditMixin, UpdateView):
|
2017-06-12 08:02:38 +00:00
|
|
|
model = Picture
|
|
|
|
form_class = PictureEditForm
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/edit.jinja"
|
2016-11-25 12:47:09 +00:00
|
|
|
pk_url_kwarg = "picture_id"
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2024-10-10 16:53:49 +00:00
|
|
|
class PictureAskRemovalView(CanViewMixin, DetailView, FormView):
|
|
|
|
"""View to allow users to ask pictures to be removed."""
|
|
|
|
|
|
|
|
model = Picture
|
|
|
|
template_name = "sas/ask_picture_removal.jinja"
|
|
|
|
pk_url_kwarg = "picture_id"
|
|
|
|
form_class = PictureModerationRequestForm
|
|
|
|
|
|
|
|
def get_form_kwargs(self) -> dict[str, Any]:
|
|
|
|
"""Add the user and picture to the form kwargs.
|
|
|
|
|
|
|
|
Those are required to create the PictureModerationRequest,
|
|
|
|
and aren't part of the form itself
|
|
|
|
(picture is a path parameter, and user is the request user).
|
|
|
|
"""
|
|
|
|
return super().get_form_kwargs() | {
|
|
|
|
"user": self.request.user,
|
|
|
|
"picture": self.object,
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_success_url(self) -> str:
|
|
|
|
"""Return the URL to the album containing the picture."""
|
|
|
|
album = Album.objects.filter(pk=self.object.parent_id).first()
|
|
|
|
if not album:
|
|
|
|
return reverse("sas:main")
|
|
|
|
return album.get_absolute_url()
|
|
|
|
|
|
|
|
def form_valid(self, form: PictureModerationRequestForm) -> HttpResponseRedirect:
|
|
|
|
form.save()
|
|
|
|
self.object.is_moderated = False
|
|
|
|
self.object.asked_for_removal = True
|
|
|
|
self.object.save()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
2016-11-30 08:28:22 +00:00
|
|
|
class AlbumEditView(CanEditMixin, UpdateView):
|
2017-06-12 08:02:38 +00:00
|
|
|
model = Album
|
|
|
|
form_class = AlbumEditForm
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "core/edit.jinja"
|
2016-11-25 12:47:09 +00:00
|
|
|
pk_url_kwarg = "album_id"
|
2016-11-09 08:23:39 +00:00
|
|
|
|
2016-12-18 18:08:25 +00:00
|
|
|
def form_valid(self, form):
|
2024-06-27 12:46:43 +00:00
|
|
|
ret = super().form_valid(form)
|
2018-10-04 19:29:19 +00:00
|
|
|
if form.cleaned_data["recursive"]:
|
2024-06-27 12:57:40 +00:00
|
|
|
self.object.apply_rights_recursively(only_folders=True)
|
2016-12-18 18:08:25 +00:00
|
|
|
return ret
|