From 72ad6ec081c3dd741e434748424436b216c438b6 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 14 Feb 2025 16:11:03 +0100 Subject: [PATCH] added 404 page + prepared for production setup --- .env.template | 3 +++ .gitignore | 3 ++- TimeDispatcher/settings.py | 23 +++++++++++++++-------- TimeDispatcher/urls.py | 8 ++++++-- context_processors.py | 3 ++- dispatcher/static/base.css | 6 ++++++ dispatcher/static/error.css | 20 ++++++++++++++++++++ requirements.txt | 3 ++- templates/404.html | 13 +++++++++++++ templates/base.html | 1 + 10 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 .env.template create mode 100644 dispatcher/static/error.css create mode 100644 templates/404.html diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..ff1514b --- /dev/null +++ b/.env.template @@ -0,0 +1,3 @@ +DJANGO_SECRET_KEY= +DJANGO_ENV=prod +DJANGO_HOSTS=localhost,127.0.0.1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8ad3199..c0022d5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -db.sqlite3 \ No newline at end of file +/db.sqlite3 +/.env diff --git a/TimeDispatcher/settings.py b/TimeDispatcher/settings.py index 70c0850..80f23bf 100644 --- a/TimeDispatcher/settings.py +++ b/TimeDispatcher/settings.py @@ -9,25 +9,28 @@ https://docs.djangoproject.com/en/5.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/5.1/ref/settings/ """ - +import os from pathlib import Path +from dotenv import load_dotenv + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent -APP_VERSION = "0.0.1" +APP_VERSION = "0.1.0" +load_dotenv(BASE_DIR / ".env") # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-^*x5i_#=$9kuj6k^v0cy5dqefmzo%j*i&0w93i%!zmgsa_z)2z' +SECRET_KEY = os.environ["DJANGO_SECRET_KEY"] # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.environ.get("DJANGO_ENV", "dev").lower() != "prod" -ALLOWED_HOSTS = ["localhost", "192.168.2.68"] +ALLOWED_HOSTS = list(map(lambda h: h.strip(), os.environ.get("DJANGO_HOSTS", "localhost,127.0.0.1").split(","))) # Application definition @@ -57,8 +60,7 @@ ROOT_URLCONF = 'TimeDispatcher.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [BASE_DIR / 'templates'] - , + 'DIRS': [BASE_DIR / 'templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -111,7 +113,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'Europe/Zurich' USE_I18N = True @@ -121,7 +123,12 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.1/howto/static-files/ + +STATICFILES_DIRS = [ + BASE_DIR / "dispatcher" / "static" +] STATIC_URL = 'static/' +_STATIC_ROOT = BASE_DIR / "dispatcher" / "static" # Default primary key field type # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field diff --git a/TimeDispatcher/urls.py b/TimeDispatcher/urls.py index b79ba28..10c7866 100644 --- a/TimeDispatcher/urls.py +++ b/TimeDispatcher/urls.py @@ -14,9 +14,10 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from django.contrib import admin -from django.urls import path, register_converter +from django.urls import path, register_converter, re_path +from django.views.static import serve +from TimeDispatcher import settings from TimeDispatcher.converters import DateConverter, YearMonthConverter from dispatcher import views @@ -41,3 +42,6 @@ urlpatterns = [ path("clockings//", views.set_clocking, name="set_clocking"), path("sagex///", views.set_real_sagex, name="set_real_sagex"), ] + +if not settings.DEBUG: + urlpatterns.append(re_path(r"^static/(?P.*)$", serve, {"document_root": settings._STATIC_ROOT})) \ No newline at end of file diff --git a/context_processors.py b/context_processors.py index 1a0606d..664fa0a 100644 --- a/context_processors.py +++ b/context_processors.py @@ -1,8 +1,9 @@ -from TimeDispatcher.settings import APP_VERSION +from TimeDispatcher.settings import APP_VERSION, DEBUG def version(request): return { + "debug": DEBUG, "version": APP_VERSION } diff --git a/dispatcher/static/base.css b/dispatcher/static/base.css index 03c4707..e265e2f 100644 --- a/dispatcher/static/base.css +++ b/dispatcher/static/base.css @@ -77,6 +77,12 @@ footer .sep { height: 1em; } +footer .debug { + margin-right: auto; + font-style: italic; + color: #ffb33d; +} + main { flex-grow: 1; display: flex; diff --git a/dispatcher/static/error.css b/dispatcher/static/error.css new file mode 100644 index 0000000..4fc3f53 --- /dev/null +++ b/dispatcher/static/error.css @@ -0,0 +1,20 @@ +.wrapper { + margin: auto; + max-width: 25em; + width: 100%; + text-align: center; + display: flex; + flex-direction: column; + gap: 2em; +} + +.desc { + line-height: 1.4; +} + +.url { + font-family: monospace; + background-color: rgba(255, 255, 255, 0.11); + padding: 0.1em 0.4em; + border-radius: 4px; +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index edaee23..d528fda 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Django~=5.1.5 -djangorestframework~=3.15.2 \ No newline at end of file +djangorestframework~=3.15.2 +python-dotenv~=1.0.1 \ No newline at end of file diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..955cea8 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% load static %} +{% block title %}404 Not Found{% endblock %} +{% block head %} + +{% endblock %} +{% block footer %}{% endblock %} +{% block main %} +
+

Page not found

+

Oops, page {{ request.path }} could not be found. Go back to Dashboard

+
+{% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 4ed7f19..ddb4a86 100644 --- a/templates/base.html +++ b/templates/base.html @@ -22,6 +22,7 @@ {% block main %}{% endblock %}