Sith/sas/views.py

381 lines
14 KiB
Python
Raw Permalink Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith3
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2017-06-12 08:02:38 +00:00
from ajax_select import make_ajax_field
from ajax_select.fields import AutoCompleteSelectMultipleField
2024-06-24 11:07:36 +00:00
from django import forms
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404, HttpResponse
2024-06-27 12:46:43 +00:00
from django.shortcuts import get_object_or_404, redirect
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-06-24 11:07:36 +00:00
from core.models import Notification, SithFile, User
from core.views import CanEditMixin, CanViewMixin
from core.views.files import FileView, MultipleImageField, send_file
from core.views.forms import SelectDate
from sas.models import Album, PeoplePictureRelation, Picture
2016-10-26 17:21:19 +00:00
2017-06-12 08:02:38 +00:00
2016-11-08 00:43:13 +00:00
class SASForm(forms.Form):
2018-10-04 19:29:19 +00:00
album_name = forms.CharField(
2024-09-08 11:29:33 +00:00
label=_("Add a new album"), max_length=Album.NAME_MAX_LENGTH, required=False
2018-10-04 19:29:19 +00:00
)
2024-06-22 19:16:42 +00:00
images = MultipleImageField(
2018-10-04 19:29:19 +00:00
label=_("Upload images"),
required=False,
)
2016-11-08 00:43:13 +00:00
2024-06-27 12:57:40 +00:00
def process(self, parent, owner, files, *, automodere=False):
2016-11-08 00:43:13 +00:00
try:
2018-10-04 19:29:19 +00:00
if self.cleaned_data["album_name"] != "":
album = Album(
parent=parent,
name=self.cleaned_data["album_name"],
owner=owner,
is_moderated=automodere,
)
2016-11-08 00:43:13 +00:00
album.clean()
album.save()
except Exception as e:
2018-10-04 19:29:19 +00:00
self.add_error(
None,
_("Error creating album %(album)s: %(msg)s")
% {"album": self.cleaned_data["album_name"], "msg": repr(e)},
)
2016-11-08 00:43:13 +00:00
for f in files:
2018-10-04 19:29:19 +00:00
new_file = Picture(
parent=parent,
name=f.name,
file=f,
owner=owner,
mime_type=f.content_type,
size=f.size,
2018-10-04 19:29:19 +00:00
is_folder=False,
is_moderated=automodere,
)
2016-12-15 12:10:05 +00:00
if automodere:
new_file.moderator = owner
2016-11-08 00:43:13 +00:00
try:
new_file.clean()
2016-11-20 23:02:40 +00:00
new_file.generate_thumbnails()
2016-11-08 00:43:13 +00:00
new_file.save()
except Exception as e:
2018-10-04 19:29:19 +00:00
self.add_error(
None,
_("Error uploading file %(file_name)s: %(msg)s")
% {"file_name": f, "msg": repr(e)},
)
2016-11-08 00:43:13 +00:00
2017-06-12 08:02:38 +00:00
class RelationForm(forms.ModelForm):
class Meta:
model = PeoplePictureRelation
2018-10-04 19:29:19 +00:00
fields = ["picture"]
widgets = {"picture": forms.HiddenInput}
users = AutoCompleteSelectMultipleField(
"users", show_help_text=False, help_text="", label=_("Add user"), required=False
)
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()
if request.user.is_authenticated and request.user.is_in_group(
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)
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
class PictureView(CanViewMixin, DetailView, FormMixin):
2016-10-26 17:21:19 +00:00
model = Picture
form_class = RelationForm
2016-10-26 17:21:19 +00:00
pk_url_kwarg = "picture_id"
template_name = "sas/picture.jinja"
def get_initial(self):
2018-10-04 19:29:19 +00:00
return {"picture": self.object}
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
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)
2018-10-04 19:29:19 +00:00
if "ask_removal" in request.GET.keys():
self.object.is_moderated = False
self.object.asked_for_removal = True
self.object.save()
return redirect("sas:album", album_id=self.object.parent.id)
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
if request.user.is_authenticated and request.user.was_subscribed:
if self.form.is_valid():
2018-10-04 19:29:19 +00:00
for uid in self.form.cleaned_data["users"]:
2016-12-06 12:53:16 +00:00
u = User.objects.filter(id=uid).first()
if not u: # Don't use a non existing user
continue
if PeoplePictureRelation.objects.filter(
user=u, picture=self.form.cleaned_data["picture"]
).exists(): # Avoid existing relation
continue
2018-10-04 19:29:19 +00:00
PeoplePictureRelation(
user=u, picture=self.form.cleaned_data["picture"]
).save()
if not u.notifications.filter(
type="NEW_PICTURES", viewed=False
).exists():
Notification(
user=u,
url=reverse("core:user_pictures", kwargs={"user_id": u.id}),
type="NEW_PICTURES",
).save()
2024-06-27 12:46:43 +00:00
return super().form_valid(self.form)
else:
self.form.add_error(None, _("You do not have the permission to do that"))
return self.form_invalid(self.form)
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2024-08-09 15:34:35 +00:00
pictures_qs = Picture.objects.filter(
parent_id=self.object.parent_id
).viewable_by(self.request.user)
2018-10-04 19:29:19 +00:00
kwargs["form"] = self.form
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
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse("sas:picture", kwargs={"picture_id": self.object.id})
2017-06-12 08:02:38 +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()
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")
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()
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")
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(
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):
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
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")
kwargs["pictures"] = Picture.objects.filter(is_moderated=False)
2018-10-04 19:29:19 +00:00
kwargs["albums"] = Album.objects.filter(
id__in=kwargs["pictures"].values("parent").distinct("parent")
)
2016-11-09 08:23:39 +00:00
return kwargs
2017-06-12 08:02:38 +00:00
2016-11-25 12:47:09 +00:00
class PictureEditForm(forms.ModelForm):
class Meta:
model = Picture
2018-10-04 19:29:19 +00:00
fields = ["name", "parent"]
parent = make_ajax_field(Picture, "parent", "files", help_text="")
2016-11-25 12:47:09 +00:00
2017-06-12 08:02:38 +00:00
2016-11-25 12:47:09 +00:00
class AlbumEditForm(forms.ModelForm):
class Meta:
model = Album
2018-10-04 19:29:19 +00:00
fields = ["name", "date", "file", "parent", "edit_groups"]
2024-09-08 11:29:33 +00:00
name = forms.CharField(max_length=Album.NAME_MAX_LENGTH, label=_("file name"))
2017-02-21 13:29:58 +00:00
date = forms.DateField(label=_("Date"), widget=SelectDate, required=True)
2018-10-04 19:29:19 +00:00
parent = make_ajax_field(Album, "parent", "files", help_text="")
edit_groups = make_ajax_field(Album, "edit_groups", "groups", help_text="")
recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False)
2016-11-25 12:47:09 +00:00
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
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
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)
return ret