manager: select template when adding a page (#10117)
This commit is contained in:
parent
c0158d6172
commit
465c856895
|
@ -30,6 +30,20 @@ def get_groups_as_choices():
|
|||
return [(x.id, x.name) for x in Group.objects.all().order_by('name')]
|
||||
|
||||
|
||||
def get_template_name_choices():
|
||||
def template_exists(template):
|
||||
try:
|
||||
get_template(settings.COMBO_PUBLIC_TEMPLATES[template].get('template'))
|
||||
except TemplateDoesNotExist:
|
||||
return False
|
||||
return True
|
||||
|
||||
templates = [(x[0], x[1]['name']) for x in settings.COMBO_PUBLIC_TEMPLATES.items()]
|
||||
templates = [x for x in templates if template_exists(x[0])]
|
||||
templates.sort(key=lambda x: x[1])
|
||||
return templates
|
||||
|
||||
|
||||
class PageDuplicateForm(forms.Form):
|
||||
title = forms.CharField(label=_('New title'), max_length=150, required=False)
|
||||
|
||||
|
@ -38,6 +52,17 @@ class PageExportForm(forms.Form):
|
|||
with_subpages = forms.BooleanField(label=_('Also export subpages'), required=False)
|
||||
|
||||
|
||||
class PageAddForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Page
|
||||
fields = ('title', 'template_name')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['template_name'].widget = forms.Select(choices=get_template_name_choices())
|
||||
self.fields['template_name'].initial = settings.COMBO_DEFAULT_PUBLIC_TEMPLATE
|
||||
|
||||
|
||||
class PageEditTitleForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Page
|
||||
|
@ -90,18 +115,7 @@ class PageSelectTemplateForm(forms.ModelForm):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(PageSelectTemplateForm, self).__init__(*args, **kwargs)
|
||||
templates = [(x[0], x[1]['name']) for x in settings.COMBO_PUBLIC_TEMPLATES.items()]
|
||||
templates = [x for x in templates if self.template_exists(x[0])]
|
||||
templates.sort(key=lambda x: x[1])
|
||||
if 'template_name' in self.fields:
|
||||
self.fields['template_name'].widget = forms.Select(choices=templates)
|
||||
|
||||
def template_exists(self, template):
|
||||
try:
|
||||
get_template(settings.COMBO_PUBLIC_TEMPLATES[template].get('template'))
|
||||
except TemplateDoesNotExist:
|
||||
return False
|
||||
return True
|
||||
self.fields['template_name'].widget = forms.Select(choices=get_template_name_choices())
|
||||
|
||||
|
||||
class PageEditRedirectionForm(forms.ModelForm):
|
||||
|
|
|
@ -43,7 +43,7 @@ from combo.data.utils import (
|
|||
export_site, export_site_tar, import_site, import_site_tar, MissingGroups)
|
||||
from combo import plugins
|
||||
|
||||
from .forms import (PageEditTitleForm, PageVisibilityForm, SiteImportForm, SiteExportForm,
|
||||
from .forms import (PageAddForm, PageEditTitleForm, PageVisibilityForm, SiteImportForm, SiteExportForm,
|
||||
PageEditRedirectionForm, PageSelectTemplateForm, PageEditSlugForm,
|
||||
PageEditPictureForm, PageEditIncludeInNavigationForm,
|
||||
PageEditDescriptionForm, CellVisibilityForm, PageDuplicateForm, PageExportForm)
|
||||
|
@ -145,7 +145,7 @@ def invalid_cell_report(request):
|
|||
class PageAddView(CreateView):
|
||||
model = Page
|
||||
template_name = 'combo/page_add.html'
|
||||
form_class = PageEditTitleForm
|
||||
form_class = PageAddForm
|
||||
|
||||
def get_initial(self):
|
||||
initial = super(PageAddView, self).get_initial()
|
||||
|
@ -166,6 +166,7 @@ class PageAddView(CreateView):
|
|||
def get_success_url(self):
|
||||
return reverse('combo-manager-page-view', kwargs={'pk': self.object.id})
|
||||
|
||||
|
||||
page_add = PageAddView.as_view()
|
||||
|
||||
|
||||
|
|
|
@ -29,6 +29,8 @@ from combo.data.forms import LinkCellForm
|
|||
from combo.data.models import (
|
||||
Page, CellBase, TextCell, LinkCell, ConfigJsonCell, JsonCell, PageSnapshot,
|
||||
LinkListCell, ParentContentCell, MenuCell, ValidityInfo)
|
||||
from combo.manager.forms import PageAddForm
|
||||
from combo.manager.forms import PageSelectTemplateForm
|
||||
from combo.apps.assets.models import Asset
|
||||
from combo.apps.family.models import FamilyInfosCell
|
||||
from combo.apps.search.models import SearchCell
|
||||
|
@ -80,7 +82,7 @@ def test_add_page(app, admin_user):
|
|||
assert resp.location.endswith('/manage/pages/%s/' % page.pk)
|
||||
assert page.slug == 'index'
|
||||
assert page.title == 'Home'
|
||||
assert page.template_name == 'standard' # first one was taken
|
||||
assert page.template_name == 'standard' # default template
|
||||
assert page.exclude_from_navigation is True
|
||||
|
||||
|
||||
|
@ -115,6 +117,27 @@ def test_add_child_page(app, admin_user):
|
|||
app.get('/manage/pages/42/add/', status=404)
|
||||
|
||||
|
||||
def test_page_add_forms(settings):
|
||||
settings.COMBO_PUBLIC_TEMPLATES = {
|
||||
'one': {
|
||||
'name': 'BBB',
|
||||
'template': 'combo/page_template.html',
|
||||
},
|
||||
'two': {
|
||||
'name': 'AAA',
|
||||
'template': 'combo/page_template_sidebar.html',
|
||||
},
|
||||
'unknown': {
|
||||
'name': 'Unknown',
|
||||
'template': 'combo/unknown.html',
|
||||
},
|
||||
}
|
||||
settings.COMBO_DEFAULT_PUBLIC_TEMPLATE = 'one'
|
||||
form = PageAddForm()
|
||||
assert form.fields['template_name'].widget.choices == [('two', 'AAA'), ('one', 'BBB')]
|
||||
assert form['template_name'].initial == 'one'
|
||||
|
||||
|
||||
def test_edit_page(app, admin_user):
|
||||
Page.objects.all().delete()
|
||||
app = login(app)
|
||||
|
@ -143,7 +166,7 @@ def test_edit_page(app, admin_user):
|
|||
# template name
|
||||
resp = resp.click(href='.*/template')
|
||||
assert resp.form['template_name'].value == 'standard'
|
||||
assert len(resp.form['template_name'].options) == 2 # by default there are 2 templates
|
||||
assert len(resp.form['template_name'].options) == 2 # by default there are 2 templates
|
||||
resp.form['template_name'].value = 'standard-sidebar'
|
||||
resp = resp.form.submit()
|
||||
resp = resp.follow()
|
||||
|
@ -170,6 +193,28 @@ def test_edit_page(app, admin_user):
|
|||
assert Page.objects.all()[0].exclude_from_navigation is False
|
||||
|
||||
|
||||
def test_page_edit_template_form(settings):
|
||||
page = Page.objects.create(title='One', slug='one', template_name='two')
|
||||
settings.COMBO_PUBLIC_TEMPLATES = {
|
||||
'one': {
|
||||
'name': 'BBB',
|
||||
'template': 'combo/page_template.html',
|
||||
},
|
||||
'two': {
|
||||
'name': 'AAA',
|
||||
'template': 'combo/page_template_sidebar.html',
|
||||
},
|
||||
'unknown': {
|
||||
'name': 'Unknown',
|
||||
'template': 'combo/unknown.html',
|
||||
},
|
||||
}
|
||||
settings.COMBO_DEFAULT_PUBLIC_TEMPLATE = 'one'
|
||||
form = PageSelectTemplateForm(instance=page)
|
||||
assert form.fields['template_name'].widget.choices == [('two', 'AAA'), ('one', 'BBB')]
|
||||
assert form['template_name'].initial == 'two'
|
||||
|
||||
|
||||
def test_edit_page_cell_invalid_placeholder(freezer, app, admin_user):
|
||||
freezer.move_to('2020-06-05 12:00:01')
|
||||
page = Page.objects.create(title='One', slug='one', template_name='standard')
|
||||
|
|
Loading…
Reference in New Issue