Skip to content

Commit

Permalink
ensure tags are lowercase slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rloomans committed May 20, 2016
1 parent ba1e98a commit 4e26a72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion measure_mate/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.core.validators import RegexValidator
from django.utils.translation import ugettext_lazy as _, ungettext_lazy

import re


class Template(models.Model):
Expand Down Expand Up @@ -52,14 +56,16 @@ def __unicode__(self):
return (self.attribute.name + " - " +
self.name)

uppercase_re = re.compile(r'[A-Z]')
validate_lowercase = RegexValidator(uppercase_re, _(u"Enter a valid 'slug' consisting of lowercase letters, numbers, underscores or hyphens."), 'invalid', True)

class Tag(models.Model):
class Meta:
verbose_name_plural = "Tags"
ordering = ['name']

id = models.AutoField(primary_key=True, verbose_name="Tag ID")
name = models.SlugField(unique=True, verbose_name="Tag Name")
name = models.SlugField(unique=True, verbose_name="Tag Name", validators=[validate_lowercase])

def __unicode__(self):
return self.name
Expand Down
29 changes: 28 additions & 1 deletion measure_mate/tests/api/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,34 @@ def test_create_tag(self):
Ensure we can create a new tag object.
"""
url = reverse('tag-list')
data = {"name": "test-tag-alpha", }
data = {"name": "test-tag-alpha_1", }
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Tag.objects.count(), 1)

def test_create_invalid_tag_with_spaces(self):
"""
Ensure we can't create an invalid tag object.
"""
url = reverse('tag-list')
data = {"name": "test tag alpha", }
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_create_invalid_tag_with_punctuation(self):
"""
Ensure we can't create an invalid tag object.
"""
url = reverse('tag-list')
data = {"name": "test_tag_alpha_!!()", }
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_create_invalid_tag_with_uppercase(self):
"""
Ensure we can't create an invalid tag object.
"""
url = reverse('tag-list')
data = {"name": "Test_Tag_Alpha", }
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

0 comments on commit 4e26a72

Please sign in to comment.