0001-manager-select-template-when-adding-a-page-10117.patch
combo/manager/forms.py | ||
---|---|---|
30 | 30 |
return [(x.id, x.name) for x in Group.objects.all().order_by('name')] |
31 | 31 | |
32 | 32 | |
33 |
def get_template_name_choices(): |
|
34 |
def template_exists(template): |
|
35 |
try: |
|
36 |
get_template(settings.COMBO_PUBLIC_TEMPLATES[template].get('template')) |
|
37 |
except TemplateDoesNotExist: |
|
38 |
return False |
|
39 |
return True |
|
40 | ||
41 |
templates = [(x[0], x[1]['name']) for x in settings.COMBO_PUBLIC_TEMPLATES.items()] |
|
42 |
templates = [x for x in templates if template_exists(x[0])] |
|
43 |
templates.sort(key=lambda x: x[1]) |
|
44 |
return templates |
|
45 | ||
46 | ||
33 | 47 |
class PageDuplicateForm(forms.Form): |
34 | 48 |
title = forms.CharField(label=_('New title'), max_length=150, required=False) |
35 | 49 | |
... | ... | |
38 | 52 |
with_subpages = forms.BooleanField(label=_('Also export subpages'), required=False) |
39 | 53 | |
40 | 54 | |
55 |
class PageAddForm(forms.ModelForm): |
|
56 |
class Meta: |
|
57 |
model = Page |
|
58 |
fields = ('title', 'template_name') |
|
59 | ||
60 |
def __init__(self, *args, **kwargs): |
|
61 |
super().__init__(*args, **kwargs) |
|
62 |
self.fields['template_name'].widget = forms.Select(choices=get_template_name_choices()) |
|
63 |
self.fields['template_name'].initial = settings.COMBO_DEFAULT_PUBLIC_TEMPLATE |
|
64 | ||
65 | ||
41 | 66 |
class PageEditTitleForm(forms.ModelForm): |
42 | 67 |
class Meta: |
43 | 68 |
model = Page |
... | ... | |
90 | 115 | |
91 | 116 |
def __init__(self, *args, **kwargs): |
92 | 117 |
super(PageSelectTemplateForm, self).__init__(*args, **kwargs) |
93 |
templates = [(x[0], x[1]['name']) for x in settings.COMBO_PUBLIC_TEMPLATES.items()] |
|
94 |
templates = [x for x in templates if self.template_exists(x[0])] |
|
95 |
templates.sort(key=lambda x: x[1]) |
|
96 |
if 'template_name' in self.fields: |
|
97 |
self.fields['template_name'].widget = forms.Select(choices=templates) |
|
98 | ||
99 |
def template_exists(self, template): |
|
100 |
try: |
|
101 |
get_template(settings.COMBO_PUBLIC_TEMPLATES[template].get('template')) |
|
102 |
except TemplateDoesNotExist: |
|
103 |
return False |
|
104 |
return True |
|
118 |
self.fields['template_name'].widget = forms.Select(choices=get_template_name_choices()) |
|
105 | 119 | |
106 | 120 | |
107 | 121 |
class PageEditRedirectionForm(forms.ModelForm): |
combo/manager/views.py | ||
---|---|---|
40 | 40 |
from combo.data.utils import export_site, import_site, MissingGroups |
41 | 41 |
from combo import plugins |
42 | 42 | |
43 |
from .forms import (PageEditTitleForm, PageVisibilityForm, SiteImportForm, |
|
43 |
from .forms import (PageAddForm, PageEditTitleForm, PageVisibilityForm, SiteImportForm,
|
|
44 | 44 |
PageEditRedirectionForm, PageSelectTemplateForm, PageEditSlugForm, |
45 | 45 |
PageEditPictureForm, PageEditIncludeInNavigationForm, |
46 | 46 |
PageEditDescriptionForm, CellVisibilityForm, PageDuplicateForm, PageExportForm) |
... | ... | |
121 | 121 |
class PageAddView(CreateView): |
122 | 122 |
model = Page |
123 | 123 |
template_name = 'combo/page_add.html' |
124 |
form_class = PageEditTitleForm
|
|
124 |
form_class = PageAddForm
|
|
125 | 125 | |
126 | 126 |
def get_initial(self): |
127 | 127 |
initial = super(PageAddView, self).get_initial() |
... | ... | |
142 | 142 |
def get_success_url(self): |
143 | 143 |
return reverse('combo-manager-page-view', kwargs={'pk': self.object.id}) |
144 | 144 | |
145 | ||
145 | 146 |
page_add = PageAddView.as_view() |
146 | 147 | |
147 | 148 |
tests/test_manager.py | ||
---|---|---|
28 | 28 |
from combo.data.models import ( |
29 | 29 |
Page, CellBase, TextCell, LinkCell, ConfigJsonCell, JsonCell, PageSnapshot, |
30 | 30 |
LinkListCell, ParentContentCell, MenuCell, ValidityInfo) |
31 |
from combo.manager.forms import PageAddForm |
|
32 |
from combo.manager.forms import PageSelectTemplateForm |
|
31 | 33 |
from combo.apps.assets.models import Asset |
32 | 34 |
from combo.apps.family.models import FamilyInfosCell |
33 | 35 |
from combo.apps.search.models import SearchCell |
... | ... | |
79 | 81 |
assert resp.location.endswith('/manage/pages/%s/' % page.pk) |
80 | 82 |
assert page.slug == 'index' |
81 | 83 |
assert page.title == 'Home' |
82 |
assert page.template_name == 'standard' # first one was taken
|
|
84 |
assert page.template_name == 'standard' # default template
|
|
83 | 85 |
assert page.exclude_from_navigation is True |
84 | 86 | |
85 | 87 | |
... | ... | |
114 | 116 |
app.get('/manage/pages/42/add/', status=404) |
115 | 117 | |
116 | 118 | |
119 |
def test_page_add_forms(settings): |
|
120 |
settings.COMBO_PUBLIC_TEMPLATES = { |
|
121 |
'one': { |
|
122 |
'name': 'BBB', |
|
123 |
'template': 'combo/page_template.html', |
|
124 |
}, |
|
125 |
'two': { |
|
126 |
'name': 'AAA', |
|
127 |
'template': 'combo/page_template_sidebar.html', |
|
128 |
}, |
|
129 |
'unknown': { |
|
130 |
'name': 'Unknown', |
|
131 |
'template': 'combo/unknown.html', |
|
132 |
}, |
|
133 |
} |
|
134 |
settings.COMBO_DEFAULT_PUBLIC_TEMPLATE = 'one' |
|
135 |
form = PageAddForm() |
|
136 |
assert form.fields['template_name'].widget.choices == [('two', 'AAA'), ('one', 'BBB')] |
|
137 |
assert form['template_name'].initial == 'one' |
|
138 | ||
139 | ||
117 | 140 |
def test_edit_page(app, admin_user): |
118 | 141 |
Page.objects.all().delete() |
119 | 142 |
app = login(app) |
... | ... | |
142 | 165 |
# template name |
143 | 166 |
resp = resp.click(href='.*/template') |
144 | 167 |
assert resp.form['template_name'].value == 'standard' |
145 |
assert len(resp.form['template_name'].options) == 2 # by default there are 2 templates |
|
168 |
assert len(resp.form['template_name'].options) == 2 # by default there are 2 templates
|
|
146 | 169 |
resp.form['template_name'].value = 'standard-sidebar' |
147 | 170 |
resp = resp.form.submit() |
148 | 171 |
resp = resp.follow() |
... | ... | |
169 | 192 |
assert Page.objects.all()[0].exclude_from_navigation is False |
170 | 193 | |
171 | 194 | |
195 |
def test_page_edit_template_form(settings): |
|
196 |
page = Page.objects.create(title='One', slug='one', template_name='two') |
|
197 |
settings.COMBO_PUBLIC_TEMPLATES = { |
|
198 |
'one': { |
|
199 |
'name': 'BBB', |
|
200 |
'template': 'combo/page_template.html', |
|
201 |
}, |
|
202 |
'two': { |
|
203 |
'name': 'AAA', |
|
204 |
'template': 'combo/page_template_sidebar.html', |
|
205 |
}, |
|
206 |
'unknown': { |
|
207 |
'name': 'Unknown', |
|
208 |
'template': 'combo/unknown.html', |
|
209 |
}, |
|
210 |
} |
|
211 |
settings.COMBO_DEFAULT_PUBLIC_TEMPLATE = 'one' |
|
212 |
form = PageSelectTemplateForm(instance=page) |
|
213 |
assert form.fields['template_name'].widget.choices == [('two', 'AAA'), ('one', 'BBB')] |
|
214 |
assert form['template_name'].initial == 'two' |
|
215 | ||
216 | ||
172 | 217 |
def test_edit_page_cell_invalid_placeholder(freezer, app, admin_user): |
173 | 218 |
freezer.move_to('2020-06-05 12:00:01') |
174 | 219 |
page = Page.objects.create(title='One', slug='one', template_name='standard') |
175 |
- |