322 lines
8.7 KiB
Python
322 lines
8.7 KiB
Python
|
|
|
|
"""
|
|
Django settings for config project.
|
|
|
|
Generated by 'django-admin startproject' using Django 5.2.3.
|
|
|
|
For more information on this file, see
|
|
https://docs.djangoproject.com/en/5.2/topics/settings/
|
|
|
|
For the full list of settings and their values, see
|
|
https://docs.djangoproject.com/en/5.2/ref/settings/
|
|
"""
|
|
import os
|
|
import ssl
|
|
import smtplib
|
|
|
|
from pathlib import Path
|
|
from corsheaders.defaults import default_headers
|
|
from datetime import timedelta
|
|
|
|
# --- SOLO PARA DESARROLLO: Desactivar verificación de certificados SSL en SMTP ---
|
|
smtplib.SMTP_SSL.default_context = ssl._create_unverified_context
|
|
|
|
from dotenv import load_dotenv
|
|
import re
|
|
|
|
# Celery Beat Schedule
|
|
from celery.schedules import crontab
|
|
from config.stg.storage import *
|
|
|
|
CELERY_BEAT_SCHEDULE = {
|
|
|
|
|
|
}
|
|
|
|
# Cargar variables de entorno desde un archivo .env
|
|
load_dotenv()
|
|
|
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
# Quick-start development settings - unsuitable for production
|
|
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
|
|
|
|
# SECURITY WARNING: keep the secret key used in production secret!
|
|
SECRET_KEY = os.getenv('SECRET_KEY')
|
|
|
|
# SECURITY WARNING: don't run with debug turned on in production!
|
|
DEBUG = os.getenv('DEBUG', 'True') == 'True'
|
|
|
|
ALLOWED_HOSTS = [
|
|
'localhost'
|
|
,'host.docker.internal'
|
|
,'192.168.1.195'
|
|
,'127.0.0.1'
|
|
,'74.208.78.59'
|
|
,'api.efc-aduanasoft.com'
|
|
,'backend'
|
|
,'backend:8000'
|
|
,'0.0.0.0'
|
|
,'192.168.1.79'
|
|
]
|
|
|
|
SITE_URL = os.getenv('SITE_URL')
|
|
SERVICE_API_URL = os.getenv('SERVICE_API_URL')
|
|
SERVICE_API_URL_V2 = os.getenv('SERVICE_API_URL_V2')
|
|
# Application definition
|
|
BASE_APPS = [
|
|
'django.contrib.admin',
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.messages',
|
|
'django.contrib.staticfiles',
|
|
]
|
|
|
|
THIRD_APPS = [
|
|
'rest_framework',
|
|
'rest_framework.authtoken',
|
|
'rest_framework_simplejwt',
|
|
'drf_yasg',
|
|
'corsheaders',
|
|
'django_filters',
|
|
'rest_framework_simplejwt.token_blacklist',
|
|
]
|
|
|
|
OWN_APPS = [
|
|
'api',
|
|
'api.customs',
|
|
'api.record',
|
|
'api.organization',
|
|
'api.licence',
|
|
'api.cuser',
|
|
'api.datastage',
|
|
'api.vucem',
|
|
'api.logger',
|
|
'api.notificaciones',
|
|
'api.reports',
|
|
'api.cards',
|
|
'api.tasks',
|
|
]
|
|
|
|
INSTALLED_APPS = BASE_APPS + THIRD_APPS + OWN_APPS
|
|
MIDDLEWARE = [
|
|
'corsheaders.middleware.CorsMiddleware', # Debe ir antes de CommonMiddleware
|
|
'django.middleware.security.SecurityMiddleware',
|
|
'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',
|
|
'api.logger.middleware.RequestLoggingMiddleware',
|
|
'api.logger.middleware.ErrorLoggingMiddleware',
|
|
]
|
|
|
|
# Crear directorio de logs
|
|
LOGS_DIR = BASE_DIR / 'logs'
|
|
LOGS_DIR.mkdir(exist_ok=True)
|
|
|
|
# Configuración de logging unificada
|
|
LOGGING = {
|
|
'version': 1,
|
|
'disable_existing_loggers': False,
|
|
'handlers': {
|
|
'console': {
|
|
'class': 'logging.StreamHandler',
|
|
},
|
|
},
|
|
'root': {
|
|
'handlers': ['console'],
|
|
'level': 'INFO',
|
|
},
|
|
'loggers': {
|
|
'django': {
|
|
'handlers': ['console'],
|
|
'level': 'INFO',
|
|
'propagate': False,
|
|
},
|
|
'celery': {
|
|
'handlers': ['console'],
|
|
'level': 'INFO',
|
|
'propagate': False,
|
|
},
|
|
},
|
|
}
|
|
|
|
# Configuración para desarrollo y producción
|
|
if DEBUG:
|
|
CORS_ALLOW_ALL_ORIGINS = True
|
|
CORS_ALLOW_CREDENTIALS = True
|
|
SESSION_COOKIE_SECURE = False
|
|
CSRF_COOKIE_SECURE = False
|
|
USE_X_FORWARDED_HOST = False
|
|
else:
|
|
CORS_ALLOW_ALL_ORIGINS = False
|
|
CORS_ALLOW_CREDENTIALS = False
|
|
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
|
CORS_ALLOWED_ORIGINS = os.getenv('CORS_ALLOWED_ORIGINS').split(',')
|
|
CSRF_COOKIE_SECURE = True
|
|
SESSION_COOKIE_SECURE = True
|
|
USE_X_FORWARDED_HOST = True
|
|
|
|
CORS_ALLOW_HEADERS = list(default_headers) + [
|
|
'access-control-allow-origin',
|
|
'access-control-allow-credentials',
|
|
]
|
|
|
|
# # JWT Authentication settings
|
|
REST_FRAMEWORK = {
|
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
|
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
|
'rest_framework.authentication.TokenAuthentication', # Añade esta línea
|
|
],
|
|
'DEFAULT_PERMISSION_CLASSES': [
|
|
'rest_framework.permissions.IsAuthenticated',
|
|
],
|
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
|
'PAGE_SIZE': 20,
|
|
'DEFAULT_FILTER_BACKENDS': [
|
|
'django_filters.rest_framework.DjangoFilterBackend',
|
|
'rest_framework.filters.SearchFilter',
|
|
'rest_framework.filters.OrderingFilter',
|
|
],
|
|
}
|
|
|
|
# Configuración de Swagger
|
|
SWAGGER_SETTINGS = {
|
|
"DEFAULT_AUTO_SCHEMA_CLASS": "core.swagger.CustomAutoSchema",
|
|
'DEFAULT_AUTHENTICATION_CLASSES': (
|
|
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
|
),
|
|
'SECURITY_DEFINITIONS': {
|
|
'Bearer': {
|
|
'type': 'apiKey',
|
|
'name': 'Authorization',
|
|
'in': 'header',
|
|
'description': 'JWT Authorization header using the Bearer scheme. Example: "Authorization: Bearer {token}"'
|
|
}
|
|
},
|
|
'USE_SESSION_AUTH': False,
|
|
'LOGIN_URL': '/api/v1/token/',
|
|
'LOGOUT_URL': '/api/v1/auth/logout/',
|
|
'DOC_EXPANSION': 'None',
|
|
}
|
|
|
|
# Configuración adicional para ReDoc
|
|
REDOC_SETTINGS = {
|
|
'LAZY_RENDERING': False,
|
|
'HIDE_HOSTNAME': False,
|
|
'EXPAND_RESPONSES': 'all',
|
|
'PATH_IN_MIDDLE': True,
|
|
}
|
|
|
|
CSRF_TRUSTED_ORIGINS = [
|
|
"https://api.efc-aduanasoft.com",
|
|
"http://192.168.1.195",
|
|
"http://192.168.1.195:8000"
|
|
]
|
|
|
|
# URL Configuration
|
|
ROOT_URLCONF = 'config.urls'
|
|
|
|
# Template configuration
|
|
TEMPLATES = [
|
|
{
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'DIRS': [BASE_DIR / 'templates'],
|
|
'APP_DIRS': True,
|
|
'OPTIONS': {
|
|
'context_processors': [
|
|
'django.template.context_processors.request',
|
|
'django.contrib.auth.context_processors.auth',
|
|
'django.contrib.messages.context_processors.messages',
|
|
],
|
|
},
|
|
},
|
|
]
|
|
|
|
WSGI_APPLICATION = 'config.wsgi.application'
|
|
|
|
# Database
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE' : 'django.db.backends.postgresql',
|
|
'NAME' : os.getenv('DB_NAME'),
|
|
'USER' : os.getenv('DB_USER'),
|
|
'PASSWORD' : os.getenv('DB_PASSWORD'),
|
|
'HOST' : os.getenv('DB_HOST'),
|
|
'PORT' : os.getenv('DB_PORT'),
|
|
}
|
|
}
|
|
|
|
# Password validation
|
|
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
|
|
LANGUAGE_CODE = 'en-us'
|
|
TIME_ZONE = 'America/Mexico_City' # Zona horaria de Cd. Juárez, Chihuahua
|
|
USE_I18N = True
|
|
USE_TZ = True
|
|
|
|
# Static files
|
|
STATIC_URL = 'static/'
|
|
if DEBUG:
|
|
STATICFILES_DIRS = [BASE_DIR / 'staticfiles']
|
|
else:
|
|
STATICFILES_DIRS = []
|
|
STATIC_ROOT = BASE_DIR / 'static'
|
|
|
|
if STORAGE_BACKEND == 'minio':
|
|
MEDIA_URL = f"http://{os.getenv('MINIO_ENDPOINT')}/{AWS_STORAGE_BUCKET_NAME}/"
|
|
|
|
MEDIA_URL = '/media/'
|
|
MEDIA_ROOT = BASE_DIR / 'media'
|
|
|
|
# Default primary key field type
|
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
|
|
AUTH_USER_MODEL = 'cuser.CustomUser'
|
|
|
|
# Configuración SMTP para envío de correos electrónicos
|
|
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
|
EMAIL_HOST = os.getenv('EMAIL_HOST')
|
|
EMAIL_PORT = int(os.getenv('EMAIL_PORT'))
|
|
EMAIL_USE_TLS = True
|
|
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
|
|
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
|
|
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
|
|
|
|
# Configuración Celery
|
|
CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL', 'redis://redis:6379/0')
|
|
CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND', 'redis://redis:6379/0')
|
|
CELERY_TIMEZONE = 'America/Mexico_City'
|
|
|
|
# Configuración para procesamiento asíncrono nativo de Django
|
|
ASGI_APPLICATION = 'config.asgi.application'
|
|
|
|
SIMPLE_JWT = {
|
|
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=30), # Tokens de acceso cortos por seguridad
|
|
'REFRESH_TOKEN_LIFETIME': timedelta(days=5), # Refresh token de 5 días
|
|
'ROTATE_REFRESH_TOKENS': True, # Rotar refresh tokens para mayor seguridad
|
|
'BLACKLIST_AFTER_ROTATION': True,
|
|
'AUTH_HEADER_TYPES': ('Bearer',),
|
|
}
|
|
|
|
|