| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
from dotenv import load_dotenv |
| 4 |
|
| 5 |
# Load environment variables |
| 6 |
load_dotenv() |
| 7 |
|
| 8 |
# Build paths inside the project |
| 9 |
BASE_DIR = Path(__file__).resolve().parent.parent |
| 10 |
|
| 11 |
# SECURITY WARNING: keep the secret key used in production secret! |
| 12 |
SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-change-this-in-production') |
| 13 |
|
| 14 |
# SECURITY WARNING: don't run with debug turned on in production! |
| 15 |
DEBUG = os.getenv('DEBUG', 'False') == 'True' |
| 16 |
|
| 17 |
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',') |
| 18 |
|
| 19 |
# Application definition |
| 20 |
INSTALLED_APPS = [ |
| 21 |
'django.contrib.admin', |
| 22 |
'django.contrib.auth', |
| 23 |
'django.contrib.contenttypes', |
| 24 |
'django.contrib.sessions', |
| 25 |
'django.contrib.messages', |
| 26 |
'django.contrib.staticfiles', |
| 27 |
'rest_framework', |
| 28 |
'corsheaders', |
| 29 |
'api', |
| 30 |
] |
| 31 |
|
| 32 |
MIDDLEWARE = [ |
| 33 |
'django.middleware.security.SecurityMiddleware', |
| 34 |
'corsheaders.middleware.CorsMiddleware', # Must be before CommonMiddleware |
| 35 |
'django.middleware.common.CommonMiddleware', |
| 36 |
'django.middleware.csrf.CsrfViewMiddleware', |
| 37 |
'django.contrib.sessions.middleware.SessionMiddleware', |
| 38 |
'django.contrib.auth.middleware.AuthenticationMiddleware', |
| 39 |
'django.contrib.messages.middleware.MessageMiddleware', |
| 40 |
'django.middleware.clickjacking.XFrameOptionsMiddleware', |
| 41 |
] |
| 42 |
|
| 43 |
ROOT_URLCONF = 'localtoast.urls' |
| 44 |
|
| 45 |
TEMPLATES = [ |
| 46 |
{ |
| 47 |
'BACKEND': 'django.template.backends.django.DjangoTemplates', |
| 48 |
'DIRS': [], |
| 49 |
'APP_DIRS': True, |
| 50 |
'OPTIONS': { |
| 51 |
'context_processors': [ |
| 52 |
'django.template.context_processors.debug', |
| 53 |
'django.template.context_processors.request', |
| 54 |
'django.contrib.auth.context_processors.auth', |
| 55 |
'django.contrib.messages.context_processors.messages', |
| 56 |
], |
| 57 |
}, |
| 58 |
}, |
| 59 |
] |
| 60 |
|
| 61 |
WSGI_APPLICATION = 'localtoast.wsgi.application' |
| 62 |
|
| 63 |
# Database |
| 64 |
DATABASES = { |
| 65 |
'default': { |
| 66 |
'ENGINE': 'django.db.backends.sqlite3', |
| 67 |
'NAME': BASE_DIR / 'db.sqlite3', |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
# Password validation |
| 72 |
AUTH_PASSWORD_VALIDATORS = [ |
| 73 |
{ |
| 74 |
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', |
| 75 |
}, |
| 76 |
{ |
| 77 |
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', |
| 78 |
}, |
| 79 |
{ |
| 80 |
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', |
| 81 |
}, |
| 82 |
{ |
| 83 |
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', |
| 84 |
}, |
| 85 |
] |
| 86 |
|
| 87 |
# Internationalization |
| 88 |
LANGUAGE_CODE = 'en-us' |
| 89 |
TIME_ZONE = 'UTC' |
| 90 |
USE_I18N = True |
| 91 |
USE_TZ = True |
| 92 |
|
| 93 |
# Static files (CSS, JavaScript, Images) |
| 94 |
STATIC_URL = 'static/' |
| 95 |
STATIC_ROOT = BASE_DIR / 'staticfiles' |
| 96 |
|
| 97 |
# Default primary key field type |
| 98 |
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
| 99 |
|
| 100 |
# REST Framework settings |
| 101 |
REST_FRAMEWORK = { |
| 102 |
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', |
| 103 |
'PAGE_SIZE': 100, |
| 104 |
'DEFAULT_RENDERER_CLASSES': [ |
| 105 |
'rest_framework.renderers.JSONRenderer', |
| 106 |
], |
| 107 |
} |
| 108 |
|
| 109 |
# CORS settings - Updated for better compatibility |
| 110 |
CORS_ALLOWED_ORIGINS = [ |
| 111 |
"http://localhost:3000", |
| 112 |
"http://localhost:3001", |
| 113 |
"https://localtoast-frontend.vercel.app", |
| 114 |
"https://www.localtoast-frontend.vercel.app", |
| 115 |
"https://www.localtoast.fyi", |
| 116 |
"https://localtoast.fyi", |
| 117 |
] |
| 118 |
|
| 119 |
# Add frontend URL from environment if provided |
| 120 |
if os.getenv('FRONTEND_URL'): |
| 121 |
CORS_ALLOWED_ORIGINS.append(os.getenv('FRONTEND_URL')) |
| 122 |
|
| 123 |
# Explicit CORS configuration |
| 124 |
CORS_ALLOW_ALL_ORIGINS = False # Never True in production |
| 125 |
CORS_ALLOW_CREDENTIALS = True |
| 126 |
CORS_PREFLIGHT_MAX_AGE = 86400 |
| 127 |
|
| 128 |
CORS_ALLOW_METHODS = [ |
| 129 |
'DELETE', |
| 130 |
'GET', |
| 131 |
'OPTIONS', |
| 132 |
'PATCH', |
| 133 |
'POST', |
| 134 |
'PUT', |
| 135 |
] |
| 136 |
|
| 137 |
CORS_ALLOW_HEADERS = [ |
| 138 |
'accept', |
| 139 |
'accept-encoding', |
| 140 |
'authorization', |
| 141 |
'content-type', |
| 142 |
'dnt', |
| 143 |
'origin', |
| 144 |
'user-agent', |
| 145 |
'x-csrftoken', |
| 146 |
'x-requested-with', |
| 147 |
] |
| 148 |
|
| 149 |
# Media files |
| 150 |
MEDIA_URL = '/media/' |
| 151 |
MEDIA_ROOT = BASE_DIR / 'media' |
| 152 |
|
| 153 |
# Google Places API Key |
| 154 |
GOOGLE_PLACES_API_KEY = os.getenv('GOOGLE_PLACES_API_KEY', '') |