From a1b325b653019f8902867bfdf9703c23e3c2556b Mon Sep 17 00:00:00 2001 From: Faraphel Date: Thu, 4 Jan 2024 00:18:52 +0100 Subject: [PATCH] simplified environment variables management --- .gitignore | 7 +++++++ Palto/asgi.py | 7 ++++--- Palto/wsgi.py | 6 ++++-- README.md | 16 +++++++++++++++- manage.py | 9 +++++++-- requirements.txt | 1 + utils/__init__.py | 0 utils/env.py | 23 +++++++++++++++++++++++ 8 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/env.py diff --git a/.gitignore b/.gitignore index f910067..39766e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,12 @@ +# IDE .idea + +# Virtual Environment venv +.venv + +# Django media static-collected db.sqlite3 +.env diff --git a/Palto/asgi.py b/Palto/asgi.py index 972e6b9..e81ba04 100644 --- a/Palto/asgi.py +++ b/Palto/asgi.py @@ -7,10 +7,11 @@ For more information on this file, see https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ """ -import os - +import dotenv from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Palto.settings') +from utils import env + +dotenv.load_dotenv(env.create_dotenv()) application = get_asgi_application() diff --git a/Palto/wsgi.py b/Palto/wsgi.py index 9133ba7..1d5c335 100644 --- a/Palto/wsgi.py +++ b/Palto/wsgi.py @@ -7,10 +7,12 @@ For more information on this file, see https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ """ -import os +import dotenv from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Palto.settings') +from utils import env + +dotenv.load_dotenv(env.create_dotenv()) application = get_wsgi_application() diff --git a/README.md b/README.md index d72fb01..507f27a 100644 --- a/README.md +++ b/README.md @@ -1 +1,15 @@ -# M1-Projet-Serveur \ No newline at end of file +# Palto-Server + +(This is a project realised for our University, it will not be maintained after and it should not be used outside of testing) + +Palto is a project to check students attendances at their school classes. +It allows teachers to create sessions containing students that should be present. +They can then scan their student card with the NFC technology and they will be automatically marked as present to +this session. + +# Installation + +1. Install `python >= 3.11` +2. Create a virtual environment with `python -m venv ./.venv/`. The next steps will be inside it. +3. Install the dependencies with `python -m pip install -r ./requirements.txt`. +4. Run the program by with `python ./manage.py runserver`. diff --git a/manage.py b/manage.py index 6203449..3dae112 100644 --- a/manage.py +++ b/manage.py @@ -1,12 +1,17 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" -import os + import sys +import dotenv + +from utils import env + def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Palto.settings') + dotenv.load_dotenv(env.create_dotenv()) + try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/requirements.txt b/requirements.txt index 7207d04..0e80e38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ factory_boy # Other librairies markdown +python-dotenv diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/env.py b/utils/env.py new file mode 100644 index 0000000..de4c4af --- /dev/null +++ b/utils/env.py @@ -0,0 +1,23 @@ +from pathlib import Path + +from django.core.management.utils import get_random_secret_key + + +path_dotenv = Path("./.env") + + +def create_dotenv(force: bool = False) -> Path: + # if not forced and the file already exist, ignore + if not force and path_dotenv.exists(): + return path_dotenv + + # otherwise create the file + path_dotenv.write_text( + ( + f"DJANGO_SETTINGS_MODULE='Palto.settings'\n" + f"DJANGO_SECRET={get_random_secret_key()!r}\n" + ), + encoding="utf-8" + ) + + return path_dotenv