Format sas

This commit is contained in:
Pierre Brunet 2017-06-12 10:02:38 +02:00
parent 13ec91e7e5
commit e2311dcb71
4 changed files with 52 additions and 45 deletions

View File

@ -30,5 +30,3 @@ from sas.models import *
admin.site.register(Album)
# admin.site.register(Picture)
admin.site.register(PeoplePictureRelation)

View File

@ -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
@ -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])
@ -113,6 +113,7 @@ class Picture(SithFile):
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
@ -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

View File

@ -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<club_id>[0-9]+)/$', ClubView.as_view(), name='club_view'),
]

View File

@ -22,31 +22,26 @@
#
#
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"),
@ -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
@ -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
@ -239,6 +242,7 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
# Admin views
class ModerationView(TemplateView):
template_name = "sas/moderation.jinja"
@ -257,7 +261,8 @@ 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):
@ -268,12 +273,14 @@ class ModerationView(TemplateView):
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']
parent = make_ajax_field(Picture, 'parent', 'files', help_text="")
class AlbumEditForm(forms.ModelForm):
class Meta:
model = Album
@ -283,12 +290,14 @@ 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'
pk_url_kwarg = "picture_id"
class AlbumEditView(CanEditMixin, UpdateView):
model = Album
form_class = AlbumEditForm
@ -300,4 +309,3 @@ class AlbumEditView(CanEditMixin, UpdateView):
if form.cleaned_data['recursive']:
self.object.apply_rights_recursively(True)
return ret