Skip to content

Commit

Permalink
Merge pull request #38 from edx/ri/EDUCATOR-1672-org-short-name-valid…
Browse files Browse the repository at this point in the history
…ation-django-admin

EDUCATOR-1672 validate org short_name field and add help_text in django admin to prohibit unicode
  • Loading branch information
Rabia23 authored Nov 17, 2017
2 parents 9e5c342 + 1a447a3 commit 066b17c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
19 changes: 19 additions & 0 deletions organizations/migrations/0005_auto_20171116_0640.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', '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),
),
]
12 changes: 11 additions & 1 deletion organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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):
"""
Expand Down
42 changes: 42 additions & 0 deletions organizations/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 066b17c

Please sign in to comment.