Commit 66cf870c by Asad Azam Committed by AsadAzam

Validate organization key field

parent a6dc243f
import datetime import datetime
import itertools import itertools
import logging import logging
import re
from collections import defaultdict from collections import defaultdict
from urllib.parse import urljoin from urllib.parse import urljoin
from uuid import uuid4 from uuid import uuid4
...@@ -175,7 +176,7 @@ class Organization(TimeStampedModel): ...@@ -175,7 +176,7 @@ class Organization(TimeStampedModel):
""" Organization model. """ """ Organization model. """
partner = models.ForeignKey(Partner, null=True, blank=False) partner = models.ForeignKey(Partner, null=True, blank=False)
uuid = models.UUIDField(blank=False, null=False, default=uuid4, editable=False, verbose_name=_('UUID')) uuid = models.UUIDField(blank=False, null=False, default=uuid4, editable=False, verbose_name=_('UUID'))
key = models.CharField(max_length=255) key = models.CharField(max_length=255, help_text=_('Only ascii characters allowed (a-zA-Z0-9)'))
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
marketing_url_path = models.CharField(max_length=255, null=True, blank=True) marketing_url_path = models.CharField(max_length=255, null=True, blank=True)
description = models.TextField(null=True, blank=True) description = models.TextField(null=True, blank=True)
...@@ -192,6 +193,10 @@ class Organization(TimeStampedModel): ...@@ -192,6 +193,10 @@ class Organization(TimeStampedModel):
help_text=_('Pick a tag from the suggestions. To make a new tag, add a comma after the tag name.'), help_text=_('Pick a tag from the suggestions. To make a new tag, add a comma after the tag name.'),
) )
def clean(self):
if not re.match("^[a-zA-Z0-9_-]*$", self.key):
raise ValidationError(_('Please do not use any spaces or special characters in the key field'))
class Meta: class Meta:
unique_together = ( unique_together = (
('partner', 'key'), ('partner', 'key'),
......
...@@ -331,6 +331,7 @@ class CourseRunTests(TestCase): ...@@ -331,6 +331,7 @@ class CourseRunTests(TestCase):
assert self.course_run.image_url == self.course_run.course.image_url assert self.course_run.image_url == self.course_run.course.image_url
@ddt.ddt
class OrganizationTests(TestCase): class OrganizationTests(TestCase):
""" Tests for the `Organization` model. """ """ Tests for the `Organization` model. """
...@@ -338,6 +339,30 @@ class OrganizationTests(TestCase): ...@@ -338,6 +339,30 @@ class OrganizationTests(TestCase):
super(OrganizationTests, self).setUp() super(OrganizationTests, self).setUp()
self.organization = factories.OrganizationFactory() self.organization = factories.OrganizationFactory()
@ddt.data(
"key with space",
"key[with,special",
"keyó"
)
def test_clean_error(self, key):
"""
Verify that the clean method raises validation error if key consists of special characters
"""
self.organization.key = key
self.assertRaises(ValidationError, self.organization.clean)
@ddt.data(
"keywithoutspace",
"correctkey",
"correct_key"
)
def test_clean_success(self, key):
"""
Verify that the clean method returns None if key is valid
"""
self.organization.key = key
self.assertEqual(self.organization.clean(), None)
def test_str(self): def test_str(self):
""" Verify casting an instance to a string returns a string containing the key and name. """ """ Verify casting an instance to a string returns a string containing the key and name. """
self.assertEqual(str(self.organization), '{key}: {name}'.format(key=self.organization.key, self.assertEqual(str(self.organization), '{key}: {name}'.format(key=self.organization.key,
......
...@@ -294,6 +294,10 @@ msgid "Subject model translations" ...@@ -294,6 +294,10 @@ msgid "Subject model translations"
msgstr "" msgstr ""
#: apps/course_metadata/models.py #: apps/course_metadata/models.py
msgid "Only ascii characters allowed (a-zA-Z0-9)"
msgstr ""
#: apps/course_metadata/models.py
msgid "" msgid ""
"Logo to be displayed on certificates. If this logo is the same as " "Logo to be displayed on certificates. If this logo is the same as "
"logo_image_url, copy and paste the same value to both fields." "logo_image_url, copy and paste the same value to both fields."
...@@ -306,6 +310,10 @@ msgid "" ...@@ -306,6 +310,10 @@ msgid ""
msgstr "" msgstr ""
#: apps/course_metadata/models.py #: apps/course_metadata/models.py
msgid "Please do not use any spaces or special characters in the key field"
msgstr ""
#: apps/course_metadata/models.py
msgid "People" msgid "People"
msgstr "" msgstr ""
......
...@@ -347,6 +347,12 @@ msgid "Subject model translations" ...@@ -347,6 +347,12 @@ msgid "Subject model translations"
msgstr "Süßjéçt mödél tränslätïöns Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕ#" msgstr "Süßjéçt mödél tränslätïöns Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕ#"
#: apps/course_metadata/models.py #: apps/course_metadata/models.py
msgid "Only ascii characters allowed (a-zA-Z0-9)"
msgstr ""
"Önlý äsçïï çhäräçtérs ällöwéd (ä-zÀ-Z0-9) Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєтυя #"
#: apps/course_metadata/models.py
msgid "" msgid ""
"Logo to be displayed on certificates. If this logo is the same as " "Logo to be displayed on certificates. If this logo is the same as "
"logo_image_url, copy and paste the same value to both fields." "logo_image_url, copy and paste the same value to both fields."
...@@ -363,6 +369,12 @@ msgstr "" ...@@ -363,6 +369,12 @@ msgstr ""
"täg nämé. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢т#" "täg nämé. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢т#"
#: apps/course_metadata/models.py #: apps/course_metadata/models.py
msgid "Please do not use any spaces or special characters in the key field"
msgstr ""
"Pléäsé dö nöt üsé äný späçés ör spéçïäl çhäräçtérs ïn thé kéý fïéld Ⱡ'σяєм "
"ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тєтυя #"
#: apps/course_metadata/models.py
msgid "People" msgid "People"
msgstr "Péöplé Ⱡ'σяєм ιρѕυ#" msgstr "Péöplé Ⱡ'σяєм ιρѕυ#"
......
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