- Add token-based admin login/logout/me endpoints - Bootstrap admin from ADMIN_USERNAME/ADMIN_PASSWORD in Docker entrypoint - Read ALLOWED_HOSTS and CORS from env only (no hardcoded server IPs) - Keep docs/Postman/tests free of real credentials - Cover login and auth flows with 31 local API tests Co-authored-by: Cursor <cursoragent@cursor.com>
205 lines
5.7 KiB
Python
205 lines
5.7 KiB
Python
"""
|
|
Django settings for zonco_backend project.
|
|
|
|
Generated by 'django-admin startproject' using Django 4.2.7.
|
|
|
|
For more information on this file, see
|
|
https://docs.djangoproject.com/en/4.2/topics/settings/
|
|
|
|
For the full list of settings and their values, see
|
|
https://docs.djangoproject.com/en/4.2/ref/settings/
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import os
|
|
|
|
# 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/4.2/howto/deployment/checklist/
|
|
|
|
# SECURITY WARNING: keep the secret key used in production secret!
|
|
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-ro-sqgr@$*(qao)@d&ezk*z9&%+vbeurgi$b+6y650j*$1b+n5')
|
|
|
|
# SECURITY WARNING: don't run with debug turned on in production!
|
|
# Default to False for production safety - set DEBUG=True only in development
|
|
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
|
|
|
|
# Allow all hosts in development mode for network access
|
|
# In production, ALLOWED_HOSTS MUST be set via environment variable
|
|
if DEBUG:
|
|
ALLOWED_HOSTS = ['*'] # Allow all hosts in development only
|
|
else:
|
|
allowed_hosts_env = os.environ.get('ALLOWED_HOSTS', '')
|
|
ALLOWED_HOSTS = [h.strip() for h in allowed_hosts_env.split(',') if h.strip()]
|
|
if not ALLOWED_HOSTS:
|
|
# Fail safe: empty list rejects all hosts until env is configured
|
|
ALLOWED_HOSTS = []
|
|
|
|
|
|
# Application definition
|
|
|
|
INSTALLED_APPS = [
|
|
'django.contrib.admin',
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.messages',
|
|
'django.contrib.staticfiles',
|
|
# Third party apps
|
|
'rest_framework',
|
|
'rest_framework.authtoken',
|
|
'corsheaders',
|
|
# Local apps
|
|
'api',
|
|
]
|
|
|
|
MIDDLEWARE = [
|
|
'django.middleware.security.SecurityMiddleware',
|
|
'corsheaders.middleware.CorsMiddleware',
|
|
'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 = 'zonco_backend.urls'
|
|
|
|
TEMPLATES = [
|
|
{
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'DIRS': [],
|
|
'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',
|
|
],
|
|
},
|
|
},
|
|
]
|
|
|
|
WSGI_APPLICATION = 'zonco_backend.wsgi.application'
|
|
|
|
|
|
# Database
|
|
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
|
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.postgresql',
|
|
'NAME': os.environ.get('POSTGRES_DB', 'Zoneco_ORG'),
|
|
'USER': os.environ.get('POSTGRES_USER', 'postgres'),
|
|
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'postgres'),
|
|
'HOST': os.environ.get('POSTGRES_HOST', 'localhost'),
|
|
'PORT': os.environ.get('POSTGRES_PORT', '5432'),
|
|
}
|
|
}
|
|
|
|
|
|
# Password validation
|
|
# https://docs.djangoproject.com/en/4.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',
|
|
},
|
|
]
|
|
|
|
|
|
# Internationalization
|
|
# https://docs.djangoproject.com/en/4.2/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/4.2/howto/static-files/
|
|
|
|
STATIC_URL = 'static/'
|
|
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
|
|
|
# Media files
|
|
MEDIA_URL = 'media/'
|
|
MEDIA_ROOT = BASE_DIR / 'media'
|
|
|
|
# Default primary key field type
|
|
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
|
|
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
|
|
# REST Framework configuration
|
|
REST_FRAMEWORK = {
|
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
|
'rest_framework.authentication.TokenAuthentication',
|
|
'rest_framework.authentication.BasicAuthentication',
|
|
'rest_framework.authentication.SessionAuthentication',
|
|
],
|
|
'DEFAULT_PERMISSION_CLASSES': [
|
|
'rest_framework.permissions.AllowAny',
|
|
],
|
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
|
'PAGE_SIZE': 20,
|
|
'DEFAULT_RENDERER_CLASSES': [
|
|
'rest_framework.renderers.JSONRenderer',
|
|
],
|
|
'DEFAULT_PARSER_CLASSES': [
|
|
'rest_framework.parsers.JSONParser',
|
|
'rest_framework.parsers.MultiPartParser',
|
|
'rest_framework.parsers.FormParser',
|
|
],
|
|
}
|
|
|
|
# CORS configuration
|
|
# In development, allow all origins for easier testing
|
|
# In production, CORS_ALLOWED_ORIGINS MUST be set via environment variable
|
|
if DEBUG:
|
|
CORS_ALLOW_ALL_ORIGINS = True # Development only - allows all origins
|
|
else:
|
|
cors_origins_env = os.environ.get('CORS_ALLOWED_ORIGINS', '')
|
|
CORS_ALLOWED_ORIGINS = [o.strip() for o in cors_origins_env.split(',') if o.strip()]
|
|
|
|
CORS_ALLOW_CREDENTIALS = True
|
|
|
|
CORS_ALLOW_METHODS = [
|
|
'DELETE',
|
|
'GET',
|
|
'OPTIONS',
|
|
'PATCH',
|
|
'POST',
|
|
'PUT',
|
|
]
|
|
|
|
CORS_ALLOW_HEADERS = [
|
|
'accept',
|
|
'accept-encoding',
|
|
'authorization',
|
|
'content-type',
|
|
'dnt',
|
|
'origin',
|
|
'user-agent',
|
|
'x-csrftoken',
|
|
'x-requested-with',
|
|
]
|