Repository navigation
Expand file tree
/
Copy pathsettings.py
More file actions
165 lines (145 loc) · 6.14 KB
/
Copy pathsettings.py
File metadata and controls
165 lines (145 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Django settings for the LAN Programmy4V installation.
The defaults intentionally work with a local SQLite database. Production uses
PostgreSQL by setting DATABASE_URL (or the individual DB_* variables); no
application code needs to change between the two backends.
"""
import os
from pathlib import Path
from urllib.parse import urlparse
from django.core.exceptions import ImproperlyConfigured
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "django-insecure-aulaweb-development-only")
DEBUG = os.environ.get("DJANGO_DEBUG", "1").lower() in {"1", "true", "yes", "on"}
ALLOWED_HOSTS = [host.strip() for host in os.environ.get("DJANGO_ALLOWED_HOSTS", "*").split(",") if host.strip()]
if not ALLOWED_HOSTS:
ALLOWED_HOSTS = ["*"]
if not DEBUG:
if SECRET_KEY.startswith("django-insecure-"):
raise ImproperlyConfigured("DJANGO_SECRET_KEY debe definirse en producción.")
if "*" in ALLOWED_HOSTS:
raise ImproperlyConfigured("DJANGO_ALLOWED_HOSTS no puede contener * en producción.")
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"accounts",
"learning",
"grading",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
# Serve the compiled, same-origin bundle from the application container.
# WhiteNoise must follow SecurityMiddleware so its responses inherit the
# normal Django security headers while remaining usable without Nginx.
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"accounts.middleware.ForcePasswordChangeMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "aulaweb.urls"
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_processor
9854
s.messages",
"aulaweb.context_processors.csp_nonce",
],
},
}
]
WSGI_APPLICATION = "aulaweb.wsgi.application"
ASGI_APPLICATION = "aulaweb.asgi.application"
def _database_from_environment():
database_url = os.environ.get("DATABASE_URL", "").strip()
if database_url:
parsed = urlparse(database_url)
scheme = parsed.scheme.lower()
if scheme in {"postgres", "postgresql"}:
options = {}
if os.environ.get("DB_SSLMODE"):
options["sslmode"] = os.environ["DB_SSLMODE"]
return {
"ENGINE": "django.db.backends.postgresql",
"NAME": parsed.path.lstrip("/"),
"USER": parsed.username or "",
"PASSWORD": parsed.password or "",
"HOST": parsed.hostname or "localhost",
"PORT": str(parsed.port or 5432),
"OPTIONS": options,
"CONN_MAX_AGE": int(os.environ.get("DB_CONN_MAX_AGE", "60")),
}
if scheme in {"sqlite", "sqlite3"}:
sqlite_name = parsed.path or "db.sqlite3"
if sqlite_name.startswith("/") and not sqlite_name.startswith("//"):
sqlite_name = sqlite_name.lstrip("/")
return {"ENGINE": "django.db.backends.sqlite3", "NAME": BASE_DIR / sqlite_name}
if os.environ.get("DB_NAME"):
return {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("DB_NAME"),
"USER": os.environ.get("DB_USER", "aulaweb"),
"PASSWORD": os.environ.get("DB_PASSWORD", ""),
"HOST": os.environ.get("DB_HOST", "postgres"),
"PORT": os.environ.get("DB_PORT", "5432"),
"OPTIONS": {"sslmode": os.environ["DB_SSLMODE"]} if os.environ.get("DB_SSLMODE") else {},
"CONN_MAX_AGE": int(os.environ.get("DB_CONN_MAX_AGE", "60")),
}
data_dir = BASE_DIR / "data"
data_dir.mkdir(parents=True, exist_ok=True)
return {"ENGINE": "django.db.backends.sqlite3", "NAME": data_dir / "db.sqlite3"}
DATABASES = {"default": _database_from_environment()}
AUTH_USER_MODEL = "accounts.User"
AUTH_PASSWORD_VALIDATORS = [
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", "OPTIONS": {"min_length": 10}},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
]
PASSWORD_HASHERS = [
"django.contrib.auth.hashers.Argon2PasswordHasher",
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
]
LANGUAGE_CODE = "es-es"
TIME_ZONE = "Europe/Madrid"
USE_I18N = True
USE_TZ = True
STATIC_URL = "/static/"
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_ROOT = BASE_DIR / "staticfiles"
STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
LOGIN_URL = "/login/"
LOGIN_REDIRECT_URL = "/dashboard/"
LOGOUT_REDIRECT_URL = "/login/"
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = "Lax"
CSRF_COOKIE_SAMESITE = "Lax"
SESSION_COOKIE_SECURE = os.environ.get("DJANGO_SESSION_COOKIE_SECURE", "0").lower() in {"1", "true", "yes"}
CSRF_COOKIE_SECURE = os.environ.get("DJANGO_CSRF_COOKIE_SECURE", "0").lower() in {"1", "true", "yes"}
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = "DENY"
SECURE_REFERRER_POLICY = "same-origin"
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
CSRF_TRUSTED_ORIGINS = [
origin.strip()
for origin in os.environ.get("DJANGO_CSRF_TRUSTED_ORIGINS", "").split(",")
if origin.strip()
]