Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make base_url an optional argument for cloning #158

Merged
merged 1 commit into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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