From 058205f8f1dae4e721e2248d18efd801fcd7726b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 10 Jun 2018 10:02:54 +0200 Subject: [PATCH 1/2] pwa: add possibility to use a template for manifest.json (#24400) --- combo/apps/pwa/__init__.py | 26 ++++++++++++++++++++++++++ combo/apps/pwa/urls.py | 21 +++++++++++++++++++++ combo/apps/pwa/views.py | 25 +++++++++++++++++++++++++ combo/settings.py | 1 + tests/templates-1/combo/manifest.json | 3 +++ tests/test_pwa.py | 15 +++++++++++++++ 6 files changed, 91 insertions(+) create mode 100644 combo/apps/pwa/__init__.py create mode 100644 combo/apps/pwa/urls.py create mode 100644 combo/apps/pwa/views.py create mode 100644 tests/templates-1/combo/manifest.json create mode 100644 tests/test_pwa.py diff --git a/combo/apps/pwa/__init__.py b/combo/apps/pwa/__init__.py new file mode 100644 index 0000000..8fec7cf --- /dev/null +++ b/combo/apps/pwa/__init__.py @@ -0,0 +1,26 @@ +# combo - content management system +# Copyright (C) 2015-2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import django.apps + +class AppConfig(django.apps.AppConfig): + name = 'combo.apps.pwa' + + def get_before_urls(self): + from . import urls + return urls.urlpatterns + +default_app_config = 'combo.apps.pwa.AppConfig' diff --git a/combo/apps/pwa/urls.py b/combo/apps/pwa/urls.py new file mode 100644 index 0000000..5842d45 --- /dev/null +++ b/combo/apps/pwa/urls.py @@ -0,0 +1,21 @@ +# combo - content management system +# Copyright (C) 2015-2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.conf.urls import url + +from .views import manifest_json + +urlpatterns = [url('^manifest.json', manifest_json)] diff --git a/combo/apps/pwa/views.py b/combo/apps/pwa/views.py new file mode 100644 index 0000000..fe96996 --- /dev/null +++ b/combo/apps/pwa/views.py @@ -0,0 +1,25 @@ +# combo - content management system +# Copyright (C) 2015-2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.http import HttpResponse, Http404 +from django.template.loader import get_template, TemplateDoesNotExist + +def manifest_json(request, *args, **kwargs): + try: + template = get_template('combo/manifest.json') + except TemplateDoesNotExist: + raise Http404() + return HttpResponse(template.render({}, request), content_type='application/json') diff --git a/combo/settings.py b/combo/settings.py index c261f49..3421e8f 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -76,6 +76,7 @@ INSTALLED_APPS = ( 'combo.apps.usersearch', 'combo.apps.maps', 'combo.apps.calendar', + 'combo.apps.pwa', 'haystack', 'xstatic.pkg.josefinsans', 'xstatic.pkg.leaflet', diff --git a/tests/templates-1/combo/manifest.json b/tests/templates-1/combo/manifest.json new file mode 100644 index 0000000..600e5f1 --- /dev/null +++ b/tests/templates-1/combo/manifest.json @@ -0,0 +1,3 @@ +{ + "name": "test" +} diff --git a/tests/test_pwa.py b/tests/test_pwa.py new file mode 100644 index 0000000..19b17d5 --- /dev/null +++ b/tests/test_pwa.py @@ -0,0 +1,15 @@ +import os +import pytest + +from django.conf import settings +from django.test import override_settings + +pytestmark = pytest.mark.django_db + +def test_manifest_json(app): + app.get('/manifest.json', status=404) + + templates_settings = [settings.TEMPLATES[0].copy()] + templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))] + with override_settings(TEMPLATES=templates_settings): + assert app.get('/manifest.json', status=200).json['name'] == 'test' -- 2.17.1