Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Re-add accidentally removed CORS middleware (#2075)

Dhruv Bhanushali962c556ed864
diff --git a/api/conf/settings/base.py b/api/conf/settings/base.py
index d3203f94a..c49dcc3a0 100644
--- a/api/conf/settings/base.py
+++ b/api/conf/settings/base.py
@@ -6,7 +6,6 @@ INSTALLED_APPS = [
"django.contrib.messages",
"django.contrib.staticfiles",
# Third-party installed apps, more can be added in other settings files.
- "corsheaders",
"sslserver",
]
diff --git a/api/conf/settings/logging.py b/api/conf/settings/logging.py
index de60a8ca8..495827bd7 100644
--- a/api/conf/settings/logging.py
+++ b/api/conf/settings/logging.py
@@ -115,6 +115,8 @@ if DJANGO_DB_LOGGING:
if not DEBUG:
# WARNING: Do not run in production long-term as it can impact performance.
- MIDDLEWARE.append(
- "api.middleware.force_debug_cursor_middleware.force_debug_cursor_middleware" # noqa: E501
+ middleware = (
+ "api.middleware.force_debug_cursor_middleware.force_debug_cursor_middleware"
)
+ if middleware not in MIDDLEWARE:
+ MIDDLEWARE.append(middleware)
diff --git a/api/conf/settings/oauth2.py b/api/conf/settings/oauth2.py
index ca09cc399..37d851401 100644
--- a/api/conf/settings/oauth2.py
+++ b/api/conf/settings/oauth2.py
@@ -3,13 +3,12 @@ from decouple import config
from conf.settings.base import INSTALLED_APPS, MIDDLEWARE
-INSTALLED_APPS += [
- "oauth2_provider",
-]
+if "oauth2_provider" not in INSTALLED_APPS:
+ INSTALLED_APPS.append("oauth2_provider")
-MIDDLEWARE += [
- "oauth2_provider.middleware.OAuth2TokenMiddleware",
-]
+middleware = "oauth2_provider.middleware.OAuth2TokenMiddleware"
+if middleware not in MIDDLEWARE:
+ MIDDLEWARE.append(middleware)
OAUTH2_PROVIDER = {
"SCOPES": {
diff --git a/api/conf/settings/openverse.py b/api/conf/settings/openverse.py
index 026311538..ff716941d 100644
--- a/api/conf/settings/openverse.py
+++ b/api/conf/settings/openverse.py
@@ -1,6 +1,5 @@
from conf.settings.base import INSTALLED_APPS
-INSTALLED_APPS += [
- "api",
-]
+if "api" not in INSTALLED_APPS:
+ INSTALLED_APPS.append("api")
diff --git a/api/conf/settings/rest_framework.py b/api/conf/settings/rest_framework.py
index 1ced0971b..b994d45c1 100644
--- a/api/conf/settings/rest_framework.py
+++ b/api/conf/settings/rest_framework.py
@@ -3,9 +3,8 @@ from decouple import config
from conf.settings.base import INSTALLED_APPS
-INSTALLED_APPS += [
- "rest_framework",
-]
+if "rest_framework" not in INSTALLED_APPS:
+ INSTALLED_APPS.append("rest_framework")
THROTTLE_ANON_BURST = config("THROTTLE_ANON_BURST", default="5/hour")
diff --git a/api/conf/settings/s3.py b/api/conf/settings/s3.py
index c21078c86..bcbef2b3c 100644
--- a/api/conf/settings/s3.py
+++ b/api/conf/settings/s3.py
@@ -10,9 +10,8 @@ USE_S3 = config("USE_S3", default=False, cast=bool)
if USE_S3:
- INSTALLED_APPS += [
- "storages",
- ]
+ if "storages" not in INSTALLED_APPS:
+ INSTALLED_APPS.append("storages")
STORAGES["default"]["BACKEND"] = "storages.backends.s3boto3.S3Boto3Storage"
diff --git a/api/conf/settings/security.py b/api/conf/settings/security.py
index af5bdef54..221829997 100644
--- a/api/conf/settings/security.py
+++ b/api/conf/settings/security.py
@@ -2,6 +2,8 @@ from socket import gethostbyname, gethostname
from decouple import config
+from conf.settings.base import INSTALLED_APPS, MIDDLEWARE
+
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
@@ -35,6 +37,13 @@ BASE_URL = config("BASE_URL", default="https://openverse.org/")
CSRF_TRUSTED_ORIGINS = ["https://*.openverse.engineering"]
# Allow anybody to access the API from any domain
+if "corsheaders" not in INSTALLED_APPS:
+ INSTALLED_APPS.append("corsheaders")
+
+middleware = "corsheaders.middleware.CorsMiddleware"
+if middleware not in MIDDLEWARE:
+ MIDDLEWARE.insert(0, middleware)
+
CORS_ORIGIN_ALLOW_ALL = True
# Proxy handling, for production
diff --git a/api/conf/settings/spectacular.py b/api/conf/settings/spectacular.py
index 690750350..e1165db07 100644
--- a/api/conf/settings/spectacular.py
+++ b/api/conf/settings/spectacular.py
@@ -4,9 +4,8 @@ from conf.settings.base import INSTALLED_APPS
from conf.settings.misc import API_VERSION
-INSTALLED_APPS += [
- "drf_spectacular",
-]
+if "drf_spectacular" not in INSTALLED_APPS:
+ INSTALLED_APPS.append("drf_spectacular")
SPECTACULAR_SETTINGS = {
"TITLE": "Openverse API",