Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRogelio Mita <rogeliomita@activitycentral.com>2013-09-12 17:06:13 (GMT)
committer Rogelio Mita <rogeliomita@activitycentral.com>2013-09-12 17:06:13 (GMT)
commit45e952392e740da72018cb413591e0a09c50c65d (patch)
treee5a2940a3b6d04b567559c8f8688e1769d00c9c9
parent713607e49509c1068397420e4d31615ae5218321 (diff)
Better checking for image options
-rw-r--r--webapp/polls/models.py16
-rw-r--r--webapp/polls/tests/option_tests.py23
-rw-r--r--webapp/polls/tests/poll_tests.py5
-rw-r--r--webapp/polls/tests/structure_tests.py32
-rw-r--r--webapp/polls/views.py5
5 files changed, 61 insertions, 20 deletions
diff --git a/webapp/polls/models.py b/webapp/polls/models.py
index 5852a3a..4c57abd 100644
--- a/webapp/polls/models.py
+++ b/webapp/polls/models.py
@@ -28,6 +28,10 @@ from utils.strings import multiple_replace
from pollster.models import Pollster
+def is_image_file(value):
+ return hasattr(value, "name")
+
+
WIDGET_TYPES = (
('TextInput', 'Texto'),
('MultipleCheckBox', 'Checklist (multiple opciĆ³n)'),
@@ -513,7 +517,7 @@ class Option(AbstractObject, ComponentStructure):
weight = data.get('weight', None)
self.weight = int(weight) if weight else weight
- if not self.img_name and isinstance(self.img, InMemoryUploadedFile):
+ if not self.img_name and is_image_file(self.img):
fileExtension = os.path.splitext(self.img.name)[1]
self.img_name = '%s%s' % (self.id, fileExtension)
@@ -534,13 +538,17 @@ class Option(AbstractObject, ComponentStructure):
settings.IMAGE_OPTIONS_ROOT, str(self.poll.id), self.img_name
)
- def validate(self):
+ def validate(self, img_validator=None):
self.dict_errors = {}
self.errors = []
- if self.img and isinstance(self.img, InMemoryUploadedFile):
+ if self.img and is_image_file(self.img):
+ validator = ImageField().to_python
+ if img_validator:
+ validator = img_validator
+
try:
- img = ImageField().to_python(self.img)
+ img = validator(self.img)
except DjangoValidationError, e:
self.dict_errors.update(
{'img': '%s: %s' % (self.img.name, e.messages[0])})
diff --git a/webapp/polls/tests/option_tests.py b/webapp/polls/tests/option_tests.py
index 03fe684..b22f4a0 100644
--- a/webapp/polls/tests/option_tests.py
+++ b/webapp/polls/tests/option_tests.py
@@ -3,7 +3,9 @@ from django.conf import settings
from polls.models import Poll, Option
-from utils.test import MongoTestCase, mock_in_memory_image, mock_text_file
+from utils.test import MongoTestCase, mock_text_file
+from polls.tests.poll_tests import MockImageFileInterface
+from utils.test import mock_image_file
class OptionTests(MongoTestCase):
@@ -35,7 +37,7 @@ class OptionTests(MongoTestCase):
def test_option_img(self):
- img_file = mock_in_memory_image('image.jpg')
+ img_file = MockImageFileInterface('image.jpg')
data = {'id': "1", 'img': img_file}
option = Option(data)
self.assertEqual(img_file, option.img)
@@ -43,7 +45,7 @@ class OptionTests(MongoTestCase):
def test_option_img_and_weight(self):
- img_file = mock_in_memory_image('image.jpg')
+ img_file = MockImageFileInterface('image.jpg')
data = {'id': "1", 'img': img_file, 'weight': "100"}
option = Option(data)
self.assertEqual(img_file, option.img)
@@ -76,7 +78,7 @@ class OptionTests(MongoTestCase):
def test_image_option_get_absolute_path(self):
- img_file = mock_in_memory_image(size=(250, 250))
+ img_file = MockImageFileInterface(size=(250, 250))
data = {'id': '1', 'img': img_file}
option = Option(data, poll=self.poll)
@@ -97,30 +99,31 @@ class OptionTests(MongoTestCase):
self.assertIsNone(option.img_name)
def test_image_option_validation_invalid_size(self):
-
- invalid_img_file = mock_in_memory_image(size=(300, 200))
+ wrong_size = (300, 200)
+ invalid_img_file = MockImageFileInterface(size=wrong_size)
data = {'id': '1', 'img': invalid_img_file}
option = Option(data)
- self.assertRaises(Option.ValidationError, option.validate)
+ validator = lambda img: mock_image_file(size=wrong_size)
+ self.assertRaises(Option.ValidationError, option.validate, validator)
msg = u"%s: Se necesita una imagen menor a 250x250." % option.id
self.assertTrue(msg in option.errors)
self.assertIsNone(option.img)
self.assertIsNone(option.img_name)
- invalid_img_file = mock_in_memory_image(size=(200, 300))
+ invalid_img_file = MockImageFileInterface(size=wrong_size)
data = {'id': '1', 'img': invalid_img_file}
option = Option(data)
- self.assertRaises(Option.ValidationError, option.validate)
+ self.assertRaises(Option.ValidationError, option.validate, validator)
msg = u"%s: Se necesita una imagen menor a 250x250." % option.id
self.assertTrue(msg in option.errors)
self.assertIsNone(option.img)
self.assertIsNone(option.img_name)
- img_file = mock_in_memory_image(size=(250, 250))
+ img_file = MockImageFileInterface(size=(250, 250))
data = {'id': '1', 'img': img_file}
option = Option(data)
diff --git a/webapp/polls/tests/poll_tests.py b/webapp/polls/tests/poll_tests.py
index 53446cd..af70446 100644
--- a/webapp/polls/tests/poll_tests.py
+++ b/webapp/polls/tests/poll_tests.py
@@ -485,8 +485,9 @@ class AddResultFilesTest(MongoTestCase):
class MockImageFileInterface(object):
- def __init__(self):
- self.name = "a_name.jpg"
+ def __init__(self, name="a_name.jpg", size=(250, 250)):
+ self.name = name
+ self.size = size
class CleanDataMustCheckImagesTypes(TestCase):
diff --git a/webapp/polls/tests/structure_tests.py b/webapp/polls/tests/structure_tests.py
index 4f28dd5..e7ee213 100644
--- a/webapp/polls/tests/structure_tests.py
+++ b/webapp/polls/tests/structure_tests.py
@@ -2,6 +2,7 @@
from polls.models import Poll, Group, Field, Structure
from utils.test import MongoTestCase, mock_in_memory_image
+from polls.tests.poll_tests import MockImageFileInterface
class StructureTests(MongoTestCase):
@@ -284,3 +285,34 @@ class StructureTests(MongoTestCase):
self.assertEqual(
img_name, structure.groups[0].fields[0].get_img_name())
+
+ def test_get_img_options(self):
+ poll = Poll({"name": "poll name"})
+ poll_id = poll.save()
+ poll = poll.get(poll_id)
+ img = "img"
+ data = {
+ 'groups': {
+ '0': {
+ 'name': "group name",
+ 'fields': {
+ '0': {
+ 'name': "field_0_0",
+ 'widget_type': Field.ImageCheckBox,
+ 'options': {
+ '131212': {
+ 'img': MockImageFileInterface(),
+ 'text': "text",
+ 'order': 0
+ }
+ },
+ 'img': img
+ }
+ }
+ }
+ }
+ }
+
+ structure = Structure(data=data, poll=poll)
+
+ self.assertEqual(1, len(structure.get_image_options()))
diff --git a/webapp/polls/views.py b/webapp/polls/views.py
index 5dc023d..db297ac 100644
--- a/webapp/polls/views.py
+++ b/webapp/polls/views.py
@@ -25,6 +25,7 @@ from utils.data_structure import dict_merge
from polls.models import (WIDGET_TYPES, Structure, Poll, PollResult,
PollResultFile)
+from polls.models import is_image_file
from polls.forms import PollForm
from pollster.models import Pollster
@@ -44,10 +45,6 @@ __all__ = [
]
-def is_image_file(value):
- return hasattr(value, "name")
-
-
def clean_data(value):
if isinstance(value, dict):