Sith/sas/views.py

391 lines
14 KiB
Python
Raw Permalink Normal View History

# -*- coding:utf-8 -*
#
# 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 django.shortcuts import redirect
2019-09-15 23:26:20 +00:00
from django.http import HttpResponse, Http404
from django.urls import reverse_lazy, reverse
2017-06-13 08:48:04 +00:00
from core.views.forms import SelectDate
2017-06-12 08:02:38 +00:00
from django.views.generic import DetailView, TemplateView
from django.views.generic.edit import UpdateView, FormMixin, FormView
from django.utils.translation import gettext_lazy as _
2016-10-26 17:21:19 +00:00
from django.conf import settings
from django import forms
2016-11-19 14:28:21 +00:00
from django.core.exceptions import PermissionDenied
2019-09-15 23:26:20 +00:00
from django.core.paginator import Paginator, InvalidPage
2016-10-26 17:21:19 +00:00
2017-06-12 08:02:38 +00:00
from ajax_select import make_ajax_field
from ajax_select.fields import AutoCompleteSelectMultipleField
2016-10-26 17:21:19 +00:00
2017-06-12 08:02:38 +00:00
from core.views import CanViewMixin, CanEditMixin
2016-12-13 16:17:58 +00:00
from core.views.files import send_file, FileView
2016-12-08 18:47:28 +00:00
from core.models import SithFile, User, Notification, RealGroup
2016-10-26 17:21:19 +00:00
from sas.models import Picture, Album, PeoplePictureRelation
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(
label=_("Add a new album"), max_length=30, required=False
)
images = forms.ImageField(
widget=forms.ClearableFileInput(attrs={"multiple": True}),
label=_("Upload images"),
required=False,
)
2016-11-08 00:43:13 +00:00
2016-11-09 08:23:39 +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(
2023-05-10 09:56:33 +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():
return super(SASMainView, self).form_valid(self.form)
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):
kwargs = super(SASMainView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["categories"] = Album.objects.filter(
parent__id=settings.SITH_SAS_ROOT_DIR_ID
).order_by("id")
kwargs["latest"] = Album.objects.filter(is_moderated=True).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()
2018-10-04 19:29:19 +00:00
if "rotate_right" in request.GET.keys():
2016-11-20 12:39:04 +00:00
self.object.rotate(270)
2018-10-04 19:29:19 +00:00
if "rotate_left" in request.GET.keys():
2016-11-20 12:39:04 +00:00
self.object.rotate(90)
2018-10-04 19:29:19 +00:00
if "remove_user" in request.GET.keys():
try:
2018-10-04 19:29:19 +00:00
user = User.objects.filter(id=int(request.GET["remove_user"])).first()
if user.id == request.user.id or request.user.is_in_group(
2023-05-10 09:56:33 +00:00
pk=settings.SITH_GROUP_SAS_ADMIN_ID
2018-10-04 19:29:19 +00:00
):
PeoplePictureRelation.objects.filter(
user=user, picture=self.object
).delete()
2017-06-12 08:02:38 +00:00
except:
pass
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)
return super(PictureView, self).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()
return super(PictureView, self).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):
kwargs = super(PictureView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["form"] = self.form
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,
automodere=request.user.is_in_group(
2023-05-10 09:56:33 +00:00
pk=settings.SITH_GROUP_SAS_ADMIN_ID
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
paginate_by = settings.SITH_SAS_IMAGES_PER_PAGE
def dispatch(self, request, *args, **kwargs):
try:
self.asked_page = int(request.GET.get("page", 1))
except ValueError:
raise Http404
return super(AlbumView, self).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"] = []
2016-11-08 00:43:13 +00:00
return super(AlbumView, self).get(request, *args, **kwargs)
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(
2023-05-10 09:56:33 +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():
return super(AlbumView, self).form_valid(self.form)
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):
kwargs = super(AlbumView, self).get_context_data(**kwargs)
2019-09-15 23:26:20 +00:00
kwargs["paginator"] = Paginator(
self.object.children_pictures.order_by("id"), self.paginate_by
)
try:
kwargs["pictures"] = kwargs["paginator"].page(self.asked_page)
except InvalidPage:
raise Http404
2018-10-04 19:29:19 +00:00
kwargs["form"] = self.form
kwargs["clipboard"] = SithFile.objects.filter(
id__in=self.request.session["clipboard"]
)
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-10 09:56:33 +00:00
if request.user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
2016-11-19 14:28:21 +00:00
return super(ModerationView, self).get(request, *args, **kwargs)
raise PermissionDenied
2016-12-09 14:46:47 +00:00
def post(self, request, *args, **kwargs):
2023-05-10 09:56:33 +00:00
if request.user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
2016-12-15 12:10:05 +00:00
try:
2018-10-04 19:29:19 +00:00
a = Album.objects.filter(id=request.POST["album_id"]).first()
if "moderate" in request.POST.keys():
2016-12-15 12:10:05 +00:00
a.moderator = request.user
a.is_moderated = True
a.save()
2018-10-04 19:29:19 +00:00
elif "delete" in request.POST.keys():
2016-12-15 12:10:05 +00:00
a.delete()
2017-06-12 08:02:38 +00:00
except:
pass
2016-12-15 12:10:05 +00:00
return super(ModerationView, self).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):
kwargs = super(ModerationView, self).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, is_in_sas=True, is_folder=False
)
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"]
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):
ret = super(AlbumEditView, self).form_valid(form)
2018-10-04 19:29:19 +00:00
if form.cleaned_data["recursive"]:
self.object.apply_rights_recursively(True)
return ret