sgmp/config/settings/base.py

129 lines
3.9 KiB
Python
Raw Permalink Normal View History

2026-03-09 18:46:01 +00:00
# SGMP_PROD/config/settings/base.py
import os
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Em produção defina a variável de ambiente SECRET_KEY; default apenas para desenvolvimento.
SECRET_KEY = os.getenv('SECRET_KEY', 'dev-insecure-key')
DEBUG = False
# Lista separada por vírgulas; em produção defina ALLOWED_HOSTS (ex.: "localhost,127.0.0.1,app.exemplo.com").
ALLOWED_HOSTS = [h.strip() for h in os.getenv('ALLOWED_HOSTS', '').split(',') if h.strip()]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Apps de terceiros
'corsheaders',
# Seus apps
'solicitacoes'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
# 'winthor_sessions.middleware.WinthorBasicAuthMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'solicitacoes.context_processors.usuario_sistema',
],
},
},
]
# Banco de dados padrão (SQLite), será sobrescrito em produção e testes.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
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'},
]
# Internacionalização
LANGUAGE_CODE = 'pt-br'
TIME_ZONE = 'America/Sao_Paulo'
USE_I18N = True
USE_TZ = True
# Arquivos Estáticos (CSS, JS, Imagens)
STATIC_URL = '/static/'
STATICFILES_DIRS = [d for d in [BASE_DIR / 'static'] if d.exists()]
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
# Arquivos de Mídia (Uploads de usuários)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'mediafiles'
# Chave Primária Padrão
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# ============================
# CONFIGURAÇÃO DE SESSÃO
# ============================
# 30 minutos (em segundos)
SESSION_COOKIE_AGE = 30 * 60
# SESSION_COOKIE_AGE = 1
# Expira por tempo, não só ao fechar browser
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_SAVE_EVERY_REQUEST = True
# Segurança básica do cookie
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = "Lax"
SQLSERVER_CONFIG = {
"SERVER": os.getenv("RM_HOST"),
"PORT": int(os.getenv("RM_PORT", 38000)),
"USER": os.getenv("RM_USER"),
"PASSWORD": os.getenv("RM_PASS"),
"DATABASE": os.getenv("RM_DB"),
}
# ============================
# CONFIGURAÇÃO DE LOGIN/URLS
# ============================
LOGIN_URL = "/login/"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/login/"