0001-manager-associate-a-page-to-a-card-model-68675.patch
combo/apps/wcs/utils.py | ||
---|---|---|
85 | 85 |
('%s:%s' % (reference, custom_view['id']), '%s - %s' % (label, custom_view['text'])) |
86 | 86 |
) |
87 | 87 |
return references |
88 | ||
89 | ||
90 |
def get_wcs_matching_card_model(sub_slug): |
|
91 |
card_models = get_wcs_options('/api/cards/@list') |
|
92 |
for carddef_reference, card_label in card_models: |
|
93 |
card_id = '%s_id' % carddef_reference.split(':')[1] |
|
94 |
if '<%s>' % card_id in sub_slug or sub_slug == card_id: |
|
95 |
return carddef_reference, card_label |
combo/data/models.py | ||
---|---|---|
64 | 64 |
from django.utils.translation import gettext_lazy as _ |
65 | 65 | |
66 | 66 |
from combo import utils |
67 |
from combo.apps.wcs.utils import get_wcs_matching_card_model, is_wcs_enabled |
|
67 | 68 |
from combo.utils import NothingInCacheException |
68 | 69 | |
69 | 70 |
from .fields import RichTextField, TemplatableURLField |
... | ... | |
268 | 269 |
return None |
269 | 270 |
return os.path.splitext(self.picture.name)[-1] |
270 | 271 | |
272 |
def get_sub_slug_details(self): |
|
273 |
if not self.sub_slug: |
|
274 |
return |
|
275 | ||
276 |
if not is_wcs_enabled(None): |
|
277 |
return self.sub_slug, None |
|
278 |
result = get_wcs_matching_card_model(self.sub_slug) |
|
279 |
if not result: |
|
280 |
return self.sub_slug, None |
|
281 |
return self.sub_slug.replace('-', '_'), result[1] |
|
282 | ||
271 | 283 |
def save(self, *args, **kwargs): |
272 | 284 |
if 'update_fields' in kwargs: |
273 | 285 |
return super().save(*args, **kwargs) |
combo/manager/forms.py | ||
---|---|---|
25 | 25 |
from django.template.loader import TemplateDoesNotExist, get_template |
26 | 26 |
from django.utils.translation import gettext_lazy as _ |
27 | 27 | |
28 |
from combo.apps.wcs.utils import get_wcs_matching_card_model, get_wcs_options |
|
28 | 29 |
from combo.data.forms import get_page_choices |
29 | 30 |
from combo.data.models import Page, ParentContentCell, SiteSettings, compile_sub_slug |
30 | 31 | |
... | ... | |
149 | 150 |
model = Page |
150 | 151 |
fields = ('slug', 'sub_slug') |
151 | 152 | |
153 |
def __init__(self, *args, **kwargs): |
|
154 |
super().__init__(*args, **kwargs) |
|
155 | ||
156 |
if self.instance.sub_slug: |
|
157 |
result = get_wcs_matching_card_model(self.instance.sub_slug) |
|
158 |
if result: |
|
159 |
self.initial['sub_slug'] = '' |
|
160 | ||
152 | 161 |
def clean_slug(self): |
153 | 162 |
value = self.cleaned_data.get('slug') |
154 | 163 |
if self.instance.slug == value: |
... | ... | |
170 | 179 |
return value |
171 | 180 | |
172 | 181 | |
182 |
class PageEditLinkedCardForm(forms.ModelForm): |
|
183 |
carddef_reference = forms.ChoiceField( |
|
184 |
label=_('Linked card model'), |
|
185 |
required=False, |
|
186 |
) |
|
187 | ||
188 |
class Meta: |
|
189 |
model = Page |
|
190 |
fields = [] |
|
191 | ||
192 |
def __init__(self, *args, **kwargs): |
|
193 |
super().__init__(*args, **kwargs) |
|
194 | ||
195 |
card_models = get_wcs_options('/api/cards/@list') |
|
196 |
self.fields['carddef_reference'].choices = [('', '-----')] + card_models |
|
197 | ||
198 |
if self.instance.sub_slug: |
|
199 |
result = get_wcs_matching_card_model(self.instance.sub_slug) |
|
200 |
if result: |
|
201 |
self.initial['carddef_reference'] = result[0] |
|
202 | ||
203 |
def save(self, commit=True, *args, **kwargs): |
|
204 |
super().save(commit=False, *args, **kwargs) |
|
205 | ||
206 |
if self.cleaned_data.get('carddef_reference'): |
|
207 |
self.instance.sub_slug = '%s_id' % self.cleaned_data['carddef_reference'].split(':')[1] |
|
208 |
else: |
|
209 |
self.instance.sub_slug = '' |
|
210 | ||
211 |
if commit: |
|
212 |
self.instance.save() |
|
213 |
return self.instance |
|
214 | ||
215 | ||
173 | 216 |
class PageEditDescriptionForm(forms.ModelForm): |
174 | 217 |
class Meta: |
175 | 218 |
model = Page |
combo/manager/templates/combo/page_view.html | ||
---|---|---|
40 | 40 |
(<a rel="popup" href="{% url 'combo-manager-page-edit-title' pk=object.id %}">{% trans 'change' %}</a>) |
41 | 41 |
</p> |
42 | 42 | |
43 |
{% with object.get_sub_slug_details as sub_slug_details %} |
|
43 | 44 |
<p> |
44 | 45 |
<label>{% trans 'Slug:' %}</label> |
45 |
<tt>{{ object.slug }}{% if object.sub_slug %}/<span class="subslug">{{ object.sub_slug }}</span>{% endif %}</tt>
|
|
46 |
<tt>{{ object.slug }}{% if sub_slug_details and not sub_slug_details.1 %}/<span class="subslug">{{ sub_slug_details.0 }}</span>{% endif %}</tt>
|
|
46 | 47 |
(<a rel="popup" href="{% url 'combo-manager-page-edit-slug' pk=object.id %}">{% trans 'change' %}</a>) |
47 | 48 |
</p> |
48 | 49 | |
50 |
{% if with_wcs %} |
|
51 |
<p> |
|
52 |
<label>{% trans 'Linked card model:' %}</label> |
|
53 |
<tt>{% if sub_slug_details.1 %}{{ sub_slug_details.1 }} <span class="subslug">{{ sub_slug_details.0 }}</span>{% endif %}</tt> |
|
54 |
(<a rel="popup" href="{% url 'combo-manager-page-edit-linked-card' pk=object.id %}">{% trans 'change' %}</a>) |
|
55 |
</p> |
|
56 |
{% endif %} |
|
57 |
{% endwith %} |
|
58 | ||
49 | 59 |
<p> |
50 | 60 |
<label>{% trans 'Description:' %}</label> |
51 | 61 |
{% if object.description %}{{ object.description|truncatewords:32 }}{% else %}<i>{% trans 'none' %}</i>{% endif %} |
combo/manager/urls.py | ||
---|---|---|
52 | 52 |
name='combo-manager-page-edit-include-in-navigation', |
53 | 53 |
), |
54 | 54 |
re_path(r'^pages/(?P<pk>\d+)/slug$', views.page_edit_slug, name='combo-manager-page-edit-slug'), |
55 |
re_path( |
|
56 |
r'^pages/(?P<pk>\d+)/linked-card$', |
|
57 |
views.page_edit_linked_card, |
|
58 |
name='combo-manager-page-edit-linked-card', |
|
59 |
), |
|
55 | 60 |
re_path(r'^pages/(?P<pk>\d+)/title$', views.page_edit_title, name='combo-manager-page-edit-title'), |
56 | 61 |
re_path( |
57 | 62 |
r'^pages/(?P<pk>\d+)/description$', |
combo/manager/views.py | ||
---|---|---|
54 | 54 |
) |
55 | 55 | |
56 | 56 |
from combo import plugins |
57 |
from combo.apps.wcs.utils import is_wcs_enabled |
|
57 | 58 |
from combo.data.library import get_cell_class |
58 | 59 |
from combo.data.models import CellBase, LinkListCell, Page, PageSnapshot, ParentContentCell, SiteSettings |
59 | 60 |
from combo.data.utils import ( |
... | ... | |
75 | 76 |
PageEditDescriptionForm, |
76 | 77 |
PageEditExtraVariablesFormSet, |
77 | 78 |
PageEditIncludeInNavigationForm, |
79 |
PageEditLinkedCardForm, |
|
78 | 80 |
PageEditPictureForm, |
79 | 81 |
PageEditRedirectionForm, |
80 | 82 |
PageEditRolesForm, |
... | ... | |
335 | 337 |
page_edit_slug = PageEditSlugView.as_view() |
336 | 338 | |
337 | 339 | |
340 |
class PageEditLinkedCardView(PageEditView): |
|
341 |
form_class = PageEditLinkedCardForm |
|
342 |
comment = _('changed linked card') |
|
343 | ||
344 | ||
345 |
page_edit_linked_card = PageEditLinkedCardView.as_view() |
|
346 | ||
347 | ||
338 | 348 |
class PageEditDescriptionView(PageEditView): |
339 | 349 |
form_class = PageEditDescriptionForm |
340 | 350 |
comment = _('changed description') |
... | ... | |
436 | 446 |
context['cell_type_groups'] = list(cell_type_groups.items()) |
437 | 447 |
context['cell_type_groups'].sort(key=lambda x: x[0]) |
438 | 448 |
context['page_has_subpages'] = self.object.get_children().exists() |
449 |
context['with_wcs'] = is_wcs_enabled(None) |
|
439 | 450 | |
440 | 451 |
self.object.prefetched_cells = cells |
441 | 452 |
placeholders = [] |
tests/wcs/test_card.py | ||
---|---|---|
2727 | 2727 |
assert cell2.only_for_user is False |
2728 | 2728 |
assert cell2.carddef_reference == 'default:card_model_1' |
2729 | 2729 |
assert cell2.related_card_path == '__all__' |
2730 | ||
2731 | ||
2732 |
@mock.patch('requests.Session.send', side_effect=mocked_requests_send) |
|
2733 |
def test_page_edit_linked_card(mock_send, app, admin_user): |
|
2734 |
page = Page.objects.create(title='One', slug='one', template_name='two') |
|
2735 | ||
2736 |
app = login(app) |
|
2737 |
resp = app.get('/manage/pages/%s/' % page.pk) |
|
2738 | ||
2739 |
resp = resp.click(href='.*/linked-card') |
|
2740 |
assert [o[0] for o in resp.form['carddef_reference'].options] == [ |
|
2741 |
'', |
|
2742 |
'default:card_model_1', |
|
2743 |
'default:card_model_2', |
|
2744 |
'default:card_model_3', |
|
2745 |
'default:card_a', |
|
2746 |
'default:card_b', |
|
2747 |
'default:card_c', |
|
2748 |
'default:card_d', |
|
2749 |
'default:card_e', |
|
2750 |
'other:card_model_1', |
|
2751 |
'other:card_model_2', |
|
2752 |
'other:card_model_3', |
|
2753 |
'other:card_a', |
|
2754 |
'other:card_b', |
|
2755 |
'other:card_c', |
|
2756 |
'other:card_d', |
|
2757 |
'other:card_e', |
|
2758 |
] |
|
2759 |
resp.form['carddef_reference'] = 'default:card_model_3' |
|
2760 |
resp = resp.form.submit().follow() |
|
2761 |
page.refresh_from_db() |
|
2762 |
assert page.sub_slug == 'card_model_3_id' |
|
2763 |
resp = app.get('/manage/pages/%s/slug' % page.pk) |
|
2764 |
assert resp.form['sub_slug'].value == '' |
|
2765 | ||
2766 |
resp = app.get('/manage/pages/%s/linked-card' % page.pk) |
|
2767 |
assert resp.form['carddef_reference'].value == 'default:card_model_3' |
|
2768 |
resp.form['carddef_reference'] = '' |
|
2769 |
resp = resp.form.submit().follow() |
|
2770 |
page.refresh_from_db() |
|
2771 |
assert page.sub_slug == '' |
|
2730 |
- |