2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.http import HttpResponse
|
2016-11-19 16:19:00 +00:00
|
|
|
from django.core.urlresolvers 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
|
2017-03-27 12:31:58 +00:00
|
|
|
from django.utils.translation import ugettext_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
|
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
|
|
|
|
2016-11-19 16:19:00 +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):
|
|
|
|
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"),
|
2017-06-12 08:02:38 +00:00
|
|
|
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-12-08 18:47:28 +00:00
|
|
|
notif = False
|
2016-11-08 00:43:13 +00:00
|
|
|
try:
|
|
|
|
if self.cleaned_data['album_name'] != "":
|
2016-11-09 08:23:39 +00:00
|
|
|
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()
|
2017-01-05 09:05:13 +00:00
|
|
|
notif = not automodere
|
2016-11-08 00:43:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.add_error(None, _("Error creating album %(album)s: %(msg)s") %
|
2017-06-12 08:02:38 +00:00
|
|
|
{'album': self.cleaned_data['album_name'], 'msg': repr(e)})
|
2016-11-08 00:43:13 +00:00
|
|
|
for f in files:
|
2016-11-08 14:12:37 +00:00
|
|
|
new_file = Picture(parent=parent, name=f.name, file=f, owner=owner, mime_type=f.content_type, size=f._size,
|
2017-06-12 08:02:38 +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()
|
2017-01-05 09:05:13 +00:00
|
|
|
notif = not automodere
|
2016-11-08 00:43:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.add_error(None, _("Error uploading file %(file_name)s: %(msg)s") % {'file_name': f, 'msg': repr(e)})
|
2016-12-08 18:47:28 +00:00
|
|
|
if notif:
|
2016-12-10 00:29:56 +00:00
|
|
|
for u in RealGroup.objects.filter(id=settings.SITH_GROUP_SAS_ADMIN_ID).first().users.all():
|
2016-12-14 08:11:26 +00:00
|
|
|
if not u.notifications.filter(type="SAS_MODERATION", viewed=False).exists():
|
2016-12-09 23:06:17 +00:00
|
|
|
Notification(user=u, url=reverse("sas:moderation"), type="SAS_MODERATION").save()
|
2016-11-08 00:43:13 +00:00
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-19 16:19:00 +00:00
|
|
|
class RelationForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = PeoplePictureRelation
|
2016-12-06 12:53:16 +00:00
|
|
|
fields = ['picture']
|
2016-11-19 16:19:00 +00:00
|
|
|
widgets = {'picture': forms.HiddenInput}
|
2016-12-06 12:53:16 +00:00
|
|
|
users = AutoCompleteSelectMultipleField('users', show_help_text=False, help_text="", label=_("Add user"), required=False)
|
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"
|
2016-11-08 14:12:37 +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()
|
|
|
|
files = request.FILES.getlist('images')
|
2016-11-09 08:23:39 +00:00
|
|
|
root = User.objects.filter(username="root").first()
|
2016-12-10 00:29:56 +00:00
|
|
|
if request.user.is_authenticated() and request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID):
|
2016-11-08 00:43:13 +00:00
|
|
|
if self.form.is_valid():
|
2016-11-09 08:23:39 +00:00
|
|
|
self.form.process(parent=parent, owner=root, files=files, automodere=True)
|
|
|
|
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)
|
|
|
|
kwargs['root_file'] = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first()
|
2017-01-10 22:32:17 +00:00
|
|
|
kwargs['latest'] = SithFile.objects.filter(is_in_sas=True, is_folder=True, 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
|
|
|
|
2016-11-19 16:19:00 +00:00
|
|
|
class PictureView(CanViewMixin, DetailView, FormMixin):
|
2016-10-26 17:21:19 +00:00
|
|
|
model = Picture
|
2016-11-19 16:19:00 +00:00
|
|
|
form_class = RelationForm
|
2016-10-26 17:21:19 +00:00
|
|
|
pk_url_kwarg = "picture_id"
|
|
|
|
template_name = "sas/picture.jinja"
|
|
|
|
|
2016-11-19 16:19:00 +00:00
|
|
|
def get_initial(self):
|
|
|
|
return {'picture': self.object}
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
self.form = self.get_form()
|
2016-11-20 12:39:04 +00:00
|
|
|
if 'rotate_right' in request.GET.keys():
|
|
|
|
self.object.rotate(270)
|
|
|
|
if 'rotate_left' in request.GET.keys():
|
|
|
|
self.object.rotate(90)
|
2016-11-19 16:19:00 +00:00
|
|
|
if 'remove_user' in request.GET.keys():
|
|
|
|
try:
|
|
|
|
user = User.objects.filter(id=int(request.GET['remove_user'])).first()
|
2016-12-10 00:29:56 +00:00
|
|
|
if user.id == request.user.id or request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID):
|
2017-06-13 08:48:04 +00:00
|
|
|
PeoplePictureRelation.objects.filter(user=user, picture=self.object).delete()
|
2017-06-12 08:02:38 +00:00
|
|
|
except:
|
|
|
|
pass
|
2016-11-19 16:19:00 +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()
|
2017-07-26 14:55:00 +00:00
|
|
|
if request.user.is_authenticated() and request.user.was_subscribed:
|
2016-11-19 16:19:00 +00:00
|
|
|
if self.form.is_valid():
|
2016-12-06 12:53:16 +00:00
|
|
|
for uid in self.form.cleaned_data['users']:
|
|
|
|
u = User.objects.filter(id=uid).first()
|
|
|
|
PeoplePictureRelation(user=u,
|
2017-06-12 08:02:38 +00:00
|
|
|
picture=self.form.cleaned_data['picture']).save()
|
2016-12-14 08:11:26 +00:00
|
|
|
if not u.notifications.filter(type="NEW_PICTURES", viewed=False).exists():
|
2016-12-09 23:06:17 +00:00
|
|
|
Notification(user=u, url=reverse("core:user_pictures", kwargs={'user_id': u.id}), type="NEW_PICTURES").save()
|
2016-11-19 16:19:00 +00:00
|
|
|
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)
|
|
|
|
kwargs['form'] = self.form
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('sas:picture', kwargs={'picture_id': self.object.id})
|
|
|
|
|
2017-06-12 08:02:38 +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()
|
|
|
|
files = request.FILES.getlist('images')
|
2017-02-24 01:59:59 +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():
|
|
|
|
self.form.process(parent=parent, owner=request.user, files=files,
|
2017-06-12 08:02:38 +00:00
|
|
|
automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID))
|
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"
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.form = self.get_form()
|
2016-12-13 16:17:58 +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()
|
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()
|
2016-12-13 16:17:58 +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()
|
2016-11-08 00:43:13 +00:00
|
|
|
files = request.FILES.getlist('images')
|
2017-02-24 01:59:59 +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():
|
2016-11-20 22:53:41 +00:00
|
|
|
self.form.process(parent=parent, owner=request.user, files=files,
|
2017-06-12 08:02:38 +00:00
|
|
|
automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID))
|
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):
|
2016-11-19 16:19:00 +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)
|
|
|
|
kwargs['form'] = self.form
|
2016-12-13 16:17:58 +00:00
|
|
|
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
|
|
|
|
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):
|
2016-12-10 00:29:56 +00:00
|
|
|
if request.user.is_in_group(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):
|
2016-12-15 12:10:05 +00:00
|
|
|
if request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID):
|
|
|
|
try:
|
|
|
|
a = Album.objects.filter(id=request.POST['album_id']).first()
|
|
|
|
if 'moderate' in request.POST.keys():
|
|
|
|
a.moderator = request.user
|
|
|
|
a.is_moderated = True
|
|
|
|
a.save()
|
|
|
|
elif 'delete' in request.POST.keys():
|
|
|
|
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)
|
2016-12-15 12:10:05 +00:00
|
|
|
kwargs['albums_to_moderate'] = Album.objects.filter(is_moderated=False, is_in_sas=True,
|
2017-06-12 08:02:38 +00:00
|
|
|
is_folder=True).order_by('id')
|
2016-12-15 12:10:05 +00:00
|
|
|
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
|
2017-06-12 08:02:38 +00:00
|
|
|
fields = ['name', 'parent']
|
2016-11-25 12:47:09 +00:00
|
|
|
parent = make_ajax_field(Picture, 'parent', 'files', help_text="")
|
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-11-25 12:47:09 +00:00
|
|
|
class AlbumEditForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Album
|
2017-02-21 13:29:58 +00:00
|
|
|
fields = ['name', 'date', 'file', 'parent', 'edit_groups']
|
|
|
|
date = forms.DateField(label=_("Date"), widget=SelectDate, required=True)
|
2016-11-25 12:47:09 +00:00
|
|
|
parent = make_ajax_field(Album, 'parent', 'files', help_text="")
|
2016-12-15 12:10:05 +00:00
|
|
|
edit_groups = make_ajax_field(Album, 'edit_groups', 'groups', help_text="")
|
2016-12-18 18:08:25 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
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):
|
|
|
|
ret = super(AlbumEditView, self).form_valid(form)
|
|
|
|
if form.cleaned_data['recursive']:
|
|
|
|
self.object.apply_rights_recursively(True)
|
|
|
|
return ret
|