simplified environment variables management

This commit is contained in:
Faraphel 2024-01-04 00:18:52 +01:00
parent 2f2332766e
commit a1b325b653
8 changed files with 61 additions and 8 deletions

7
.gitignore vendored
View file

@ -1,5 +1,12 @@
# IDE
.idea .idea
# Virtual Environment
venv venv
.venv
# Django
media media
static-collected static-collected
db.sqlite3 db.sqlite3
.env

View file

@ -7,10 +7,11 @@ For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
""" """
import os import dotenv
from django.core.asgi import get_asgi_application 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() application = get_asgi_application()

View file

@ -7,10 +7,12 @@ For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
""" """
import os
import dotenv
from django.core.wsgi import get_wsgi_application 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() application = get_wsgi_application()

View file

@ -1 +1,15 @@
# M1-Projet-Serveur # 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`.

View file

@ -1,12 +1,17 @@
#!/usr/bin/env python #!/usr/bin/env python
"""Django's command-line utility for administrative tasks.""" """Django's command-line utility for administrative tasks."""
import os
import sys import sys
import dotenv
from utils import env
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Palto.settings') dotenv.load_dotenv(env.create_dotenv())
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:

View file

@ -13,3 +13,4 @@ factory_boy
# Other librairies # Other librairies
markdown markdown
python-dotenv

0
utils/__init__.py Normal file
View file

23
utils/env.py Normal file
View file

@ -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