summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2013-08-25 17:35:32 +0200
committerÉtienne Loks <etienne.loks@peacefrogs.net>2013-08-25 17:35:32 +0200
commit2bae28290b1802d27a560da184476b83c711c5a1 (patch)
tree54218f760a4955d071f934a54811a2be9778894e /ishtar_common
parent9658cb19f4a6066eb652e0e9ec04758053a5d67a (diff)
downloadIshtar-2bae28290b1802d27a560da184476b83c711c5a1.tar.bz2
Ishtar-2bae28290b1802d27a560da184476b83c711c5a1.zip
Add images to finds (refs #1314)
* an abstract model class has been defined to manage image and thumbnail with resizing on the fly * max size for images and thumbnails are set in project settings * new field have been added to Find model to save images and thumbnails * form and wizard of Find have been changed
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models.py52
-rw-r--r--ishtar_common/templates/ishtar/wizard/default_wizard.html2
2 files changed, 53 insertions, 1 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 12b1b79f9..fc4cdbd17 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -20,10 +20,14 @@
"""
Models description
"""
+from cStringIO import StringIO
import datetime
+from PIL import Image
+import os
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ValidationError
+from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.validators import validate_slug
from django.utils.translation import ugettext_lazy as _, ugettext
from django.utils.safestring import SafeUnicode, mark_safe
@@ -258,6 +262,54 @@ class GeneralType(models.Model):
for child in cls._get_childs(item, dct, instances, exclude=exclude):
yield child
+class ImageModel(models.Model):
+ image = models.ImageField(upload_to="upload/", blank=True, null=True)
+ thumbnail = models.ImageField(upload_to='upload/thumbs/', blank=True,
+ null=True)
+ IMAGE_MAX_SIZE = settings.IMAGE_MAX_SIZE
+ THUMB_MAX_SIZE = settings.THUMB_MAX_SIZE
+
+ class Meta:
+ abstract = True
+
+ def has_changed(self, field):
+ if not self.pk:
+ return True
+ manager = getattr(self.__class__, 'objects')
+ old = getattr(manager.get(pk=self.pk), field)
+ return not getattr(self, field) == old
+
+ def create_thumb(self, image, size):
+ """Returns the image resized to fit inside a box of the given size"""
+ image.thumbnail(size, Image.ANTIALIAS)
+ temp = StringIO()
+ image.save(temp, 'jpeg')
+ temp.seek(0)
+ return SimpleUploadedFile('temp', temp.read())
+
+ def save(self, *args, **kwargs):
+ # manage images
+ if self.has_changed('image'):
+ # convert to jpg
+ filename = os.path.splitext(os.path.split(self.image.name)[-1])[0]
+ filename = "%s.jpg" % filename
+ image = Image.open(self.image.file)
+
+ # convert to RGB
+ if image.mode not in ('L', 'RGB'):
+ image = image.convert('RGB')
+
+ # resize if necessary
+ self.image.save(filename,
+ self.create_thumb(image, self.IMAGE_MAX_SIZE),
+ save=False)
+
+ # save the thumbnail
+ self.thumbnail.save('_%s' % filename,
+ self.create_thumb(image, self.THUMB_MAX_SIZE),
+ save=False)
+ super(ImageModel, self).save(*args, **kwargs)
+
class HistoryError(Exception):
def __init__(self, value):
self.value = value
diff --git a/ishtar_common/templates/ishtar/wizard/default_wizard.html b/ishtar_common/templates/ishtar/wizard/default_wizard.html
index c14ad281e..28b9850d7 100644
--- a/ishtar_common/templates/ishtar/wizard/default_wizard.html
+++ b/ishtar_common/templates/ishtar/wizard/default_wizard.html
@@ -6,7 +6,7 @@
{% block content %}
{% block wizard_head %}
<h2>{{wizard_label}}</h2>
-<form action="." method="post" name='wizard'>{% csrf_token %}
+<form action="." method="post" name='wizard'{% if wizard.form.file_upload %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}
<ul id='form_path'>
{% for step in previous_steps %}
<li>&raquo;&nbsp;<button name="form_prev_step" value="{{forloop.counter0}}">{{step.form_label}}</button></li>