Commit fa67db19 by Asad Azam Committed by AsadAzam

Form validation on course number

parent 3a178f57
import html
import logging
import re
import waffle
from dal import autocomplete
......@@ -191,6 +192,15 @@ class CourseForm(BaseForm):
"""
return html.unescape(self.cleaned_data.get("title"))
def clean_number(self):
"""
Validate that number doesn't consist of any special characters
"""
number = self.cleaned_data.get("number")
if not re.match("^[a-zA-Z0-9_.]*$", number):
raise ValidationError(_('Please do not use any spaces or special characters.'))
return number
def clean(self):
cleaned_data = self.cleaned_data
organization = cleaned_data.get("organization")
......
......@@ -246,6 +246,22 @@ class PublisherCustomCourseFormTests(TestCase):
course_form.cleaned_data['number'] = "123a"
self.assertEqual(course_form.clean(), course_form.cleaned_data)
def test_invalid_number(self):
"""
Verify that clean_number raises 'ValidationError' if the course number consists of special characters
or spaces
"""
course_form = CourseForm()
course_form.cleaned_data = {'number': '123 a'}
with self.assertRaises(ValidationError):
course_form.clean_number()
course_form.cleaned_data['number'] = "123.a"
self.assertEqual(course_form.clean_number(), "123.a")
course_form.cleaned_data['number'] = "123a"
self.assertEqual(course_form.clean_number(), "123a")
def test_course_title_formatting(self):
"""
Verify that course_title is properly escaped and saved in database while
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-10-16 17:29+0000\n"
"POT-Creation-Date: 2017-10-25 06:07+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -684,6 +684,10 @@ msgid "Syllabus"
msgstr ""
#: apps/publisher/forms.py
msgid "Please do not use any spaces or special characters."
msgstr ""
#: apps/publisher/forms.py
msgid "This course title already exists"
msgstr ""
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-10-16 17:29+0000\n"
"POT-Creation-Date: 2017-10-25 06:08+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-10-16 17:29+0000\n"
"POT-Creation-Date: 2017-10-25 06:07+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -815,6 +815,12 @@ msgid "Syllabus"
msgstr "Sýlläßüs Ⱡ'σяєм ιρѕυм ∂#"
#: apps/publisher/forms.py
msgid "Please do not use any spaces or special characters."
msgstr ""
"Pléäsé dö nöt üsé äný späçés ör spéçïäl çhäräçtérs. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт "
"αмєт, ¢σηѕє¢тєтυя α#"
#: apps/publisher/forms.py
msgid "This course title already exists"
msgstr ""
"Thïs çöürsé tïtlé älréädý éxïsts Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-10-16 17:29+0000\n"
"POT-Creation-Date: 2017-10-25 06:08+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
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