diff options
author | Rogelio Mita <rogeliomita@activitycentral.com> | 2013-07-24 14:41:39 (GMT) |
---|---|---|
committer | Rogelio Mita <rogeliomita@activitycentral.com> | 2013-07-24 15:10:29 (GMT) |
commit | b7f20900d583d88a7415affac60fb4d3f1865040 (patch) | |
tree | 216fa08a6c4469083a8c5f8867afacf84627be90 | |
parent | 0543610a7fcb91861d798ec4fc1239fbab249895 (diff) |
issue 4054: Questions with images allow options without img
-rw-r--r-- | webapp/polls/models.py | 8 | ||||
-rw-r--r-- | webapp/polls/tests/option_tests.py | 31 | ||||
-rw-r--r-- | webapp/sociologist/views.py | 14 |
3 files changed, 44 insertions, 9 deletions
diff --git a/webapp/polls/models.py b/webapp/polls/models.py index 1848ffb..005cac5 100644 --- a/webapp/polls/models.py +++ b/webapp/polls/models.py @@ -463,6 +463,8 @@ class Option(AbstractObject, ComponentStructure): if file_name == option_id: return "%s/%s" % (path, file), settings.MEDIA_URL + return None, None + def get_absolute_path(self): return "%s/%s/%s" % ( settings.IMAGE_OPTIONS_ROOT, str(self.poll.id), self.img_name @@ -472,10 +474,6 @@ class Option(AbstractObject, ComponentStructure): self.dict_errors = {} self.errors = [] - if self.type == "img_input" and not self.img and not self.img_name: - msg = u"opcion %s: imagen requerida." % self.id - self.dict_errors.update({'type': msg}) - if self.img and isinstance(self.img, InMemoryUploadedFile): try: img = ImageField().to_python(self.img) @@ -527,6 +525,8 @@ class Option(AbstractObject, ComponentStructure): data[self.id].update({'img': image_string}) else: data[self.id].update({'img_name': self.img_name}) + elif self.type == "img_input": + data[self.id].update({'img': None}) if self.weight is not None and self.weight != '': data[self.id].update({'weight': self.weight}) diff --git a/webapp/polls/tests/option_tests.py b/webapp/polls/tests/option_tests.py index 626aa19..30793b5 100644 --- a/webapp/polls/tests/option_tests.py +++ b/webapp/polls/tests/option_tests.py @@ -59,6 +59,21 @@ class OptionTests(MongoTestCase): msg = u"opcion %s: ponderaciĆ³n requerida." % option.id self.assertTrue(msg in option.errors) + def test_option_validation_img_is_not_required(self): + + data = { + 'type': 'img_input', + 'text': 'text', + 'weight': '2', + 'order': '1' + } + option = Option(data=data) + + try: + option.validate() + except: + self.fail("Option without img file is allowed") + def test_image_option_get_absolute_path(self): img_file = mock_in_memory_image(size=(250, 250)) @@ -112,6 +127,22 @@ class OptionTests(MongoTestCase): self.assertTrue(len(option.errors) == 0) + def test_option_without_img_and_type_img_input_must_send_null_img(self): + + option_id = '1' + data = { + 'type': 'img_input', + 'text': 'text', + 'weight': '2', + 'order': '1', + 'id': option_id + } + option = Option(data=data) + + to_python = option.to_python()[option_id] + self.assertTrue('img' in to_python.keys()) + self.assertIsNone(to_python['img']) + def test_to_python(self): order = 0 diff --git a/webapp/sociologist/views.py b/webapp/sociologist/views.py index ba91440..4f0a0af 100644 --- a/webapp/sociologist/views.py +++ b/webapp/sociologist/views.py @@ -121,11 +121,15 @@ class PollResultListView(ListView): if "Image" in field_data['widget_type']: img_src, media_url = Option.get_img( str(self.poll.id), option_id) - img = render_to_string( - 'image_option_thumbnail.html', - {"img": open(img_src)} - ) - _answers.append(img) + if img_src is not None: + img = render_to_string( + 'image_option_thumbnail.html', + {"img": open(img_src)} + ) + _answers.append(img) + text = option_selected.get('text', None) + if text: + _answers.append(text) else: _answers.append( option_selected.get('text', "")) |