Skip to content

Commit

Permalink
Check slug field for uniqueness when creating a new story
Browse files Browse the repository at this point in the history
Sentry issue number: PYDOTORG-PROD-22
  • Loading branch information
berkerpeksag committed Jul 5, 2018
1 parent d666e12 commit a3719ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion successstories/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django import forms
from django.db.models import Q
from django.utils.text import slugify

from .models import Story
from cms.forms import ContentManageableModelForm
Expand All @@ -25,7 +27,8 @@ class Meta:

def clean_name(self):
name = self.cleaned_data.get('name')
story = Story.objects.filter(name=name).exclude(pk=self.instance.pk)
slug = slugify(name)
story = Story.objects.filter(Q(name=name) | Q(slug=slug)).exclude(pk=self.instance.pk)
if name is not None and story.exists():
raise forms.ValidationError('Please use a unique name.')
return name
25 changes: 25 additions & 0 deletions successstories/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,28 @@ def test_story_multiline_email_subject(self):
self.assertNotIn('Second line', mail.outbox[0].subject)

del mail.outbox[:]

def test_story_duplicate_slug(self):
url = reverse('success_story_create')

post_data = {
'name': 'r87comwwwpythonorg',
'company_name': 'Company Three',
'company_url': 'http://djangopony.com/',
'category': self.category.pk,
'author': 'Kevin Arnold',
'author_email': '[email protected]',
'pull_quote': 'Liver!',
'content': 'Growing up is never easy.\n\nFoo bar baz.\n',
settings.HONEYPOT_FIELD_NAME: settings.HONEYPOT_VALUE,
}

response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, url)

post_data = post_data.copy()
post_data['name'] = '///r87.com/?www.python.org/'
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Please use a unique name.')

0 comments on commit a3719ef

Please sign in to comment.