wcs: custom schema, link item, target a page with sub_slug (#68534)

This commit is contained in:
Lauréline Guérin 2022-08-30 15:09:00 +02:00
parent 8cdbd1c0e7
commit 04165ad13c
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
6 changed files with 126 additions and 43 deletions

View File

@ -1340,6 +1340,20 @@ class WcsCardCell(CardMixin, CellBase):
def css_class_names(self):
return '%s card card-%s %s' % (super().css_class_names, self.display_mode, self.get_reference())
def get_matching_pages(self, order=True):
if order:
pages = Page.get_with_hierarchy_attributes().values()
pages_with_sub_slug = [p for p in pages if p.sub_slug]
else:
pages_with_sub_slug = Page.objects.exclude(sub_slug='')
card_id = '%s_id' % self.card_slug
matching_pages = [
p for p in pages_with_sub_slug if '<%s>' % card_id in p.sub_slug or p.sub_slug == card_id
]
if not order:
return matching_pages
return Page.get_as_reordered_flat_hierarchy(matching_pages)
def get_cell_extra_context(self, context):
extra_context = super().get_cell_extra_context(context)
if self.title_type in ['auto', 'manual']:
@ -1358,11 +1372,7 @@ class WcsCardCell(CardMixin, CellBase):
# not configured
return extra_context
pages_with_sub_slug = Page.objects.exclude(sub_slug='')
card_id = '%s_id' % self.carddef_reference.split(':')[1]
matching_pages = [
p for p in pages_with_sub_slug if '<%s>' % card_id in p.sub_slug or p.sub_slug == card_id
]
matching_pages = self.get_matching_pages(order=False)
if matching_pages:
card_page = matching_pages[0]
extra_context['card_page_base_url'] = card_page.get_online_url()
@ -1433,13 +1443,25 @@ class WcsCardCell(CardMixin, CellBase):
)
if item['varname'] == '@link@':
render_template(
item=item,
template_key='url_template',
template_context=custom_context,
target_key='urls',
target_context=extra_context,
)
if item.get('page'):
if item['page'] in extra_context['card']['urls']:
pass
try:
target = Page.objects.get(pk=item['page'])
extra_context['card']['urls'][item['page']] = '%s%s/' % (
target.get_online_url(),
card_id,
)
except Page.DoesNotExist:
pass
else:
render_template(
item=item,
template_key='url_template',
template_context=custom_context,
target_key='urls',
target_context=extra_context,
)
return extra_context

View File

