from django.shortcuts import render, redirect from django.core.urlresolvers import reverse_lazy, reverse from django.views.generic import ListView, DetailView, RedirectView, TemplateView from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin, FormView from django.utils.translation import ugettext as _ from django.utils import timezone from django.conf import settings from django import forms from django.core.exceptions import PermissionDenied from ajax_select import make_ajax_form, make_ajax_field from io import BytesIO from PIL import Image 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 from core.models import SithFile, User from sas.models import Picture, Album, PeoplePictureRelation from core.utils import resize_image, exif_auto_rotate 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) def process(self, parent, owner, files, automodere=False): try: if self.cleaned_data['album_name'] != "": album = Album(parent=parent, name=self.cleaned_data['album_name'], owner=owner, is_moderated=automodere) 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': str(e.message)}) for f in files: new_file = Picture(parent=parent, name=f.name, file=f, owner=owner, mime_type=f.content_type, size=f._size, is_folder=False, is_moderated=automodere) try: new_file.clean() im = Image.open(BytesIO(f.read())) try: im = exif_auto_rotate(im) except: pass file = resize_image(im, max(im.size), f.content_type.split('/')[-1]) thumb = resize_image(im, 200, f.content_type.split('/')[-1]) compressed = resize_image(im, 600, f.content_type.split('/')[-1]) new_file.file = file new_file.file.name = new_file.name new_file.thumbnail = thumb new_file.thumbnail.name = new_file.name new_file.compressed = compressed new_file.compressed.name = new_file.name 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} user = make_ajax_field(PeoplePictureRelation, 'user', 'users', help_text="") class SASMainView(FormView): form_class = SASForm template_name = "sas/main.jinja" success_url = reverse_lazy('sas:main') 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') root = User.objects.filter(username="root").first() if request.user.is_authenticated() and request.user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_ID): if self.form.is_valid(): 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")) return self.form_invalid(self.form) 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): model = Picture form_class = RelationForm 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() 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 im = Image.open(BytesIO(self.object.file.read())) (w, h) = im.size kwargs['is_vertical'] = (w / h) < 1 return kwargs def get_success_url(self): return reverse('sas:picture', kwargs={'picture_id': self.object.id}) def send_pict(request, picture_id): return send_file(request, picture_id, Picture) 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") class AlbumView(CanViewMixin, DetailView, FormMixin): model = Album form_class = SASForm 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() parent = SithFile.objects.filter(id=self.object.id).first() files = request.FILES.getlist('images') if request.user.is_authenticated() and request.user.is_in_group('ae-membres'): if self.form.is_valid(): self.form.process(parent=parent, owner=request.user, files=files) if self.form.is_valid(): return super(AlbumView, 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_success_url(self): return reverse('sas:album', kwargs={'album_id': self.object.id}) def get_context_data(self, **kwargs): kwargs = super(AlbumView, self).get_context_data(**kwargs) kwargs['form'] = self.form return kwargs # Admin views class ModerationView(TemplateView): template_name = "sas/moderation.jinja" 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 pict.save() except: pass return super(ModerationView, self).get(request, *args, **kwargs) raise PermissionDenied def get_context_data(self, **kwargs): kwargs = super(ModerationView, self).get_context_data(**kwargs) kwargs['pictures'] = [p for p in Picture.objects.filter(is_moderated=False).order_by('id') if p.is_in_sas] return kwargs