From 89cadedfc9d5bc0a594affde17df0491619e3cc3 Mon Sep 17 00:00:00 2001 From: Jelle van der Waa Date: Sat, 18 Jan 2025 13:15:15 +0100 Subject: [PATCH] Fix domain being None in opensearch results The domain came from HTTP_HOST which in our nginx configuration is not set, furthermore other code already uses Site to obtain the domain. Closes: #541 --- packages/tests/test_search.py | 1 + packages/views/__init__.py | 5 +++-- public/views.py | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/tests/test_search.py b/packages/tests/test_search.py index 88fed036..2915a630 100644 --- a/packages/tests/test_search.py +++ b/packages/tests/test_search.py @@ -67,6 +67,7 @@ def test_sort(client, package): def test_packages(client, package): response = client.get('/opensearch/packages/') assert response.status_code == 200 + assert 'template="example.com/opensearch/packages/"' in response.content.decode() def test_packages_suggest(client, package): diff --git a/packages/views/__init__.py b/packages/views/__init__.py index f3b39247..50c27839 100644 --- a/packages/views/__init__.py +++ b/packages/views/__init__.py @@ -5,6 +5,7 @@ from django.contrib import messages from django.contrib.auth.decorators import permission_required from django.contrib.auth.models import User +from django.contrib.sites.models import Site from django.core.cache import cache from django.db.models import Q from django.http import HttpResponse, HttpResponseBadRequest @@ -21,10 +22,10 @@ @require_safe @cache_control(public=True, max_age=86400) def opensearch(request): - domain = "%s://%s" % (request.scheme, request.META.get('HTTP_HOST')) + current_site = Site.objects.get_current() return render(request, 'packages/opensearch.xml', - {'domain': domain}, + {'domain': current_site.domain}, content_type='application/opensearchdescription+xml') diff --git a/public/views.py b/public/views.py index 196b5ac4..17e39971 100644 --- a/public/views.py +++ b/public/views.py @@ -3,6 +3,7 @@ from operator import attrgetter from django.contrib.auth.models import User +from django.contrib.sites.models import Site from django.db.models import Count, Q from django.http import HttpResponse from django.shortcuts import get_object_or_404, render @@ -25,12 +26,12 @@ def updates(): else: def updates(): return get_recent_updates() - domain = "%s://%s" % (request.scheme, request.META.get('HTTP_HOST')) + current_site = Site.objects.get_current() context = { 'news_updates': News.objects.order_by('-postdate', '-id')[:15], 'pkg_updates': updates, 'staff_groups': StaffGroup.objects.all(), - 'domain': domain, + 'domain': current_site.domain, } return render(request, 'public/index.html', context)