@ -16,26 +16,32 @@
{% with cell.get_custom_schema as custom_schema %}
<div class="{{ custom_schema.grid_class }}">
{% for item in custom_schema.cells %}
{% if item.varname == "@custom@" and item.template %}
<div class="{{ item.cell_size|default:"" }}">
{% with card.custom_fields|get:item.template|force_escape as value %}
{% if item.display_mode == "title" %}
<h3>{{ value }}</h3>
{% elif item.display_mode == "subtitle" %}
<h4>{{ value }}</h4>
{% elif item.display_mode == "label" %}
<div class="label">{{ value }}</div>
{% elif item.display_mode == "text" %}
<div class="value">{{ value }}</div>
{% if item.varname == "@custom@" %}
{% if item.template %}
<div class="{{ item.cell_size|default:"" }}">
{% with card.custom_fields|get:item.template|force_escape as value %}
{% if item.display_mode == "title" %}
<h3>{{ value }}</h3>
{% elif item.display_mode == "subtitle" %}
<h4>{{ value }}</h4>
{% elif item.display_mode == "label" %}
<div class="label">{{ value }}</div>
{% elif item.display_mode == "text" %}
<div class="value">{{ value }}</div>
{% endif %}
{% endwith %}
</div>
{% endif %}
{% elif item.varname == "@link@" %}
{% if item.template and item.page|default:item.url_template %}
<div class="{{ item.cell_size|default:"" }}">
{% with item.page|default:item.url_template as url_key %}
{% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
<div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
{% endwith %}
{% endwith %}
</div>
{% endif %}
{% endwith %}
</div>
{% elif item.varname == "@link@" and item.url_template and item.template %}
<div class="{{ item.cell_size|default:"" }}">
{% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:item.url_template|force_escape as link_url %}
<div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
{% endwith %}
</div>
{% else %}
{% if item.varname %}
{% with fields_by_varnames|get:item.varname as field %}

View File

@ -140,7 +140,17 @@
</p>
<p>
<label>
{% trans "URL template" %}
{% trans "URL" %}
<select name="link_page" data-dynamic-display-parent="true">
{% for page in cell.get_matching_pages %}
<option value="{{ page.pk }}">{{ page.get_full_path_titles }}</option>
{% endfor %}
<option value="">{% trans "URL template" %}</option>
</select>
</label>
</p>
<p data-dynamic-display-child-of="link_page" data-dynamic-display-value="">
<label>
<textarea name="link_url_template" style="resize: vertical;"></textarea>
</label>
</p>

View File

@ -314,14 +314,13 @@ class Page(models.Model):
return '/'
return '/' + '/'.join(parts) + '/'
def get_full_path_titles(self):
parts = [x.title for x in self.get_parents_and_self()]
return ' / '.join(parts)
def get_page_of_level(self, level):
'''Return page of given level in the page hierarchy.'''
parts = [self]
page = self
while page.parent_id:
page = page._parent if hasattr(page, '_parent') else page.parent
parts.append(page)
parts.reverse()
parts = self.get_parents_and_self()
try:
return parts[level]
except IndexError:

View File

@ -540,9 +540,10 @@ Card_cell_custom.prototype = {
this.grid_cell_form.custom_template = this.grid_cell_form[6];
this.grid_cell_form.custom_display_mode = this.grid_cell_form[7];
this.grid_cell_form.link_label_template = this.grid_cell_form[8];
this.grid_cell_form.link_url_template = this.grid_cell_form[9];
this.grid_cell_form.link_display_mode = this.grid_cell_form[10];
this.grid_cell_form.size = this.grid_cell_form[11];
this.grid_cell_form.link_page = this.grid_cell_form[9];
this.grid_cell_form.link_url_template = this.grid_cell_form[10];
this.grid_cell_form.link_display_mode = this.grid_cell_form[11];
this.grid_cell_form.size = this.grid_cell_form[12];
const varname_select = this.grid_cell_form.field_varname;
this.cardSchema.fields.forEach(function(el, id) {
if (el.varname) {
@ -562,6 +563,7 @@ Card_cell_custom.prototype = {
$(document).trigger('combo:dialog-loaded');
$(_self.grid_cell_form.field_content).change();
$(_self.grid_cell_form.field_empty_display_mode).change();
$(_self.grid_cell_form.link_page).change();
},
buttons: [
{
@ -611,7 +613,13 @@ Card_cell_custom.prototype = {
if (schema_cell.varname == '@custom@') {
cell_content = (schema_cell.template || '') + ' (' + gettext('Custom') + ')';
} else if (schema_cell.varname == '@link@') {
cell_content = (schema_cell.template || '') + ': ' + (schema_cell.url_template || '') + ' (' + gettext('Link') + ')';
cell_content = (schema_cell.template || '') + ': ';
if (schema_cell.page) {
cell_content += $(this.grid_cell_form).find('select[name="link_page"] option[value="' + schema_cell.page + '"]').text();
} else {
cell_content += (schema_cell.url_template || '');
}
cell_content += ' (' + gettext('Link') + ')';
} else {
cell_content = schema_field.label;
}
@ -666,6 +674,7 @@ Card_cell_custom.prototype = {
this.grid_cell_form.custom_template.value = grid_cell.dataset.template || '';
this.grid_cell_form.custom_display_mode.value = grid_cell.dataset.display_mode;
this.grid_cell_form.link_label_template.value = '';
this.grid_cell_form.link_page.value = '';
this.grid_cell_form.link_url_template.value = '';
this.grid_cell_form.link_display_mode.value = '';
} else if (grid_cell.dataset.varname == '@link@') {
@ -678,6 +687,7 @@ Card_cell_custom.prototype = {
this.grid_cell_form.custom_template.value = '';
this.grid_cell_form.custom_display_mode.value = '';
this.grid_cell_form.link_label_template.value = grid_cell.dataset.template || '';
this.grid_cell_form.link_page.value = grid_cell.dataset.page || '';
this.grid_cell_form.link_url_template.value = grid_cell.dataset.url_template || '';
this.grid_cell_form.link_display_mode.value = grid_cell.dataset.display_mode;
} else {
@ -699,6 +709,7 @@ Card_cell_custom.prototype = {
this.grid_cell_form.custom_template.value = '';
this.grid_cell_form.custom_display_mode.value = '';
this.grid_cell_form.link_label_template.value = '';
this.grid_cell_form.link_page.value = '';
this.grid_cell_form.link_url_template.value = '';
this.grid_cell_form.link_display_mode.value = '';
}
@ -718,13 +729,19 @@ Card_cell_custom.prototype = {
delete schema_cell.empty_value;
schema_cell.display_mode = form_datas.custom_display_mode;
schema_cell.template = form_datas.custom_template;
delete schema_cell.page;
delete schema_cell.url_template;
} else if (form_datas.entry_type == '@link@') {
schema_cell.varname = '@link@';
delete schema_cell.field_content;
schema_cell.display_mode = form_datas.link_display_mode;
schema_cell.template = form_datas.link_label_template;
schema_cell.url_template = form_datas.link_url_template;
schema_cell.page = form_datas.link_page;
if (form_datas.link_page) {
schema_cell.url_template = '';
} else {
schema_cell.url_template = form_datas.link_url_template;
}
} else {
schema_cell.varname = form_datas.field_varname;
schema_cell.field_content = form_datas.field_content;
@ -735,6 +752,7 @@ Card_cell_custom.prototype = {
schema_cell.empty_value = form_datas.field_empty_display_mode;
}
delete schema_cell.template;
delete schema_cell.page;
delete schema_cell.url_template;
}
schema_cell.cell_size = form_datas.cell_size;

View File

@ -1581,6 +1581,34 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
in cell_resp
)
# check with page link
root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
page1 = Page.objects.create(
title='Card',
slug='card',
template_name='standard',
sub_slug='card_model_1_id',
parent=root_page,
)
other_root_page = Page.objects.create(title='Other root', slug='other-root', template_name='standard')
page2 = Page.objects.create(
title='Card (bis)',
slug='card-bis',
template_name='standard',
sub_slug='card_model_1_id',
parent=other_root_page,
)
cell.custom_schema['cells'][0]['page'] = page1.pk
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a').attr['href'] == '/root/card/11/'
cell.custom_schema['cells'][0]['page'] = page2.pk
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a').attr['href'] == '/other-root/card-bis/11/'
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
def test_card_cell_card_mode_render_all_cards(mock_send, nocache, app):