From 1a447a36fbf88f1b617ce4d10a865a5101a4b647 Mon Sep 17 00:00:00 2001 From: rabiaiftikhar Date: Thu, 16 Nov 2017 18:16:32 +0500 Subject: [PATCH] EDUCATOR-1672 validate org short_name field and add help_text in django admin to prohibit unicode --- .../migrations/0005_auto_20171116_0640.py | 19 +++++++++ organizations/models.py | 12 +++++- organizations/tests/test_models.py | 42 +++++++++++++++++++ test-requirements.txt | 1 + 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 organizations/migrations/0005_auto_20171116_0640.py create mode 100644 organizations/tests/test_models.py diff --git a/organizations/migrations/0005_auto_20171116_0640.py b/organizations/migrations/0005_auto_20171116_0640.py new file mode 100644 index 00000000..9423154b --- /dev/null +++ b/organizations/migrations/0005_auto_20171116_0640.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('organizations', '0004_auto_20170413_2315'), + ] + + 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. This short name will be used in the course's course key.", max_length=255, verbose_name=b'Short Name', db_index=True), + ), + ] diff --git a/organizations/models.py b/organizations/models.py index 5c596375..f2fd1643 100644 --- a/organizations/models.py +++ b/organizations/models.py @@ -4,6 +4,8 @@ offers one programmatic API -- api.py for direct Python integration. """ +import re +from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import ugettext_lazy as _ from model_utils.models import TimeStampedModel @@ -16,7 +18,11 @@ class Organization(TimeStampedModel): metadata describing the organization, including id, name, and description. """ name = models.CharField(max_length=255, db_index=True) - short_name = models.CharField(max_length=255, db_index=True, verbose_name='Short Name') + 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.") + ) description = models.TextField(null=True, blank=True) logo = models.ImageField( upload_to='organization_logos', @@ -28,6 +34,10 @@ class Organization(TimeStampedModel): 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')) + class OrganizationCourse(TimeStampedModel): """ diff --git a/organizations/tests/test_models.py b/organizations/tests/test_models.py new file mode 100644 index 00000000..6de4ad20 --- /dev/null +++ b/organizations/tests/test_models.py @@ -0,0 +1,42 @@ +# coding=utf-8 +""" +Tests for Organization Model. +""" + +import ddt +from django.core.exceptions import ValidationError +from django.test import TestCase +from organizations.tests.factories import OrganizationFactory + + +@ddt.ddt +class TestOrganizationModel(TestCase): + """ OrganizationModel tests. """ + def setUp(self): + super(TestOrganizationModel, self).setUp() + self.organization = OrganizationFactory.create() + + @ddt.data( + "short name with space", + "short_name[with,special", + "shórt_name" + ) + def test_clean_error(self, short_name): + """ + 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) + + @ddt.data( + "shortnamewithoutspace", + "shortName123", + "short_name" + ) + def test_clean_success(self, short_name): + """ + 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) diff --git a/test-requirements.txt b/test-requirements.txt index cf85071e..36835dd9 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,4 @@ +ddt>=0.8.0,<1.1.1 coverage>=4.0.3,<5.0.0 django_nose>=1.3.0,<=2.0.0 edx-lint>=0.5.2,<1.0.0