Skip to content

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

Add Django UI admin notice for next staging database restore (#2308)

Madison Swain-Bowden3bf7a47719a9
diff --git a/.prettierignore b/.prettierignore
index 5609ac7be..845e14758 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -16,5 +16,8 @@ api/catalog/templates/drf-yasg/redoc.html
api/docs/_templates/sidebar/brand.html
api/docs/_templates/page.html
+# Django templates
+api/api/templates
+
# Autogenerated
pnpm-lock.yaml
diff --git a/api/api/templates/admin/base_site.html b/api/api/templates/admin/base_site.html
new file mode 100644
index 000000000..f1c979e1b
--- /dev/null
+++ b/api/api/templates/admin/base_site.html
@@ -0,0 +1,24 @@
+{% extends "admin/base_site.html" %}
+
+{% block messages %}
+ {% if ENVIRONMENT != 'production' %}
+ <script>
+ function calculateDaysLeft() {
+ let dateToday = new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), new Date().getUTCDate()));
+ let lastDayOfMonth = new Date(Date.UTC(dateToday.getUTCFullYear(), dateToday.getUTCMonth() + 1, 0)).getUTCDate();
+ let daysUntilEndOfMonth = lastDayOfMonth - dateToday.getUTCDate();
+ document.getElementById("staging-db-refresh-days").innerText = daysUntilEndOfMonth;
+ }
+ document.addEventListener("DOMContentLoaded", calculateDaysLeft);
+ </script>
+ <ul class="messagelist">{% for message in messages %}
+ <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
+ {% endfor %}
+ <li class="warning">Next staging database restore will occur in <span id="staging-db-refresh-days"></span> days.
+ <a href="https://airflow.openverse.engineering/dags/staging_database_restore/grid">
+ View the DAG for information on how to skip this process.
+ </a>
+ </li>
+ </ul>
+ {% else %}{{ block.super }}{% endif %}
+{% endblock %}
diff --git a/api/conf/context_processors.py b/api/conf/context_processors.py
new file mode 100644
index 000000000..08deac3de
--- /dev/null
+++ b/api/conf/context_processors.py
@@ -0,0 +1,6 @@
+from conf.settings.base import ENVIRONMENT
+
+
+def export_environment(request):
+ """Export the environment to the template context."""
+ return {"ENVIRONMENT": ENVIRONMENT}
diff --git a/api/conf/settings/base.py b/api/conf/settings/base.py
index c49dcc3a0..903b1fc50 100644
--- a/api/conf/settings/base.py
+++ b/api/conf/settings/base.py
@@ -1,3 +1,10 @@
+from decouple import config
+
+from conf.settings import BASE_DIR
+
+
+ENVIRONMENT = config("ENVIRONMENT", default="local")
+
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
@@ -36,7 +43,9 @@ ROOT_URLCONF = "conf.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
- "DIRS": [],
+ "DIRS": [
+ BASE_DIR / "api" / "templates",
+ ],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
@@ -44,6 +53,8 @@ TEMPLATES = [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
+ # Make the ENVIRONMENT env var available for all contexts
+ "conf.context_processors.export_environment",
],
},
},
diff --git a/api/conf/settings/sentry.py b/api/conf/settings/sentry.py
index 3be8a16ae..ec816498b 100644
--- a/api/conf/settings/sentry.py
+++ b/api/conf/settings/sentry.py
@@ -3,13 +3,13 @@ from decouple import config
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger
+from conf.settings.base import ENVIRONMENT
+
SENTRY_DSN = config("SENTRY_DSN", default="")
SENTRY_SAMPLE_RATE = config("SENTRY_SAMPLE_RATE", default=1.0, cast=float)
-ENVIRONMENT = config("ENVIRONMENT", default="local")
-
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config("DJANGO_DEBUG_ENABLED", default=False, cast=bool)
if not DEBUG and SENTRY_DSN:
diff --git a/api/test/unit/templates/test_admin_base_html.py b/api/test/unit/templates/test_admin_base_html.py
new file mode 100644
index 000000000..f141b7451
--- /dev/null
+++ b/api/test/unit/templates/test_admin_base_html.py
@@ -0,0 +1,43 @@
+from django.shortcuts import resolve_url
+from django.test.client import RequestFactory
+
+import pytest
+
+from api.admin.site import openverse_admin
+
+
+class MockUser:
+ is_active = True
+ is_staff = True
+ pk = 1
+
+ def has_perm(self, *args):
+ return True
+
+ def has_module_perms(self, *args):
+ return True
+
+
+ "environment_value, should_be_present",
+ [
+ ("staging", True),
+ ("local", True),
+ ("production", False),
+ ],
+)
+def test_admin_base_html_renders_message(environment_value, should_be_present, db):
+ url = resolve_url("admin:index")
+ request = RequestFactory().get(url)
+ request.user = MockUser()
+
+ index = openverse_admin.index(request)
+ index.context_data["ENVIRONMENT"] = environment_value
+ html = index.rendered_content
+
+ assert (
+ "Next staging database restore will occur in" in html
+ ) == should_be_present, (
+ "Rendered message about staging database restore was missing or "
+ "incorrectly present in Admin UI"
+ )