Skip to content

Commit

Permalink
EDUCATOR-1913 Allow period(.) in organization's short name field
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabia23 committed Dec 6, 2017
1 parent 73a1220 commit 155a740
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
19 changes: 19 additions & 0 deletions organizations/migrations/0006_auto_20171206_0522.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('organizations', '0005_auto_20171116_0640'),
]

operations = [
migrations.AlterField(
model_name='organization',
name='short_name',
field=models.CharField(help_text="Please do not use any spaces or special characters in short name other than period, underscore or hyphen. This short name will be used in the course's course key.", max_length=255, verbose_name=b'Short Name', db_index=True),
),
]
10 changes: 6 additions & 4 deletions organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class Organization(TimeStampedModel):
name = models.CharField(max_length=255, db_index=True)
short_name = models.CharField(
max_length=255, db_index=True, verbose_name='Short Name',
help_text=_("Please do not use any spaces or special characters in short name. "
"This short name will be used in the course's course key.")
help_text=_('Please do not use any spaces or special characters in short name other '
'than period, underscore or hyphen. This short name will be used in the '
'course\'s course key.')
)
description = models.TextField(null=True, blank=True)
logo = models.ImageField(
Expand All @@ -35,8 +36,9 @@ def __unicode__(self):
return u"{name} ({short_name})".format(name=self.name, short_name=self.short_name)

def clean(self):
if not re.match("^[a-zA-Z0-9_-]*$", self.short_name):
raise ValidationError(_('Please do not use any spaces or special characters in the short name field'))
if not re.match("^[a-zA-Z0-9._-]*$", self.short_name):
raise ValidationError(_('Please do not use any spaces or special characters other than '
'period, underscore or hyphen in the short name field'))


class OrganizationCourse(TimeStampedModel):
Expand Down
22 changes: 10 additions & 12 deletions organizations/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ def setUp(self):
self.organization = OrganizationFactory.create()

@ddt.data(
"short name with space",
"short_name[with,special",
"shórt_name"
[" ", ",", "@", "(", "!", "#", "$", "%", "^", "&", "*", "+", "=", "{", "[", "ó"]
)
def test_clean_error(self, short_name):
def test_clean_error(self, invalid_char_list):
"""
Verify that the clean method raises validation error if org short name
consists of special characters or spaces.
"""
self.organization.short_name = short_name
self.assertRaises(ValidationError, self.organization.clean)
for char in invalid_char_list:
self.organization.short_name = char
self.assertRaises(ValidationError, self.organization.clean)

@ddt.data(
"shortnamewithoutspace",
"shortName123",
"short_name"
["shortnamewithoutspace", "shortName123", "short_name", "short-name", "short.name"]
)
def test_clean_success(self, short_name):
def test_clean_success(self, valid_short_name_list):
"""
Verify that the clean method returns None if org short name is valid
"""
self.organization.short_name = short_name
self.assertEqual(self.organization.clean(), None)
for valid_short_name in valid_short_name_list:
self.organization.short_name = valid_short_name
self.assertEqual(self.organization.clean(), None)

0 comments on commit 155a740

Please sign in to comment.