Commit 0e788dd8 authored by Skia's avatar Skia

Small refactoring

parent 0e0e5745
......@@ -8,6 +8,7 @@ from PIL import Image
from io import BytesIO
from core.models import SithFile, User
from core.utils import resize_image, exif_auto_rotate
class Picture(SithFile):
class Meta:
......@@ -40,6 +41,22 @@ class Picture(SithFile):
def get_download_thumb_url(self):
return reverse('sas:download_thumb', kwargs={'picture_id': self.id})
def generate_thumbnails(self):
im = Image.open(BytesIO(self.file.read()))
try:
im = exif_auto_rotate(im)
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, 600, self.mime_type.split('/')[-1])
self.file = file
self.file.name = self.name
self.thumbnail = thumb
self.thumbnail.name = self.name
self.compressed = compressed
self.compressed.name = self.name
self.save()
def rotate(self, degree):
for attr in ['file', 'compressed', 'thumbnail']:
if self.__getattribute__(attr):
......@@ -51,10 +68,12 @@ class Picture(SithFile):
self.save()
def get_next(self):
return self.parent.children.exclude(is_moderated=False, asked_for_removal=True).filter(id__gt=self.id).order_by('id').first()
return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
id__gt=self.id).order_by('id').first()
def get_previous(self):
return self.parent.children.exclude(is_moderated=False, asked_for_removal=True).filter(id__lt=self.id).order_by('id').last()
return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
id__lt=self.id).order_by('id').last()
class Album(SithFile):
class Meta:
......
......@@ -19,7 +19,6 @@ 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)
......@@ -40,19 +39,7 @@ class SASForm(forms.Form):
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.generate_thumbnails()
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)})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment