Commit d7ad96ce by tasawernawaz Committed by Tasawer Nawaz

override image size validator for course image

ECOM-7670
parent e6369dc5
...@@ -12,7 +12,6 @@ from django_fsm import FSMField, transition ...@@ -12,7 +12,6 @@ from django_fsm import FSMField, transition
from simple_history.models import HistoricalRecords from simple_history.models import HistoricalRecords
from sortedm2m.fields import SortedManyToManyField from sortedm2m.fields import SortedManyToManyField
from stdimage.models import StdImageField from stdimage.models import StdImageField
from stdimage.validators import MaxSizeValidator, MinSizeValidator
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
from course_discovery.apps.core.models import Currency, User from course_discovery.apps.core.models import Currency, User
...@@ -23,6 +22,7 @@ from course_discovery.apps.ietf_language_tags.models import LanguageTag ...@@ -23,6 +22,7 @@ from course_discovery.apps.ietf_language_tags.models import LanguageTag
from course_discovery.apps.publisher import emails from course_discovery.apps.publisher import emails
from course_discovery.apps.publisher.choices import CourseRunStateChoices, CourseStateChoices, PublisherUserRole from course_discovery.apps.publisher.choices import CourseRunStateChoices, CourseStateChoices, PublisherUserRole
from course_discovery.apps.publisher.utils import is_email_notification_enabled from course_discovery.apps.publisher.utils import is_email_notification_enabled
from course_discovery.apps.publisher.validators import ImageSizeValidator
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -80,7 +80,7 @@ class Course(TimeStampedModel, ChangedByMixin): ...@@ -80,7 +80,7 @@ class Course(TimeStampedModel, ChangedByMixin):
variations={ variations={
'thumbnail': (100, 100, True), 'thumbnail': (100, 100, True),
}, },
validators=[MaxSizeValidator(2120, 1192), MinSizeValidator(2120, 1192), ] validators=[ImageSizeValidator(width=2120, height=1192)]
) )
is_seo_review = models.BooleanField(default=False) is_seo_review = models.BooleanField(default=False)
......
...@@ -36,9 +36,6 @@ from course_discovery.apps.publisher.wrappers import CourseRunWrapper ...@@ -36,9 +36,6 @@ from course_discovery.apps.publisher.wrappers import CourseRunWrapper
from course_discovery.apps.publisher_comments.models import CommentTypeChoices from course_discovery.apps.publisher_comments.models import CommentTypeChoices
from course_discovery.apps.publisher_comments.tests.factories import CommentFactory from course_discovery.apps.publisher_comments.tests.factories import CommentFactory
IMAGE_TOO_SMALL = 'The image you uploaded is too small. The required minimum resolution is: 2120x1192 px.'
IMAGE_TOO_LARGE = 'The image you uploaded is too large. The required maximum resolution is: 2120x1192 px.'
@ddt.ddt @ddt.ddt
class CreateCourseViewTests(TestCase): class CreateCourseViewTests(TestCase):
...@@ -147,22 +144,24 @@ class CreateCourseViewTests(TestCase): ...@@ -147,22 +144,24 @@ class CreateCourseViewTests(TestCase):
self._assert_test_data(response, course, self.seat.type, self.seat.price) self._assert_test_data(response, course, self.seat.type, self.seat.price)
@ddt.data( @ddt.data(
(make_image_file('test_banner00.jpg', width=2120, height=1191), [IMAGE_TOO_SMALL]), make_image_file('test_banner00.jpg', width=2120, height=1191),
(make_image_file('test_banner01.jpg', width=2120, height=1193), [IMAGE_TOO_LARGE]), make_image_file('test_banner01.jpg', width=2120, height=1193),
(make_image_file('test_banner02.jpg', width=2119, height=1192), [IMAGE_TOO_SMALL]), make_image_file('test_banner02.jpg', width=2119, height=1192),
(make_image_file('test_banner03.jpg', width=2121, height=1192), [IMAGE_TOO_LARGE]), make_image_file('test_banner03.jpg', width=2121, height=1192),
(make_image_file('test_banner04.jpg', width=2121, height=1191), [IMAGE_TOO_LARGE, IMAGE_TOO_SMALL]), make_image_file('test_banner04.jpg', width=2121, height=1191),
) )
@ddt.unpack def test_create_course_invalid_image(self, image):
def test_create_course_invalid_image(self, image, errors):
""" """
Verify that a new course with an invalid image shows the proper error. Verify that a new course with an invalid image shows the proper error.
""" """
image_error = [
'The image you uploaded is of incorrect resolution. Course image files must be 2120 x 1192 pixels in size.'
]
self.user.groups.add(Group.objects.get(name=ADMIN_GROUP_NAME)) self.user.groups.add(Group.objects.get(name=ADMIN_GROUP_NAME))
self._assert_records(1) self._assert_records(1)
course_dict = self._post_data({'image': image}, self.course, self.course_run, self.seat) course_dict = self._post_data({'image': image}, self.course, self.course_run, self.seat)
response = self.client.post(reverse('publisher:publisher_courses_new'), course_dict, files=image) response = self.client.post(reverse('publisher:publisher_courses_new'), course_dict, files=image)
self.assertEqual(response.context['course_form'].errors['image'], errors) self.assertEqual(response.context['course_form'].errors['image'], image_error)
self._assert_records(1) self._assert_records(1)
def test_create_with_fail_transaction(self): def test_create_with_fail_transaction(self):
......
from django.utils.translation import ugettext_lazy as _
from stdimage.validators import BaseSizeValidator
class ImageSizeValidator(BaseSizeValidator):
"""
ImageField validator to validate the width and height of an image.
"""
def compare(self, img_size, limit_size): # pylint: disable=arguments-differ
return img_size[0] != limit_size[0] or img_size[1] != limit_size[1]
message = _(
'The image you uploaded is of incorrect resolution. '
'Course image files must be %(with)s x %(height)s pixels in size.'
)
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-04-11 13:01+0500\n" "POT-Creation-Date: 2017-04-13 13:09+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: apps/api/filters.py #: apps/api/filters.py
#, python-brace-format #, python-brace-format
...@@ -781,6 +781,13 @@ msgstr "" ...@@ -781,6 +781,13 @@ msgstr ""
msgid "Course Role" msgid "Course Role"
msgstr "" msgstr ""
#: apps/publisher/validators.py
#, python-format
msgid ""
"The image you uploaded is of incorrect resolution. Course image files must "
"be %(with)s x %(height)s pixels in size."
msgstr ""
#: apps/publisher/views.py #: apps/publisher/views.py
msgid "PARTNER MANAGER" msgid "PARTNER MANAGER"
msgstr "" msgstr ""
...@@ -1441,7 +1448,6 @@ msgid "All of the following information is required." ...@@ -1441,7 +1448,6 @@ msgid "All of the following information is required."
msgstr "" msgstr ""
#: templates/publisher/add_update_course_form.html #: templates/publisher/add_update_course_form.html
#: templates/publisher/course_run/edit_run_form.html
msgid "About Page Information" msgid "About Page Information"
msgstr "" msgstr ""
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-04-11 13:01+0500\n" "POT-Creation-Date: 2017-04-13 13:09+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: static/js/catalogs-change-form.js #: static/js/catalogs-change-form.js
msgid "Preview" msgid "Preview"
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-04-11 13:01+0500\n" "POT-Creation-Date: 2017-04-13 13:09+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: apps/api/filters.py #: apps/api/filters.py
...@@ -925,6 +925,15 @@ msgstr "Örgänïzätïön Rölé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт α ...@@ -925,6 +925,15 @@ msgstr "Örgänïzätïön Rölé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт α
msgid "Course Role" msgid "Course Role"
msgstr "Çöürsé Rölé Ⱡ'σяєм ιρѕυм ∂σłσя #" msgstr "Çöürsé Rölé Ⱡ'σяєм ιρѕυм ∂σłσя #"
#: apps/publisher/validators.py
#, python-format
msgid ""
"The image you uploaded is of incorrect resolution. Course image files must "
"be %(with)s x %(height)s pixels in size."
msgstr ""
"Thé ïmägé ýöü üplöädéd ïs öf ïnçörréçt résölütïön. Çöürsé ïmägé fïlés müst "
"ßé %(with)s x %(height)s pïxéls ïn sïzé. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αм#"
#: apps/publisher/views.py #: apps/publisher/views.py
msgid "PARTNER MANAGER" msgid "PARTNER MANAGER"
msgstr "PÀRTNÉR MÀNÀGÉR Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт α#" msgstr "PÀRTNÉR MÀNÀGÉR Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт α#"
...@@ -1693,7 +1702,6 @@ msgstr "" ...@@ -1693,7 +1702,6 @@ msgstr ""
"¢σηѕє¢тєтυя #" "¢σηѕє¢тєтυя #"
#: templates/publisher/add_update_course_form.html #: templates/publisher/add_update_course_form.html
#: templates/publisher/course_run/edit_run_form.html
msgid "About Page Information" msgid "About Page Information"
msgstr "Àßöüt Pägé Ìnförmätïön Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢#" msgstr "Àßöüt Pägé Ìnförmätïön Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢#"
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-04-11 13:01+0500\n" "POT-Creation-Date: 2017-04-13 13:09+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: static/js/catalogs-change-form.js #: static/js/catalogs-change-form.js
......
...@@ -310,8 +310,13 @@ ...@@ -310,8 +310,13 @@
<label class="field-label ">{{ form.image.label_tag }}</label> <label class="field-label ">{{ form.image.label_tag }}</label>
<div class="course-image-field"> <div class="course-image-field">
{{ form.image }} {{ form.image }}
{{ form.image.errors }} {% if form.image.errors %}
<div class="image-error"></div> <div class="field-message has-error">
<span class="field-message-content">
{{ form.image.errors|escape }}
</span>
</div>
{% endif %}
</div> </div>
</div> </div>
</div> </div>
......
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