From 1689d5a0a9d81cd035bc1a723ea91cb4bec69a2e Mon Sep 17 00:00:00 2001 From: David Burke Date: Fri, 5 Jan 2024 17:49:33 -0500 Subject: [PATCH 1/2] Remove django sites framework --- onadata/apps/api/tools.py | 3 +-- onadata/apps/main/context_processors.py | 10 +--------- .../apps/main/tests/test_custom_context_processors.py | 8 +------- onadata/libs/utils/user_auth.py | 4 ++-- onadata/settings/base.py | 3 --- 5 files changed, 5 insertions(+), 23 deletions(-) diff --git a/onadata/apps/api/tools.py b/onadata/apps/api/tools.py index 526e64247..11bd67a68 100644 --- a/onadata/apps/api/tools.py +++ b/onadata/apps/api/tools.py @@ -10,7 +10,6 @@ from django import forms from django.conf import settings from django.contrib.auth.models import User -from django.contrib.sites.models import Site from django.core.files.storage import default_storage from django.http import ( HttpResponse, @@ -245,7 +244,7 @@ def get_view_description(view_obj, html=False): Replace example.com in Django REST framework's default API description with the domain name of the current site """ - domain = Site.objects.get_current().domain + domain = settings.KOBOCAT_PUBLIC_HOSTNAME description = rest_framework_views.get_view_description(view_obj, html) # description might not be a plain string: e.g. it could be a SafeText # to prevent further HTML escaping diff --git a/onadata/apps/main/context_processors.py b/onadata/apps/main/context_processors.py index 2e2f35b5b..6e4993181 100644 --- a/onadata/apps/main/context_processors.py +++ b/onadata/apps/main/context_processors.py @@ -1,6 +1,5 @@ # coding: utf-8 from django.conf import settings -from django.contrib.sites.models import Site def google_analytics(request): @@ -15,14 +14,7 @@ def google_analytics(request): def site_name(request): - site_id = getattr(settings, 'SITE_ID', None) - try: - site = Site.objects.get(pk=site_id) - except Site.DoesNotExist: - site_name = 'example.org' - else: - site_name = site.name - return {'SITE_NAME': site_name} + return {'SITE_NAME': settings.KOBOCAT_PUBLIC_HOSTNAME} def base_url(request): diff --git a/onadata/apps/main/tests/test_custom_context_processors.py b/onadata/apps/main/tests/test_custom_context_processors.py index cd96c32b8..1545d13d7 100644 --- a/onadata/apps/main/tests/test_custom_context_processors.py +++ b/onadata/apps/main/tests/test_custom_context_processors.py @@ -1,5 +1,4 @@ # coding: utf-8 -from django.conf import settings from django.test import TestCase from onadata.apps.main.context_processors import site_name @@ -7,9 +6,4 @@ class CustomContextProcessorsTest(TestCase): def test_site_name(self): context = site_name(None) - self.assertEqual(context, {'SITE_NAME': 'example.com'}) - restore_site_id = settings.SITE_ID - settings.SITE_ID = 100 - context = site_name(None) - self.assertEqual(context, {'SITE_NAME': 'example.org'}) - settings.SITE_ID = restore_site_id + self.assertEqual(context, {'SITE_NAME': 'kc.kobotoolbox.org'}) diff --git a/onadata/libs/utils/user_auth.py b/onadata/libs/utils/user_auth.py index bfdfc7df1..90107f058 100644 --- a/onadata/libs/utils/user_auth.py +++ b/onadata/libs/utils/user_auth.py @@ -2,9 +2,9 @@ import re from functools import wraps +from django.conf import settings from django.contrib.auth import authenticate from django.contrib.auth.models import User -from django.contrib.sites.models import Site from django.http import HttpResponseRedirect, HttpResponse from django.shortcuts import get_object_or_404 from guardian.shortcuts import get_perms_for_model, assign_perm @@ -25,7 +25,7 @@ class HttpResponseNotAuthorized(HttpResponse): def __init__(self): HttpResponse.__init__(self) self['WWW-Authenticate'] =\ - 'Basic realm="%s"' % Site.objects.get_current().name + 'Basic realm="%s"' % settings.KOBOCAT_PUBLIC_HOSTNAME def check_and_set_user(request, username): diff --git a/onadata/settings/base.py b/onadata/settings/base.py index 8bad1a5ad..4a2c7a747 100644 --- a/onadata/settings/base.py +++ b/onadata/settings/base.py @@ -67,8 +67,6 @@ def skip_suspicious_operations(record): # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'en-us' -SITE_ID = env.int('DJANGO_SITE_ID', 1) - # If you set this to False, Django will make some optimizations so as not # to load the internationalization machinery. USE_I18N = True @@ -192,7 +190,6 @@ def skip_suspicious_operations(record): # https://code.djangoproject.com/ticket/10827 'django.contrib.auth', 'django.contrib.sessions', - 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', From 68726052d58c3457cff3483f8f06560711494c38 Mon Sep 17 00:00:00 2001 From: David Burke Date: Tue, 23 Jan 2024 12:02:20 -0500 Subject: [PATCH 2/2] test_site_name overrides KOBOCAT_PUBLIC_HOSTNAME in case it was changed from it's default setting --- onadata/apps/main/tests/test_custom_context_processors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/onadata/apps/main/tests/test_custom_context_processors.py b/onadata/apps/main/tests/test_custom_context_processors.py index 1545d13d7..4b4a3925b 100644 --- a/onadata/apps/main/tests/test_custom_context_processors.py +++ b/onadata/apps/main/tests/test_custom_context_processors.py @@ -1,9 +1,10 @@ # coding: utf-8 -from django.test import TestCase +from django.test import override_settings, TestCase from onadata.apps.main.context_processors import site_name class CustomContextProcessorsTest(TestCase): + @override_settings(KOBOCAT_PUBLIC_HOSTNAME="kc.kobotoolbox.org") def test_site_name(self): context = site_name(None) self.assertEqual(context, {'SITE_NAME': 'kc.kobotoolbox.org'})