diff --git a/swh/web/archive_coverage/templates/archive-coverage.html b/swh/web/archive_coverage/templates/archive-coverage.html --- a/swh/web/archive_coverage/templates/archive-coverage.html +++ b/swh/web/archive_coverage/templates/archive-coverage.html @@ -42,7 +42,7 @@ @licend The above is the entire license notice for the JavaScript code in this page. */ - + diff --git a/swh/web/browse/templates/browse-iframe.html b/swh/web/browse/templates/browse-iframe.html --- a/swh/web/browse/templates/browse-iframe.html +++ b/swh/web/browse/templates/browse-iframe.html @@ -17,7 +17,7 @@ Software Heritage archived object - + {% render_bundle 'vendors' %} {% render_bundle 'webapp' %} {% render_bundle 'browse' %} diff --git a/swh/web/browse/urls.py b/swh/web/browse/urls.py --- a/swh/web/browse/urls.py +++ b/swh/web/browse/urls.py @@ -60,6 +60,17 @@ _browse_swhid_iframe_legacy, name="browse-swhid-iframe-legacy", ), + # keep legacy SWHID resolving URL with trailing slash for backward compatibility + url( + r"^(?P(swh|SWH):[0-9]+:[A-Za-z]+:[0-9A-Fa-f]+.*)/$", + swhid_browse, + name="browse-swhid-legacy", + ), + url( + r"^(?P(swh|SWH):[0-9]+:[A-Za-z]+:[0-9A-Fa-f]+.*)$", + swhid_browse, + name="browse-swhid", + ), ] urlpatterns += BrowseUrls.get_url_patterns() diff --git a/swh/web/settings/common.py b/swh/web/settings/common.py --- a/swh/web/settings/common.py +++ b/swh/web/settings/common.py @@ -43,9 +43,11 @@ # Application definition SWH_BASE_DJANGO_APPS = [ + "swh.web.webapp", "swh.web.auth", "swh.web.browse", "swh.web.utils", + "swh.web.tests", "swh.web.api", ] SWH_EXTRA_DJANGO_APPS = [ diff --git a/swh/web/tests/auth/test_views.py b/swh/web/tests/auth/test_views.py --- a/swh/web/tests/auth/test_views.py +++ b/swh/web/tests/auth/test_views.py @@ -22,8 +22,8 @@ check_http_get_response, check_http_post_response, ) -from swh.web.urls import _default_view as homepage_view from swh.web.utils import reverse +from swh.web.webapp.urls import default_view as homepage_view def _check_oidc_login_code_flow_data( diff --git a/swh/web/tests/test_urls.py b/swh/web/tests/test_urls.py --- a/swh/web/tests/test_urls.py +++ b/swh/web/tests/test_urls.py @@ -7,9 +7,6 @@ from django.urls import get_resolver -from swh.web.tests.helpers import check_http_get_response -from swh.web.utils import reverse - def test_swh_web_urls_have_trailing_slash(): urls = set( @@ -28,8 +25,3 @@ AssertionError, match=f"Django application {app_name} not found !" ): django_settings.SWH_DJANGO_APPS = django_settings.SWH_DJANGO_APPS + [app_name] - - -def test_stat_counters_view(client): - url = reverse("stat-counters") - check_http_get_response(client, url, status_code=200) diff --git a/swh/web/tests/webapp/__init__.py b/swh/web/tests/webapp/__init__.py new file mode 100644 diff --git a/swh/web/tests/test_templates.py b/swh/web/tests/webapp/test_templates.py rename from swh/web/tests/test_templates.py rename to swh/web/tests/webapp/test_templates.py diff --git a/swh/web/tests/webapp/test_views.py b/swh/web/tests/webapp/test_views.py new file mode 100644 --- /dev/null +++ b/swh/web/tests/webapp/test_views.py @@ -0,0 +1,31 @@ +# Copyright (C) 2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU Affero General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from django.templatetags.static import static + +from swh.web.tests.helpers import check_html_get_response, check_http_get_response +from swh.web.utils import reverse +from swh.web.webapp.urls import SWH_FAVICON + + +def test_homepage_view(client): + url = reverse("swh-web-homepage") + check_html_get_response(client, url, status_code=200, template_used="homepage.html") + + +def test_stat_counters_view(client): + url = reverse("stat-counters") + check_http_get_response(client, url, status_code=200) + + +def test_js_reverse_view(client): + url = reverse("js-reverse") + check_http_get_response(client, url, status_code=200) + + +def test_favicon_view(client): + url = reverse("favicon") + resp = check_http_get_response(client, url, status_code=301) + assert resp["location"] == static(SWH_FAVICON) diff --git a/swh/web/urls.py b/swh/web/urls.py --- a/swh/web/urls.py +++ b/swh/web/urls.py @@ -4,53 +4,19 @@ # See top-level LICENSE file for more information from importlib.util import find_spec -import json from typing import List, Union -from django_js_reverse.views import urls_js -import requests - from django.conf import settings from django.conf.urls import handler400, handler403, handler404, handler500, include from django.contrib.staticfiles.views import serve -from django.http import JsonResponse -from django.shortcuts import render from django.urls import URLPattern, URLResolver from django.urls import re_path as url -from django.views.generic.base import RedirectView -from swh.web.browse.identifiers import swhid_browse from swh.web.config import get_config -from swh.web.utils import archive, origin_visit_types from swh.web.utils.exc import swh_handle400, swh_handle403, swh_handle404, swh_handle500 swh_web_config = get_config() -favicon_view = RedirectView.as_view( - url="/static/img/icons/swh-logo-32x32.png", permanent=True -) - - -def _default_view(request): - return render(request, "homepage.html", {"visit_types": origin_visit_types()}) - - -def _stat_counters(request): - stat_counters = archive.stat_counters() - url = get_config()["history_counters_url"] - stat_counters_history = {} - - if url: - response = requests.get(url, timeout=5) - stat_counters_history = json.loads(response.text) - - counters = { - "stat_counters": stat_counters, - "stat_counters_history": stat_counters_history, - } - return JsonResponse(counters) - - urlpatterns: List[Union[URLPattern, URLResolver]] = [] # Register URLs for each SWH Django application @@ -63,25 +29,6 @@ except ModuleNotFoundError: assert False, f"Django application {app} not found !" -urlpatterns += [ - url(r"^favicon\.ico/$", favicon_view), - url(r"^$", _default_view, name="swh-web-homepage"), - url(r"^jsreverse/$", urls_js, name="js_reverse"), - # keep legacy SWHID resolving URL with trailing slash for backward compatibility - url( - r"^(?P(swh|SWH):[0-9]+:[A-Za-z]+:[0-9A-Fa-f]+.*)/$", - swhid_browse, - name="browse-swhid-legacy", - ), - url( - r"^(?P(swh|SWH):[0-9]+:[A-Za-z]+:[0-9A-Fa-f]+.*)$", - swhid_browse, - name="browse-swhid", - ), - url(r"^stat_counters/$", _stat_counters, name="stat-counters"), - url(r"^", include("swh.web.tests.urls")), -] - # allow to serve assets through django staticfiles # even if settings.DEBUG is False diff --git a/swh/web/webapp/__init__.py b/swh/web/webapp/__init__.py new file mode 100644 diff --git a/swh/web/templates/error.html b/swh/web/webapp/templates/error.html rename from swh/web/templates/error.html rename to swh/web/webapp/templates/error.html diff --git a/swh/web/templates/homepage.html b/swh/web/webapp/templates/homepage.html rename from swh/web/templates/homepage.html rename to swh/web/webapp/templates/homepage.html diff --git a/swh/web/templates/includes/favicon.html b/swh/web/webapp/templates/includes/favicon.html rename from swh/web/templates/includes/favicon.html rename to swh/web/webapp/templates/includes/favicon.html diff --git a/swh/web/templates/includes/global-modals.html b/swh/web/webapp/templates/includes/global-modals.html rename from swh/web/templates/includes/global-modals.html rename to swh/web/webapp/templates/includes/global-modals.html --- a/swh/web/templates/includes/global-modals.html +++ b/swh/web/webapp/templates/includes/global-modals.html @@ -1,3 +1,10 @@ +{% comment %} +Copyright (C) 2018-2022 The Software Heritage developers +See the AUTHORS file at the top-level directory of this distribution +License: GNU Affero General Public License version 3, or any later version +See top-level LICENSE file for more information +{% endcomment %} +