From e2311dcb713f18d747371ba1b067aab9132ca5b3 Mon Sep 17 00:00:00 2001 From: Krophil Date: Mon, 12 Jun 2017 10:02:38 +0200 Subject: [PATCH] Format sas --- sas/admin.py | 2 -- sas/models.py | 22 ++++++++-------- sas/urls.py | 3 +-- sas/views.py | 70 ++++++++++++++++++++++++++++----------------------- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/sas/admin.py b/sas/admin.py index f504eec4..add09607 100644 --- a/sas/admin.py +++ b/sas/admin.py @@ -30,5 +30,3 @@ from sas.models import * admin.site.register(Album) # admin.site.register(Picture) admin.site.register(PeoplePictureRelation) - - diff --git a/sas/models.py b/sas/models.py index 10412b9e..b88d599d 100644 --- a/sas/models.py +++ b/sas/models.py @@ -23,11 +23,9 @@ # from django.db import models -from django.core.urlresolvers import reverse_lazy, reverse -from django.conf import settings from django.core.urlresolvers import reverse +from django.conf import settings from django.utils.translation import ugettext_lazy as _ -from django.core.files.base import ContentFile from PIL import Image from io import BytesIO @@ -36,6 +34,7 @@ import os from core.models import SithFile, User from core.utils import resize_image, exif_auto_rotate + class Picture(SithFile): class Meta: proxy = True @@ -50,12 +49,12 @@ class Picture(SithFile): def can_be_edited_by(self, user): # file = SithFile.objects.filter(id=self.id).first() - return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)# or user.can_edit(file) + return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID) # or user.can_edit(file) def can_be_viewed_by(self, user): # file = SithFile.objects.filter(id=self.id).first() return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and - user.was_subscribed)# or user.can_view(file) + user.was_subscribed) # or user.can_view(file) def get_download_url(self): return reverse('sas:download', kwargs={'picture_id': self.id}) @@ -73,7 +72,8 @@ class Picture(SithFile): im = Image.open(BytesIO(self.file.read())) try: im = exif_auto_rotate(im) - except: pass + except: + pass file = resize_image(im, max(im.size), self.mime_type.split('/')[-1]) thumb = resize_image(im, 200, self.mime_type.split('/')[-1]) compressed = resize_image(im, 1200, self.mime_type.split('/')[-1]) @@ -102,17 +102,18 @@ class Picture(SithFile): def get_next(self): if self.is_moderated: return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False, - id__gt=self.id).order_by('id').first() + id__gt=self.id).order_by('id').first() else: return Picture.objects.filter(id__gt=self.id, is_moderated=False, is_in_sas=True).order_by('id').first() def get_previous(self): if self.is_moderated: return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False, - id__lt=self.id).order_by('id').last() + id__lt=self.id).order_by('id').last() else: return Picture.objects.filter(id__lt=self.id, is_moderated=False, is_in_sas=True).order_by('-id').first() + class Album(SithFile): class Meta: proxy = True @@ -127,12 +128,12 @@ class Album(SithFile): def can_be_edited_by(self, user): # file = SithFile.objects.filter(id=self.id).first() - return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)# or user.can_edit(file) + return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID) # or user.can_edit(file) def can_be_viewed_by(self, user): # file = SithFile.objects.filter(id=self.id).first() return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and - user.was_subscribed)# or user.can_view(file) + user.was_subscribed) # or user.can_view(file) def get_absolute_url(self): return reverse('sas:album', kwargs={'album_id': self.id}) @@ -148,6 +149,7 @@ class Album(SithFile): self.file.name = self.name + '/thumb.jpg' self.save() + class PeoplePictureRelation(models.Model): """ The PeoplePictureRelation class makes the connection between User and Picture diff --git a/sas/urls.py b/sas/urls.py index 40f5d3b7..0a204e69 100644 --- a/sas/urls.py +++ b/sas/urls.py @@ -22,7 +22,7 @@ # # -from django.conf.urls import url, include +from django.conf.urls import url from sas.views import * @@ -40,4 +40,3 @@ urlpatterns = [ # url(r'^album/new$', AlbumCreateView.as_view(), name='album_new'), # url(r'^(?P[0-9]+)/$', ClubView.as_view(), name='club_view'), ] - diff --git a/sas/views.py b/sas/views.py index 38ab54dc..e50479cd 100644 --- a/sas/views.py +++ b/sas/views.py @@ -22,35 +22,30 @@ # # -from django.shortcuts import render, redirect -from django.http import HttpResponseRedirect, HttpResponse +from django.shortcuts import redirect +from django.http import HttpResponse 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.views.generic import DetailView, TemplateView +from django.views.generic.edit import UpdateView, FormMixin, FormView from django.utils.translation import ugettext_lazy as _ -from django.utils import timezone from django.conf import settings -from django.forms.models import modelform_factory from django import forms from django.core.exceptions import PermissionDenied -from ajax_select import make_ajax_form, make_ajax_field -from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField +from ajax_select import make_ajax_field +from ajax_select.fields import AutoCompleteSelectMultipleField -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 import CanViewMixin, CanEditMixin from core.views.files import send_file, FileView from core.models import SithFile, User, Notification, RealGroup from sas.models import Picture, Album, PeoplePictureRelation + 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) + required=False) def process(self, parent, owner, files, automodere=False): notif = False @@ -62,10 +57,10 @@ class SASForm(forms.Form): notif = not automodere except Exception as e: self.add_error(None, _("Error creating album %(album)s: %(msg)s") % - {'album': self.cleaned_data['album_name'], 'msg': repr(e)}) + {'album': self.cleaned_data['album_name'], 'msg': repr(e)}) 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) + is_folder=False, is_moderated=automodere) if automodere: new_file.moderator = owner try: @@ -80,6 +75,7 @@ class SASForm(forms.Form): if not u.notifications.filter(type="SAS_MODERATION", viewed=False).exists(): Notification(user=u, url=reverse("sas:moderation"), type="SAS_MODERATION").save() + class RelationForm(forms.ModelForm): class Meta: model = PeoplePictureRelation @@ -87,6 +83,7 @@ class RelationForm(forms.ModelForm): widgets = {'picture': forms.HiddenInput} users = AutoCompleteSelectMultipleField('users', show_help_text=False, help_text="", label=_("Add user"), required=False) + class SASMainView(FormView): form_class = SASForm template_name = "sas/main.jinja" @@ -112,6 +109,7 @@ class SASMainView(FormView): kwargs['latest'] = SithFile.objects.filter(is_in_sas=True, is_folder=True, is_moderated=True).order_by('-id')[:5] return kwargs + class PictureView(CanViewMixin, DetailView, FormMixin): model = Picture form_class = RelationForm @@ -133,7 +131,8 @@ class PictureView(CanViewMixin, DetailView, FormMixin): 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_GROUP_SAS_ADMIN_ID): r = PeoplePictureRelation.objects.filter(user=user, picture=self.object).delete() - except: pass + except: + pass if 'ask_removal' in request.GET.keys(): self.object.is_moderated = False self.object.asked_for_removal = True @@ -149,7 +148,7 @@ class PictureView(CanViewMixin, DetailView, FormMixin): for uid in self.form.cleaned_data['users']: u = User.objects.filter(id=uid).first() PeoplePictureRelation(user=u, - picture=self.form.cleaned_data['picture']).save() + 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) @@ -165,15 +164,19 @@ class PictureView(CanViewMixin, DetailView, FormMixin): 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 AlbumUploadView(CanViewMixin, DetailView, FormMixin): model = Album form_class = SASForm @@ -189,7 +192,7 @@ class AlbumUploadView(CanViewMixin, DetailView, FormMixin): if request.user.is_authenticated() and request.user.is_subscribed: if self.form.is_valid(): self.form.process(parent=parent, owner=request.user, files=files, - automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)) + automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)) if self.form.is_valid(): return HttpResponse(str(self.form.errors), status=200) return HttpResponse(str(self.form.errors), status=500) @@ -214,14 +217,14 @@ class AlbumView(CanViewMixin, DetailView, FormMixin): self.form = self.get_form() if 'clipboard' not in request.session.keys(): request.session['clipboard'] = [] - if request.user.can_edit(self.object): # Handle the copy-paste functions + if request.user.can_edit(self.object): # Handle the copy-paste functions FileView.handle_clipboard(request, self.object) parent = SithFile.objects.filter(id=self.object.id).first() files = request.FILES.getlist('images') if request.user.is_authenticated() and request.user.is_subscribed: if self.form.is_valid(): self.form.process(parent=parent, owner=request.user, files=files, - automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)) + automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)) if self.form.is_valid(): return super(AlbumView, self).form_valid(self.form) else: @@ -239,6 +242,7 @@ class AlbumView(CanViewMixin, DetailView, FormMixin): # Admin views + class ModerationView(TemplateView): template_name = "sas/moderation.jinja" @@ -257,23 +261,26 @@ class ModerationView(TemplateView): a.save() elif 'delete' in request.POST.keys(): a.delete() - except: pass + except: + pass return super(ModerationView, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): kwargs = super(ModerationView, self).get_context_data(**kwargs) kwargs['albums_to_moderate'] = Album.objects.filter(is_moderated=False, is_in_sas=True, - is_folder=True).order_by('id') + 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')) return kwargs + class PictureEditForm(forms.ModelForm): class Meta: model = Picture - fields=['name', 'parent'] + fields = ['name', 'parent'] parent = make_ajax_field(Picture, 'parent', 'files', help_text="") + class AlbumEditForm(forms.ModelForm): class Meta: model = Album @@ -283,16 +290,18 @@ class AlbumEditForm(forms.ModelForm): edit_groups = make_ajax_field(Album, 'edit_groups', 'groups', help_text="") recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False) + class PictureEditView(CanEditMixin, UpdateView): - model=Picture - form_class=PictureEditForm - template_name='core/edit.jinja' + model = Picture + form_class = PictureEditForm + template_name = 'core/edit.jinja' pk_url_kwarg = "picture_id" + class AlbumEditView(CanEditMixin, UpdateView): - model=Album - form_class=AlbumEditForm - template_name='core/edit.jinja' + model = Album + form_class = AlbumEditForm + template_name = 'core/edit.jinja' pk_url_kwarg = "album_id" def form_valid(self, form): @@ -300,4 +309,3 @@ class AlbumEditView(CanEditMixin, UpdateView): if form.cleaned_data['recursive']: self.object.apply_rights_recursively(True) return ret -