Sith/sas/views.py

220 lines
8.9 KiB
Python
Raw Normal View History

from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse_lazy, reverse
2016-10-26 17:21:19 +00:00
from django.views.generic import ListView, DetailView, RedirectView, TemplateView
2016-11-08 00:43:13 +00:00
from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin, FormView
2016-10-26 17:21:19 +00:00
from django.utils.translation import ugettext as _
from django.utils import timezone
from django.conf import settings
2016-11-25 12:47:09 +00:00
from django.forms.models import modelform_factory
2016-10-26 17:21:19 +00:00
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
from ajax_select import make_ajax_form, make_ajax_field
2016-10-26 17:21:19 +00:00
2016-11-20 12:39:04 +00:00
from io import BytesIO
from PIL import Image
2016-10-26 17:21:19 +00:00
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, TabedViewMixin
from core.views.forms import SelectUser, LoginForm, SelectDate, SelectDateTime
from core.views.files import send_file
2016-11-09 08:23:39 +00:00
from core.models import SithFile, User
2016-10-26 17:21:19 +00:00
from sas.models import Picture, Album, PeoplePictureRelation
2016-10-26 17:21:19 +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"),
required=False)
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:
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()
except Exception as e:
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:
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,
2016-11-09 08:23:39 +00:00
is_folder=False, is_moderated=automodere)
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:
self.add_error(None, _("Error uploading file %(file_name)s: %(msg)s") % {'file_name': f, 'msg': repr(e)})
class RelationForm(forms.ModelForm):
class Meta:
model = PeoplePictureRelation
fields = ['picture', 'user']
widgets = {'picture': forms.HiddenInput}
2016-11-20 22:53:41 +00:00
user = make_ajax_field(PeoplePictureRelation, 'user', 'users', label=_("Add user"))
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()
if request.user.is_authenticated() and request.user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_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()
return kwargs
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):
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)
if 'remove_user' in request.GET.keys():
try:
user = User.objects.filter(id=int(request.GET['remove_user'])).first()
if user.id == request.user.id or request.user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_ID):
r = PeoplePictureRelation.objects.filter(user=user, picture=self.object).delete()
except: pass
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.is_in_group('ae-membres'):
if self.form.is_valid():
PeoplePictureRelation(user=self.form.cleaned_data['user'],
picture=self.form.cleaned_data['picture']).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)
kwargs['form'] = self.form
return kwargs
def get_success_url(self):
return reverse('sas:picture', kwargs={'picture_id': self.object.id})
2016-10-26 17:21:19 +00:00
def send_pict(request, picture_id):
return send_file(request, picture_id, Picture)
2016-11-20 10:56:33 +00:00
def send_compressed(request, picture_id):
return send_file(request, picture_id, Picture, "compressed")
def send_thumb(request, picture_id):
return send_file(request, picture_id, Picture, "thumbnail")
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()
return super(AlbumView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
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')
2016-11-08 14:12:37 +00:00
if request.user.is_authenticated() and request.user.is_in_group('ae-membres'):
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,
automodere=request.user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_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):
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
return kwargs
2016-11-08 00:43:13 +00:00
2016-11-09 08:23:39 +00:00
# Admin views
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(settings.SITH_SAS_ADMIN_GROUP_ID):
for k,v in request.GET.items():
if k[:7] == "action_":
try:
pict = Picture.objects.filter(id=int(k[7:])).first()
if v == "delete":
pict.delete()
elif v == "moderate":
pict.is_moderated = True
pict.asked_for_removal = False
2016-11-19 14:28:21 +00:00
pict.save()
except: pass
return super(ModerationView, self).get(request, *args, **kwargs)
raise PermissionDenied
2016-11-09 08:23:39 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ModerationView, self).get_context_data(**kwargs)
kwargs['pictures'] = Picture.objects.filter(is_moderated=False, is_in_sas=True).order_by('id')
2016-11-09 08:23:39 +00:00
return kwargs
2016-11-25 12:47:09 +00:00
class PictureEditForm(forms.ModelForm):
class Meta:
model = Picture
fields=['name', 'parent']
parent = make_ajax_field(Picture, 'parent', 'files', help_text="")
class AlbumEditForm(forms.ModelForm):
class Meta:
model = Album
fields=['name', 'parent']
parent = make_ajax_field(Album, 'parent', 'files', help_text="")
class PictureEditView(UpdateView):
model=Picture
form_class=PictureEditForm
template_name='core/edit.jinja'
pk_url_kwarg = "picture_id"
class AlbumEditView(UpdateView):
model=Album
form_class=AlbumEditForm
template_name='core/edit.jinja'
pk_url_kwarg = "album_id"
2016-11-09 08:23:39 +00:00