363 lines
10 KiB
Python
363 lines
10 KiB
Python
"""
|
|
Django settings for proyecto project.
|
|
|
|
Generated by 'django-admin startproject' using Django 6.0.1.
|
|
|
|
For more information on this file, see
|
|
https://docs.djangoproject.com/en/6.0/topics/settings/
|
|
|
|
For the full list of settings and their values, see
|
|
https://docs.djangoproject.com/en/6.0/ref/settings/
|
|
"""
|
|
|
|
import logging
|
|
import os, sys
|
|
from pathlib import Path
|
|
|
|
|
|
def load_dotenv(dotenv_path: Path) -> None:
|
|
if not dotenv_path.exists():
|
|
return
|
|
|
|
for raw_line in dotenv_path.read_text(encoding='utf-8').splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith('#') or '=' not in line:
|
|
continue
|
|
|
|
key, value = line.split('=', 1)
|
|
key = key.strip()
|
|
value = value.strip().strip('"').strip("'")
|
|
os.environ.setdefault(key, value)
|
|
|
|
|
|
def env_bool(name: str, default: bool = False) -> bool:
|
|
value = os.getenv(name)
|
|
if value is None:
|
|
return default
|
|
return value.strip().lower() in {'1', 'true', 'yes', 'on'}
|
|
|
|
|
|
def env_list(name: str, default: list[str] | None = None) -> list[str]:
|
|
value = os.getenv(name)
|
|
if value is None:
|
|
return default or []
|
|
return [item.strip() for item in value.split(',') if item.strip()]
|
|
|
|
|
|
def env_int(name: str, default: int) -> int:
|
|
value = os.getenv(name)
|
|
if value is None:
|
|
return default
|
|
return int(value)
|
|
|
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
load_dotenv(BASE_DIR / '.env')
|
|
|
|
|
|
# Quick-start development settings - unsuitable for production
|
|
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
|
|
|
|
# SECURITY WARNING: keep the secret key used in production secret!
|
|
SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-#g((q@lvnkt(j6)2(gvtn0px)r2r(911)pv59i(6w)5e!_-^ao')
|
|
|
|
# SECURITY WARNING: don't run with debug turned on in production!
|
|
DEBUG = env_bool('DEBUG', True)
|
|
|
|
ALLOWED_HOSTS = env_list('ALLOWED_HOSTS', [
|
|
'192.168.1.142',
|
|
'localhost',
|
|
'127.0.0.1',
|
|
])
|
|
|
|
|
|
# Application definition
|
|
|
|
INSTALLED_APPS = [
|
|
'tienda.apps.TiendaConfig',
|
|
'django.contrib.admin',
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.messages',
|
|
'django.contrib.staticfiles',
|
|
'compressor',
|
|
]
|
|
|
|
MIDDLEWARE = [
|
|
'django.middleware.security.SecurityMiddleware',
|
|
'whitenoise.middleware.WhiteNoiseMiddleware',
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
'django.middleware.common.CommonMiddleware',
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
|
]
|
|
|
|
ROOT_URLCONF = 'proyecto.urls'
|
|
|
|
TEMPLATES = [
|
|
{
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'DIRS': [],
|
|
'APP_DIRS': True,
|
|
'OPTIONS': {
|
|
'context_processors': [
|
|
'django.template.context_processors.request',
|
|
'django.contrib.auth.context_processors.auth',
|
|
'django.contrib.messages.context_processors.messages',
|
|
'tienda.context_processors.cart_context',
|
|
],
|
|
},
|
|
},
|
|
]
|
|
|
|
WSGI_APPLICATION = 'proyecto.wsgi.application'
|
|
|
|
|
|
# Database
|
|
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
|
|
# Activa MySQL con MYSQL_ENABLED=True en el .env; si no, se usa SQLite.
|
|
|
|
if env_bool('MYSQL_ENABLED', False):
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.mysql',
|
|
'NAME': os.getenv('MYSQL_DATABASE', 'tienda'),
|
|
'USER': os.getenv('MYSQL_USER', 'root'),
|
|
'PASSWORD': os.getenv('MYSQL_PASSWORD', ''),
|
|
'HOST': os.getenv('MYSQL_HOST', '127.0.0.1'),
|
|
'PORT': env_int('MYSQL_PORT', 3306),
|
|
'OPTIONS': {
|
|
'charset': 'utf8mb4',
|
|
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
|
|
},
|
|
}
|
|
}
|
|
else:
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': BASE_DIR / 'db.sqlite3',
|
|
}
|
|
}
|
|
|
|
|
|
# Password validation
|
|
# https://docs.djangoproject.com/en/6.0/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',
|
|
},
|
|
]
|
|
|
|
|
|
# Internationalization
|
|
# https://docs.djangoproject.com/en/6.0/topics/i18n/
|
|
|
|
LANGUAGE_CODE = 'en-us'
|
|
|
|
TIME_ZONE = 'UTC'
|
|
|
|
USE_I18N = True
|
|
|
|
USE_TZ = True
|
|
|
|
|
|
# Static files (CSS, JavaScript, Images)
|
|
# https://docs.djangoproject.com/en/6.0/howto/static-files/
|
|
|
|
STATIC_URL = 'static/'
|
|
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
|
STATICFILES_DIRS = [
|
|
BASE_DIR / 'tienda' / 'static',
|
|
]
|
|
|
|
STORAGES = {
|
|
'default': {
|
|
'BACKEND': 'django.core.files.storage.FileSystemStorage',
|
|
},
|
|
'staticfiles': {
|
|
'BACKEND': 'whitenoise.storage.CompressedManifestStaticFilesStorage',
|
|
},
|
|
}
|
|
|
|
STATICFILES_FINDERS = [
|
|
'django.contrib.staticfiles.finders.FileSystemFinder',
|
|
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
|
|
'compressor.finders.CompressorFinder'
|
|
]
|
|
|
|
COMPRESS_PRECOMPILERS = (
|
|
('text/x-scss', 'sass {infile} {outfile} --load-path=tienda/static/scss'),
|
|
)
|
|
|
|
import shutil
|
|
SASS_BINARY = shutil.which('sass')
|
|
if SASS_BINARY:
|
|
COMPRESS_PRECOMPILERS = (
|
|
('text/x-scss', f'{SASS_BINARY} {{infile}} {{outfile}} --load-path=tienda/static/scss'),
|
|
)
|
|
|
|
# Media files (User uploads)
|
|
MEDIA_URL = 'media/'
|
|
MEDIA_ROOT = BASE_DIR / 'tienda' / 'static' / 'media'
|
|
|
|
# Redis Configuration
|
|
CACHES = {
|
|
'default': {
|
|
'BACKEND': 'django_redis.cache.RedisCache',
|
|
'LOCATION': os.getenv('REDIS_URL', 'redis://127.0.0.1:6379/1'),
|
|
'OPTIONS': {
|
|
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
|
|
}
|
|
}
|
|
}
|
|
|
|
# Session Configuration - Use Redis for session storage
|
|
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
|
|
SESSION_CACHE_ALIAS = 'default'
|
|
|
|
# Configuración de mensajes para Bootstrap
|
|
from django.contrib.messages import constants as messages
|
|
MESSAGE_TAGS = {
|
|
messages.DEBUG: 'debug',
|
|
messages.INFO: 'info',
|
|
messages.SUCCESS: 'success',
|
|
messages.WARNING: 'warning',
|
|
messages.ERROR: 'danger',
|
|
}
|
|
|
|
|
|
# Login URL
|
|
LOGIN_URL = '/tienda/login/'
|
|
|
|
STRIPE_PUBLISHABLE_KEY = os.getenv('STRIPE_PUBLISHABLE_KEY', '')
|
|
STRIPE_SECRET_KEY = os.getenv('STRIPE_SECRET_KEY', '')
|
|
|
|
# PayPal Configuration (Sandbox)
|
|
# Para obtener credenciales: https://sandbox.paypal.com/
|
|
PAYPAL_CLIENT_ID = os.getenv('PAYPAL_CLIENT_ID', '') # Reemplazar con tu Client ID de PayPal Sandbox
|
|
PAYPAL_CLIENT_SECRET = os.getenv('PAYPAL_CLIENT_SECRET', '') # Reemplazar con tu Client Secret de PayPal Sandbox
|
|
PAYPAL_MODE = os.getenv('PAYPAL_MODE', 'sandbox') # Cambiar a 'live' en producción
|
|
|
|
|
|
SMTP_ENDPOINT = os.getenv('SMTP_ENDPOINT', 'smtp.email.eu-paris-1.oci.oraclecloud.com')
|
|
SMTP_PORT = env_int('SMTP_PORT', 587)
|
|
SECURITY = os.getenv('SECURITY', 'tls')
|
|
SMTP_USERNAME = os.getenv('SMTP_USERNAME', None)
|
|
SMTP_PASSWORD = os.getenv('SMTP_PASSWORD', None)
|
|
SMTP_EMAIL = os.getenv("SMTP_EMAIL", None)
|
|
if SMTP_USERNAME is None or SMTP_PASSWORD is None or SMTP_EMAIL is None:
|
|
print("Se requieren credenciales SMTP")
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
AUTH_USER_MODEL = 'tienda.User'
|
|
|
|
|
|
DOMAIN = os.getenv("DOMAIN", "localhost")
|
|
PROTOCOL = os.getenv("PROTOCOL", "http")
|
|
|
|
|
|
LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO').upper()
|
|
LOG_DIR = Path(os.getenv('LOG_DIR', BASE_DIR / 'logs'))
|
|
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
|
LOG_FILE = LOG_DIR / os.getenv('LOG_FILE', 'app.log')
|
|
|
|
|
|
LOGGING = {
|
|
'version': 1,
|
|
'disable_existing_loggers': False,
|
|
'formatters': {
|
|
'standard': {
|
|
'format': '%(asctime)s | %(levelname)s | %(name)s | %(message)s',
|
|
},
|
|
},
|
|
'handlers': {
|
|
'console': {
|
|
'class': 'logging.StreamHandler',
|
|
'formatter': 'standard',
|
|
'level': LOG_LEVEL,
|
|
},
|
|
'file': {
|
|
'class': 'logging.handlers.RotatingFileHandler',
|
|
'formatter': 'standard',
|
|
'filename': str(LOG_FILE),
|
|
'maxBytes': 5 * 1024 * 1024,
|
|
'backupCount': 5,
|
|
'level': LOG_LEVEL,
|
|
'encoding': 'utf-8',
|
|
},
|
|
},
|
|
'loggers': {
|
|
'tienda': {
|
|
'handlers': ['console', 'file'],
|
|
'level': LOG_LEVEL,
|
|
'propagate': False,
|
|
},
|
|
'tienda.audit': {
|
|
'handlers': ['console', 'file'],
|
|
'level': LOG_LEVEL,
|
|
'propagate': False,
|
|
},
|
|
'django': {
|
|
'handlers': ['console'],
|
|
'level': 'INFO',
|
|
'propagate': False,
|
|
},
|
|
'email.system': {
|
|
'handlers': ['console', 'file'],
|
|
'level': LOG_LEVEL,
|
|
'propagate': False
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
logging.captureWarnings(True)
|
|
|
|
|
|
|
|
|
|
|
|
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
|
EMAIL_HOST = SMTP_ENDPOINT
|
|
EMAIL_PORT = SMTP_PORT
|
|
EMAIL_USE_TLS = (SECURITY == 'tls') # True si SECURITY es 'tls'
|
|
EMAIL_USE_SSL = (SECURITY == 'ssl') # True si SECURITY es 'ssl'
|
|
EMAIL_HOST_USER = SMTP_USERNAME
|
|
EMAIL_HOST_PASSWORD = SMTP_PASSWORD
|
|
|
|
# El correo que se usará como remitente por defecto
|
|
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", SMTP_EMAIL)
|
|
|
|
# URL de Redis (asumiendo que corre en el puerto default 6379)
|
|
CELERY_BROKER_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
|
|
|
# Opcional: para guardar el resultado de las tareas
|
|
CELERY_RESULT_BACKEND = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
|
|
|
# Configuraciones adicionales recomendadas
|
|
CELERY_ACCEPT_CONTENT = ['json']
|
|
CELERY_TASK_SERIALIZER = 'json'
|
|
CELERY_RESULT_SERIALIZER = 'json'
|
|
|
|
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
|
|
|
|
USE_X_FORWARDED_HOST = True
|
|
SECURE_REFERER_POLICY = "strict-origin-when-cross-origin"
|
|
|
|
print(f"DEBUG: ALLOWED_HOSTS is {ALLOWED_HOSTS}") |