From 2f96b06440955827846a2aa3344f4404622dea36 Mon Sep 17 00:00:00 2001 From: scayac Date: Sun, 15 Jun 2025 14:03:09 +0200 Subject: [PATCH] =?UTF-8?q?Exeternalisation=20de=20la=20conf=20dans=20un?= =?UTF-8?q?=20.env=20Cr=C3=A9ation=20d'un=20Dockerfile=20Mise=20=C3=A0=20j?= =?UTF-8?q?our=20du=20README=20et=20requirements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Dockerfile | 36 ++++++++++++++++++++++++++++++++++++ README.md | 18 ++++++++---------- default.env | 6 ++++++ ia_prof/settings.py | 28 ++++++++++------------------ requirements.txt | 3 ++- 6 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 Dockerfile create mode 100644 default.env diff --git a/.gitignore b/.gitignore index 1caf493..98ed7c0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ venv db.sqlite3 __pycache__ migrations +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e710345 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# Utilise une image Python officielle +FROM python:3.12-slim + +# Variables d'environnement +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# Installer les dépendances système +RUN apt-get update && apt-get install -y \ + build-essential \ + libpq-dev \ + apache2 \ + apache2-dev \ + && rm -rf /var/lib/apt/lists/* + +# Installer mod_wsgi +RUN pip install mod_wsgi + +# Créer le dossier de l'application +WORKDIR /code + +# Copier les fichiers de l'application +COPY . /code/ + +# Installer les dépendances Python +RUN pip install --upgrade pip +RUN pip install -r requirements.txt + +# Collecte des fichiers statiques +RUN python manage.py collectstatic --noinput + +# Exposer le port 8000 +EXPOSE 8000 + +# Commande de lancement avec mod_wsgi-express +CMD mod_wsgi-express start-server --port 8000 --url-alias /static /code/static /code/ia_prof/wsgi.py \ No newline at end of file diff --git a/README.md b/README.md index 79eaa29..f840f47 100644 --- a/README.md +++ b/README.md @@ -77,17 +77,15 @@ IAProf python manage.py runserver ``` -## Configuration de la clé OpenAI - -La clé API OpenAI doit être définie dans le fichier `ia_prof/settings.py` : - -```python -OPENAI_API_KEY = "votre_clé_openai" -``` +7. Créez un fichier `.env` à la racine du projet (ou copiez `default.env` puis renommez-le en `.env`) et renseignez vos variables sensibles : + ``` + DJANGO_SECRET_KEY= + OPENAI_API_KEY= + DJANGO_DEBUG=0 + DJANGO_ALLOWED_HOSTS=127.0.0.1,localhost + ``` ## Utilisation - Rendez-vous sur `http://127.0.0.1:8000/login` pour accéder à la page de connexion. -- Après connexion, vous serez redirigé vers la vue « Hello World ». - -Dans le code, utilisez-la via `settings.OPENAI_API_KEY` pour sécuriser et centraliser la configuration. \ No newline at end of file +- Après connexion, vous serez redirigé vers la vue de connexion. \ No newline at end of file diff --git a/default.env b/default.env new file mode 100644 index 0000000..38fb957 --- /dev/null +++ b/default.env @@ -0,0 +1,6 @@ +# Fichier .env pour Django (à placer à la racine du projet) + +DJANGO_SECRET_KEY= +OPENAI_API_KEY= +DJANGO_DEBUG=1 +DJANGO_ALLOWED_HOSTS=* diff --git a/ia_prof/settings.py b/ia_prof/settings.py index 59073e3..21b3417 100644 --- a/ia_prof/settings.py +++ b/ia_prof/settings.py @@ -1,25 +1,18 @@ -""" -Django settings for IAProf project. - -Generated by 'django-admin startproject' using Django 4.x. -""" - import os from pathlib import Path -# Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent +# Charger les variables d'environnement depuis un fichier .env si présent +from dotenv import load_dotenv -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/4.x/howto/deployment/checklist/ +load_dotenv() -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'your-secret-key' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent -ALLOWED_HOSTS = [] +SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', '') +OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '') +DEBUG = os.environ.get('DJANGO_DEBUG', '') == '1' +ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '').split(',') if not DEBUG else [] # Application definition @@ -112,5 +105,4 @@ STATIC_URL = '/static/' # Default primary key field type # https://docs.djangoproject.com/en/4.x/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -OPENAI_API_KEY = "your-openai-api-key" \ No newline at end of file +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index acb8323..bae0d48 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ djangorestframework==3.14.0 openai==0.27.0 PyPDF2 openai -pdfplumber \ No newline at end of file +pdfplumber +python-dotenv \ No newline at end of file