cells: option to bypass LinkCell url validity check (#62136)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Lauréline Guérin 2022-03-08 10:50:28 +01:00
parent 71be7b1d86
commit bed3ff7ed8
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 58 additions and 12 deletions

View File

@ -52,7 +52,7 @@ class LinkCellForm(forms.ModelForm):
class LinkCellForLinkListCellForm(LinkCellForm):
class Meta:
model = LinkCell
fields = ('title', 'url', 'link_page', 'anchor', 'extra_css_class')
fields = ('title', 'url', 'link_page', 'anchor', 'bypass_url_validity_check', 'extra_css_class')
class LinkListCellForm(forms.ModelForm):

View File

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('data', '0053_page_variables'),
]
operations = [
migrations.AddField(
model_name='linkcell',
name='bypass_url_validity_check',
field=models.BooleanField(default=False, verbose_name='No URL validity check'),
),
]

View File

@ -1513,6 +1513,7 @@ class LinkCell(CellBase):
verbose_name=_('Internal link'),
)
anchor = models.CharField(_('Anchor'), max_length=150, blank=True)
bypass_url_validity_check = models.BooleanField(_('No URL validity check'), default=False)
default_template_name = 'combo/link-cell.html'
add_as_link_label = _('add a link')
@ -1608,6 +1609,18 @@ class LinkCell(CellBase):
return LinkCellForLinkListCellForm
def get_manager_tabs(self):
tabs = super().get_manager_tabs()
tabs.insert(
1,
{
'slug': 'advanced',
'name': _('Advanced'),
'fields': ['bypass_url_validity_check'],
},
)
return tabs
def render_for_search(self):
return ''
@ -1620,6 +1633,9 @@ class LinkCell(CellBase):
return []
def check_validity(self):
if self.bypass_url_validity_check:
self.mark_as_valid()
return
if self.link_page_id:
self.mark_as_valid()
return

View File

@ -163,18 +163,23 @@ def test_link_cell():
assert cell.render(ctx).strip() == '<a href="http://example.net/#anchor">altertitle</a>'
def test_link_cell_validity(settings):
@pytest.mark.parametrize('bypass', [True, False])
def test_link_cell_validity(settings, bypass):
page = Page.objects.create(title='example page', slug='example-page')
cell = LinkCell.objects.create(
page=page,
title='Example Site',
order=0,
bypass_url_validity_check=bypass,
)
# no link defined
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'data_url_not_defined'
assert validity_info.invalid_since is not None
if bypass:
assert ValidityInfo.objects.exists() is False
else:
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'data_url_not_defined'
assert validity_info.invalid_since is not None
# no link defined but anchor set
cell.anchor = 'plop'
@ -197,9 +202,12 @@ def test_link_cell_validity(settings):
mock_json = mock.Mock(status_code=404)
requests_get.return_value = mock_json
cell.save()
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'data_url_not_found'
assert validity_info.invalid_since is not None
if bypass:
assert ValidityInfo.objects.exists() is False
else:
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'data_url_not_found'
assert validity_info.invalid_since is not None
with mock.patch('combo.data.models.requests.get') as requests_get:
mock_json = mock.Mock(status_code=200)
@ -217,9 +225,12 @@ def test_link_cell_validity(settings):
mock_json = mock.Mock(status_code=400)
requests_get.return_value = mock_json
cell.save()
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'data_url_invalid'
assert validity_info.invalid_since is not None
if bypass:
assert ValidityInfo.objects.exists() is False
else:
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'data_url_invalid'
assert validity_info.invalid_since is not None
# external link with a single variable
cell.link_page = None
@ -236,7 +247,10 @@ def test_link_cell_validity(settings):
mock_json = mock.Mock(status_code=404)
requests_get.return_value = mock_json
cell.save()
assert ValidityInfo.objects.exists() is True
if bypass:
assert ValidityInfo.objects.exists() is False
else:
assert ValidityInfo.objects.exists() is True
# external link with two variables
settings.TEMPLATE_VARS = {'var1': 'foo', 'var2': 'bar'}