diff options
| author | user@node5.net <user@node5.net> | 2025-12-16 22:04:36 +0100 |
|---|---|---|
| committer | user@node5.net <user@node5.net> | 2025-12-16 22:04:36 +0100 |
| commit | 24d43660b30bd6d51da57fb1d777c8960569f980 (patch) | |
| tree | 82451c33c333516fb6f88de4a2715eb2940d783b /web_interface/django_project | |
| parent | 133e9fd4e4248de394c3a846fe15b271dbf7ac55 (diff) | |
Rename django foldermain
Diffstat (limited to 'web_interface/django_project')
| -rw-r--r-- | web_interface/django_project/ColorLogFormatter.py | 60 | ||||
| -rw-r--r-- | web_interface/django_project/__init__.py | 0 | ||||
| -rw-r--r-- | web_interface/django_project/asgi.py | 16 | ||||
| -rw-r--r-- | web_interface/django_project/settings.py | 189 | ||||
| -rw-r--r-- | web_interface/django_project/urls.py | 30 | ||||
| -rw-r--r-- | web_interface/django_project/wsgi.py | 16 |
6 files changed, 311 insertions, 0 deletions
diff --git a/web_interface/django_project/ColorLogFormatter.py b/web_interface/django_project/ColorLogFormatter.py new file mode 100644 index 0000000..939d591 --- /dev/null +++ b/web_interface/django_project/ColorLogFormatter.py @@ -0,0 +1,60 @@ +import logging + + +class ColorFormatter(logging.Formatter): + grey = "\x1b[90;20m" + cyan = "\x1b[96;20m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format = "%(asctime)s,%(msecs)03d %(levelname)-8s %(filename)s:%(lineno)d %(message)s" + + FORMATS = { + logging.DEBUG: grey + format + reset, + logging.INFO: cyan + format + reset, + logging.WARNING: yellow + format + reset, + logging.ERROR: red + format + reset, + logging.CRITICAL: bold_red + format + reset, + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + formatted = formatter.format(record) + return formatted + + +class SysLogColorFormatter(logging.Formatter): + grey = "\x1b[90;20m" + cyan = "\x1b[96;20m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format = "%(levelname)-8s %(filename)s:%(lineno)d %(message)s" + + FORMATS = { + logging.DEBUG: grey + format + reset, + logging.INFO: cyan + format + reset, + logging.WARNING: yellow + format + reset, + logging.ERROR: red + format + reset, + logging.CRITICAL: bold_red + format + reset, + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + formatted = formatter.format(record) + return formatted + + +""" Example how to log in other modules: +logger = logging.getLogger(__name__) # Instantiate a logger to be used in this module + +logger.debug("debug message") +logger.info("info message") +logger.warning("warning message") +logger.error("error message") +logger.critical("critical message") +""" diff --git a/web_interface/django_project/__init__.py b/web_interface/django_project/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/web_interface/django_project/__init__.py diff --git a/web_interface/django_project/asgi.py b/web_interface/django_project/asgi.py new file mode 100644 index 0000000..c4af364 --- /dev/null +++ b/web_interface/django_project/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for drinks_machine project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drinks_machine.settings') + +application = get_asgi_application() diff --git a/web_interface/django_project/settings.py b/web_interface/django_project/settings.py new file mode 100644 index 0000000..4f23ce3 --- /dev/null +++ b/web_interface/django_project/settings.py @@ -0,0 +1,189 @@ +""" +Django settings for drinks-machine project. + +Generated by 'django-admin startproject' using Django 6.0. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/6.0/ref/settings/ +""" + +from pathlib import Path +import os +import sys + +from django.core.management.utils import get_random_secret_key + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! + +# Define the path to your secret key file inside the .secrets folder +secret_key_file = os.path.join(BASE_DIR, '.secrets', 'secret_key.txt') + +# Ensure the .secrets directory exists +os.makedirs(os.path.dirname(secret_key_file), exist_ok=True) + +# Check if the file exists +if not os.path.exists(secret_key_file): + # Generate a new secret key + secret_key = get_random_secret_key() + + # Save the secret key to the file + with open(secret_key_file, 'w') as f: + f.write(secret_key) +else: + # Read the secret key from the file + with open(secret_key_file, 'r') as f: + secret_key = f.read().strip() + +# Set the SECRET_KEY setting +SECRET_KEY = secret_key + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = "DEBUG" in os.environ and os.environ["DEBUG"] == "INSECURE" +print(f"Debug is: {'on' if DEBUG else 'off'}") + +ALLOWED_HOSTS = ['127.0.0.1', 'drinks-machine.node5.net'] + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'drinks', + 'colorfield', + 'motor_controls', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'django_project.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'django_project.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/6.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/6.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/6.0/howto/static-files/ + +STATIC_URL = '/static/' +#STATIC_ROOT = os.path.join(BASE_DIR, "static") +STATICFILES_DIRS = [BASE_DIR / "static"] + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "handlers": { + "console": { + "stream": sys.stdout, + "class": "logging.StreamHandler", + "formatter": "verbose", + }, + "syslog": { + "address": "/dev/log", + "class": "logging.handlers.SysLogHandler", + "formatter": "verbose", + }, + }, + "root": { + "handlers": ["console" if DEBUG is True else "syslog"], + "level": "DEBUG", + }, + "loggers": { + "django": { + "handlers": ["console" if DEBUG is True else "syslog"], + "level": "INFO", + "propagate": False, + }, + }, + "formatters": { + "verbose": { + "()": ( + "django_project.ColorLogFormatter.ColorFormatter" + if DEBUG is True + else "django_project.ColorLogFormatter.SysLogColorFormatter" + ), + "format": "{asctime:s} {levelname:8s} {filename}:{lineno:d} \t {message}", + "style": "{", + }, + }, +} diff --git a/web_interface/django_project/urls.py b/web_interface/django_project/urls.py new file mode 100644 index 0000000..aaafc65 --- /dev/null +++ b/web_interface/django_project/urls.py @@ -0,0 +1,30 @@ +""" +URL configuration for drinks_machine project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/6.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +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, include +from django.views.generic import RedirectView +from django.conf.urls.static import static +from django.conf import settings + +urlpatterns = [ + path('admin/', admin.site.urls), + path("drinks/", include("drinks.urls")), + path('', RedirectView.as_view(url='/drinks/', permanent=False), name='redirect'), + path("motor-controls/", include("motor_controls.urls")), +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \ + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + diff --git a/web_interface/django_project/wsgi.py b/web_interface/django_project/wsgi.py new file mode 100644 index 0000000..a5d2224 --- /dev/null +++ b/web_interface/django_project/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for drinks_machine project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drinks_machine.settings') + +application = get_wsgi_application() |
