pwa: allow absolute redirection url from internal page (#56974)

This commit is contained in:
Nicolas Roche 2021-09-15 16:40:25 +02:00 committed by Frédéric Péters
parent 541aed2379
commit eb11afca1b
3 changed files with 49 additions and 4 deletions

View File

@ -31,6 +31,7 @@ from py_vapid import Vapid
from combo import utils
from combo.data.fields import RichTextField
from combo.middleware import get_request
class PwaSettings(models.Model):
@ -137,9 +138,12 @@ class PwaNavigationEntry(models.Model):
def get_url(self):
if self.link_page:
return self.link_page.get_online_url()
url = self.link_page.get_online_url()
else:
return utils.get_templated_url(self.url)
url = utils.get_templated_url(self.url)
if get_request():
url = get_request().build_absolute_uri(url)
return url
def css_class_names(self):
css_class_names = self.extra_css_class or ''

View File

@ -7,7 +7,7 @@
<li class="{{ entry.css_class_names }}{% if entry.link_page in page.get_parents_and_self %} selected{% endif %}" data-entry-pk="{{ entry.pk }}"
{% if entry.notification_count %}data-notification-count-url="{{site_base}}/api/notification/count/"{% endif %}
{% if entry.use_user_name_as_label %}data-include-user-name{% endif %}>
<a href="{% if entry.link_page_id %}{{ site_base }}{% endif %}{{ entry.get_url }}"
<a href="{{ entry.get_url }}"
{% if entry.icon %}style="background-image: url({{site_base}}{{entry.icon.url}});"{% endif %}
><span>{{ entry.get_label }}</span></a></li>
{% endfor %}
@ -36,4 +36,4 @@ $('body.authenticated-user li[data-notification-count-url]').each(function(idx,
}});
});
</script>
{% endif %}
{% endif %}

View File

@ -1,4 +1,5 @@
import base64
from html.parser import HTMLParser
from unittest import mock
import pytest
@ -253,6 +254,46 @@ def test_pwa_navigation_templatetag(app):
assert 'data-pwa-user-name="{% block placeholder-user-name %}' in nav
def test_pwa_navigation_templatetag_internal_redirection(app):
page1 = Page(title='One', slug='one')
page2 = Page(title='Two', slug='two', redirect_url='http://www.example.org/test')
page3 = Page(title='Three', slug='three', redirect_url='../test')
page4 = Page(title='Four', slug='four', redirect_url='{{test_url}}plop')
page1.save()
page2.save()
page3.save()
page4.save()
entry1 = PwaNavigationEntry(link_page=page1, order=1)
entry2 = PwaNavigationEntry(link_page=page2, order=2)
entry3 = PwaNavigationEntry(link_page=page3, order=3)
entry4 = PwaNavigationEntry(link_page=page4, order=4)
entry1.save()
entry2.save()
entry3.save()
entry4.save()
t = Template('{% load pwa %}{% pwa_navigation %}')
with override_settings(TEMPLATE_VARS={'pwa_display': 'standalone'}):
request = RequestFactory().get('/')
with mock.patch('combo.apps.pwa.models.get_request', return_value=request):
nav = t.render(Context({'request': request}))
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag == 'a':
results.append(attrs[0][1])
parser = MyHTMLParser()
results = []
parser.feed(nav)
assert results == [
'http://testserver/one/',
'http://www.example.org/test',
'http://testserver/three/',
'http://testserver/four/',
]
def test_pwa_application_icon(app, admin_user):
app = login(app)
with override_settings(TEMPLATE_VARS={'pwa_display': 'standalone'}):