Skip to content

Commit

Permalink
Add backend index page
Browse files Browse the repository at this point in the history
* Support having static files at all

  Change fallback collected static files dir to "staticfiles/".
  This because having "static/" in .gitignore prevents including any
  static files anywhere.
* Support having static files outside apps, in argus.site
* Add dummy favicon.ico to shut up 404s in http log
* Add index page on /
* Fix flake8: only run on python files in src/

  (Not 100% bullet-proof, there is at least one tool that uses the "*.py"
  extension that does not contain executable python code.)
  • Loading branch information
hmpf authored Apr 18, 2024
1 parent cce5eed commit 8854376
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local_settings.py

# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
# in your Git repository. Update and uncomment the following line accordingly.
static/
staticfiles/

### Django.Python Stack ###
# Byte-compiled / optimized / DLL files
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repos:
- id: flake8
name: "Flake8: critical"
args: ['--count', '--select=E9,F63,F7,F82', '--show-source', '--statistics']
types: [file, python]
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/+fix-backend-index-page.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added an informational page on /, with working favicon, in order to cut down on
some common 404 log messages and set up the static files system properly.
1 change: 1 addition & 0 deletions src/argus/site/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = "/static/"
STATICFILES_DIRS = [SITE_DIR / "static"]
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"


Expand Down
2 changes: 1 addition & 1 deletion src/argus/site/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_str_env("SECRET_KEY", "secret-secret!")
STATIC_URL = get_str_env("STATIC_URL", "/static/")
STATIC_ROOT = get_str_env("STATIC_ROOT", "static/")
STATIC_ROOT = get_str_env("STATIC_ROOT", "staticfiles/")
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"


Expand Down
2 changes: 1 addition & 1 deletion src/argus/site/settings/test_CI.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_str_env("SECRET_KEY", "secret-secret!")
STATIC_URL = get_str_env("STATIC_URL", "/static/")
STATIC_ROOT = get_str_env("STATIC_ROOT", "static/")
STATIC_ROOT = get_str_env("STATIC_ROOT", "staticfiles/")
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"


Expand Down
Binary file added src/argus/site/static/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions src/argus/site/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Argus Server: {{ page_title }}</title>
</head>
<body>
<h1>Argus Server: {{ page_title }}</h1>
<ul>
<li>
<a href="{{ frontend }}">Frontend</a>
</li>
<li>
<a href="{% url 'admin:index' %}">Admin</a>
</li>
<li>OpenAPI
<ul>
<li>V1
<ul>
<li>
<a href="{% url 'v1:openapi:schema-v1' %}" title="Argus OpenAPI V1 schema file">Download schema</a>
</li>
<li>
<a href="{% url 'v1:openapi:swagger-ui-v1' %}" title="Argus OpenAPI V1 UI">UI</a>
</li>
</ul>
</li>
<li>V2
<ul>
<li>
<a href="{% url 'v2:openapi:schema' %}" title="Argus OpenAPI V2 schema file">Download schema</a>
</li>
<li>
<a href="{% url 'v2:openapi:swagger-ui' %}" title="Argus OpenAPI V2 UI">UI</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
5 changes: 4 additions & 1 deletion src/argus/site/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
"""
from django.contrib import admin
from django.urls import include, path, re_path
from django.views.generic.base import RedirectView

from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from social_django.urls import extra

from argus.auth.views import ObtainNewAuthToken, AuthMethodListView
from argus.dataporten import views as dataporten_views
from argus.notificationprofile.views import SchemaView
from argus.site.views import error, MetadataView
from argus.site.views import error, index, MetadataView


psa_urls = [
Expand All @@ -32,6 +33,7 @@
]

urlpatterns = [
path("favicon.ico", RedirectView.as_view(url="/static/favicon.ico", permanent=True)),
# path(".error/", error), # Only needed when testing error pages and error behavior
path("admin/", admin.site.urls),
path("oidc/", include(psa_urls)),
Expand All @@ -42,4 +44,5 @@
path("api/v2/", include(("argus.site.api_v2_urls", "api"), namespace="v2")),
path("api/", MetadataView.as_view(), name="metadata"),
path("json-schema/<slug:slug>", SchemaView.as_view(), name="json-schema"),
path("", index, name="api-home"),
]
6 changes: 5 additions & 1 deletion src/argus/site/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@


def index(request):
return render(request, "base.html")
context = {
"page_title": "Home",
"frontend": settings.FRONTEND_URL,
}
return render(request, "index.html", context=context)


def error(request):
Expand Down
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ exclude_lines =
[flake8]
max-line-length = 88
filename =
src/*
src/**/*.py
extend_exclude =
*.egg-info,
*.html,
*.txt,
migrations,
templates

0 comments on commit 8854376

Please sign in to comment.