From 3efa2177161ccb782c8fea9464ebec3c93129b9e Mon Sep 17 00:00:00 2001 From: krek0 Date: Fri, 24 Apr 2026 21:42:23 +0200 Subject: [PATCH] Load settings from .env file. --- .env.example | 16 ++++++++++++++++ photo21/settings.py | 19 +++++++------------ photologue/models.py | 8 +++++++- requirements.txt | 1 + 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..145b31e --- /dev/null +++ b/.env.example @@ -0,0 +1,16 @@ +# Copy this file to .env and fill in the values +# .env is gitignored and must never be committed + +SECRET_KEY=change-me-to-a-long-random-string + +# Set to True only for development +DEBUG=False + +# Comma-separated list of additional allowed hosts (beyond 127.0.0.1 and localhost) +EXTRA_HOSTS=photos.crans.org,photos-dev.crans.org + +# Comma-separated list of admins in "Name:email" format +ADMINS=admin:photos-admin@lists.crans.org + +# Email address used as sender for server emails +SERVER_EMAIL=photos@crans.org diff --git a/photo21/settings.py b/photo21/settings.py index 7f86536..2436141 100644 --- a/photo21/settings.py +++ b/photo21/settings.py @@ -14,6 +14,7 @@ https://docs.djangoproject.com/en/2.2/ref/settings/ import os +from decouple import Csv, config from django.contrib.messages import constants as messages from django.utils.translation import gettext_lazy as _ @@ -25,17 +26,12 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "CHANGE ME" +SECRET_KEY = config("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = False +DEBUG = config("DEBUG", default=False, cast=bool) -ALLOWED_HOSTS = [ - "127.0.0.1", - "localhost", - "photos.crans.org", - "photos-dev.crans.org", -] +ALLOWED_HOSTS = ["127.0.0.1", "localhost"] + config("EXTRA_HOSTS", default="", cast=Csv()) INTERNAL_IPS = [ "127.0.0.1", @@ -43,9 +39,8 @@ INTERNAL_IPS = [ ] # Admins receive server errors, this is useful to be notified of potential bugs -ADMINS = [ - ("admin", "photos-admin@lists.crans.org"), -] +# Format: "Name:email,Name2:email2" +ADMINS = [tuple(a.split(":")) for a in config("ADMINS", default="", cast=Csv()) if a] # Use secure cookies in production SESSION_COOKIE_SECURE = not DEBUG @@ -204,7 +199,7 @@ if DEBUG: EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" # Mail will be sent from this address -SERVER_EMAIL = "photos@crans.org" +SERVER_EMAIL = config("SERVER_EMAIL", default="photos@crans.org") DEFAULT_FROM_EMAIL = f"Serveur photos <{SERVER_EMAIL}>" EMAIL_SUBJECT_PREFIX = "[Serveur photos] " diff --git a/photologue/models.py b/photologue/models.py index 5f97cf7..3585e8c 100644 --- a/photologue/models.py +++ b/photologue/models.py @@ -224,8 +224,14 @@ class Gallery(models.Model): class ImageModel(models.Model): image = models.ImageField( - _("image"), max_length=IMAGE_FIELD_MAX_LENGTH, upload_to=get_storage_path + _("image"), + max_length=IMAGE_FIELD_MAX_LENGTH, + upload_to=get_storage_path, + width_field="image_width", + height_field="image_height", ) + image_width = models.PositiveIntegerField(editable=False, null=True) + image_height = models.PositiveIntegerField(editable=False, null=True) date_taken = models.DateTimeField( _("date taken"), null=True, diff --git a/requirements.txt b/requirements.txt index f3dfcdb..3f38e2d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ django-select2>=4.8 ExifRead>=2.1.2 Pillow>=6.0.0 django-debug-toolbar>=3.2.0 +python-decouple>=3.6