Skip to content

Commit

Permalink
allow creating a top-level index page with no slug
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgrant committed Apr 20, 2012
1 parent cdb45f0 commit 0e51bb4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion easy_pages/models.py
Expand Up @@ -46,7 +46,7 @@ class Page(MPTTModel):
)

title = models.CharField('Page title', max_length=128)
slug = models.SlugField()
slug = models.SlugField(blank=True)
parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
url_cache = models.CharField(
max_length=256,
Expand Down Expand Up @@ -86,6 +86,12 @@ def __unicode__(self):
class Meta:
unique_together = ('slug', 'parent')

def clean(self):
from django.core.exceptions import ValidationError
# only allow a blank slug if there is no parent (ie for the homepage)
if self.parent and not self.slug:
raise ValidationError('Only top-level pages may have a blank slug.')

def get_absolute_url(self, read_cache=True, write_cache=True):
if getattr(settings, 'SITE_READ_ONLY', False):
write_cache = False
Expand Down
12 changes: 12 additions & 0 deletions easy_pages/tests.py
Expand Up @@ -45,3 +45,15 @@ def test_link_oneword_content(self):
cb = RestrictedHTMLContentBlock.objects.create(
name='main', page=p, block_type=self.cbt, content='')
self.assertEqual(p.get_absolute_url(), '/norm/')


class PageSlugValidationTests(TestCase):
def test_create_index_page(self):
p = Page.objects.create(title='homepage', slug='', page_type='norm')
p.clean()
def test_create_slugless_page_raises_validation_error(self):
parent = Page.objects.create(title='parent', slug='parent', page_type='norm')
child = Page.objects.create(title='homepage', slug='', page_type='norm', parent=parent)
from django.core.exceptions import ValidationError
with self.assertRaises(ValidationError):
child.clean()
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name='django-easy-pages',
version='0.1.0dev',
version='0.1.1',
author='Gabriel Grant',
packages=['easy_pages',],
license='LGPL',
Expand Down

0 comments on commit 0e51bb4

Please sign in to comment.