Skip to content

Commit

Permalink
Merge pull request #158 from ccnmtl/clone-optional-base-url
Browse files Browse the repository at this point in the history
Make base_url an optional argument for cloning
  • Loading branch information
thraxil committed Mar 22, 2017
2 parents 7ca37ed + 5273121 commit 470e14b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pagetree/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

from django import forms
from django.utils.text import slugify
from treebeard.forms import movenodeform_factory
from pagetree.models import Hierarchy, Section

Expand All @@ -16,10 +17,17 @@ class Media:
}
js = ('pagetree/js/src/clone-loading.js',)

base_url = forms.CharField(
required=False, label='Base URL (optional)')

def clean(self):
cleaned_data = super(CloneHierarchyForm, self).clean()
name = cleaned_data.get('name')
base_url = cleaned_data.get('base_url')
if not base_url:
# If there's no base_url, derive it from the name.
cleaned_data['base_url'] = slugify(name)
base_url = cleaned_data['base_url']

if Hierarchy.objects.filter(name=name).exists():
raise forms.ValidationError(
Expand Down
1 change: 1 addition & 0 deletions pagetree/templates/pagetree/clone_hierarchy.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h1>Clone {{ hierarchy.name }}</h1>
<p class="help-block">
The Base URL field will be used as part of the clone's
URL. This should be something like: <code>my-clone</code>
If left blank, it will be derived from the name.
</p>
</form>
</div>
Expand Down
12 changes: 12 additions & 0 deletions pagetree/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def test_clean(self):
self.assertEqual(new_hier.name, 'new name')
self.assertEqual(new_hier.base_url, 'new')

def test_clean_no_base_url(self):
h = HierarchyFactory()
f = CloneHierarchyForm({
'name': 'new name',
'base_url': '',
},
instance=h)
self.assertTrue(f.is_valid())
new_hier = f.save()
self.assertEqual(new_hier.name, 'new name')
self.assertEqual(new_hier.base_url, 'new-name')

def test_clean_prevents_duplicate_name(self):
h = HierarchyFactory()
f = CloneHierarchyForm({
Expand Down

0 comments on commit 470e14b

Please sign in to comment.