diff --git a/expenses_manager/expenses_manager/settings.py b/expenses_manager/expenses_manager/settings.py index e07368b..d1b1d8a 100644 --- a/expenses_manager/expenses_manager/settings.py +++ b/expenses_manager/expenses_manager/settings.py @@ -13,7 +13,7 @@ https://docs.djangoproject.com/en/5.2/ref/settings/ import os from pathlib import Path from dotenv import load_dotenv -from django.contrib.messages import constants as messages +from django.core.exceptions import ImproperlyConfigured load_dotenv() @@ -30,13 +30,34 @@ BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv("DEBUG", "False") == "True" -SECRET_KEY = os.environ.get('SECRET_KEY', 'fallback-secret-key-for-dev') +SECRET_KEY = os.environ.get("SECRET_KEY") +if not SECRET_KEY: + if DEBUG: + SECRET_KEY = "django-insecure-dev-only-fallback-key" + else: + raise ImproperlyConfigured( + "SECRET_KEY no está definido. Es obligatorio en producción " + "(DEBUG-False) - revisa el .env del despliegue en el NAS" + ) ALLOWED_HOSTS = [ - "finanzas.kuijper.es", + h.strip() + for h in os.environ.get("ALLOWED_HOSTS", "localhost, 127.0.0.1").split(",") + if h.strip() ] +CSRF_TRUSTED_ORIGINS = [ + o.strip() + for o in os.environ.get("CSRF_TRUSTED_ORIGINS", "").split(",") + if o.strip() +] + +if not DEBUG: + SESSION_COOKIE_SECURE = True + CSRF_COOKIE_SECURE = True + SECURE_SSL_REDIRECT = True + SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") # Application definition @@ -52,6 +73,7 @@ INSTALLED_APPS = [ MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', + 'whitenoise.middleware.WhiteNoiseMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', @@ -82,50 +104,61 @@ WSGI_APPLICATION = 'expenses_manager.wsgi.application' # Database # https://docs.djangoproject.com/en/5.2/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + +DB_ENGINE = os.environ.get("DB_ENGINE", "sqlite3") +if DB_ENGINE == "postgresql": + DATABASES = { + 'default': { + "ENGINE": "django.db.backends.postgresql", + "NAME": os.environ["DB_NAME"], + "USER": os.environ["DB_USER"], + "PASSWORD": os.environ["DB_PASSWORD"], + "HOST": os.environ.get("DB_HOST", "localhost"), + "PORT": os.environ.get("DB_PORT", "5432"), + } } -} +else: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } + } + + + # Password validation # https://docs.djangoproject.com/en/5.2/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', - }, + {'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/5.2/topics/i18n/ -LANGUAGE_CODE = 'en-us' - +LANGUAGE_CODE = 'es' +LANGUAGE_CODE = 'es' TIME_ZONE = 'UTC' - USE_I18N = True - USE_TZ = True +DATE_INPUT_FORMATS = ["%d/%m/%Y"] +DATETIME_INPUT_FORMATS = ["%d/%m/%Y %H:%M"] + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.2/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = '/static/' +STATIC_ROOT = BASE_DIR / 'staticfiles' +STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" # Default primary key field type # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field @@ -134,4 +167,23 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' LOGIN_URL = 'login' LOGIN_REDIRECT_URL = 'home' -LOGOUT_REDIRECT_URL = 'login' \ No newline at end of file +LOGOUT_REDIRECT_URL = 'login' + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "handlers": { + "console": {"class": "logging.StreamHandler"}, + }, + "root": { + "handlers": ["console"], + "level": "WARNING", + }, + "loggers": { + "django": { + "handlers": ["console"], + "level": "INFO", + "propagate": False, + }, + }, +} \ No newline at end of file diff --git a/expenses_manager/requirements.txt b/expenses_manager/requirements.txt index fcb9d89..f46da45 100644 Binary files a/expenses_manager/requirements.txt and b/expenses_manager/requirements.txt differ