Skip to content

Commit

Permalink
Replace django-environ with typenv and dj-database-url
Browse files Browse the repository at this point in the history
To fix type warnings in comics.settings.
jodal committed Dec 28, 2024

Verified

This commit was signed with the committer’s verified signature.
jodal Stein Magnus Jodal
1 parent 0ac1713 commit 5b4f278
Showing 3 changed files with 88 additions and 33 deletions.
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -8,24 +8,25 @@ license = { text = "AGPL-3.0-only" }
classifiers = ["Private :: Do Not Upload"]
requires-python = ">=3.12,<3.13"
dependencies = [
"django==3.2.25",
"pillow>=11.0.0",
"cssmin==0.2.0",
"cssselect==1.2.0",
"defusedxml>=0.7.1",
"dj-database-url>=2.2.0",
"django-allauth==0.51",
"django-bootstrap-form==3.4",
"django-compressor==3.1",
"django-environ==0.9.0",
"django-invitations==1.9.3",
"django-tastypie==0.14.7",
"django==3.2.25",
"feedparser>=6.0.11",
"httpx>=0.28.1",
"jsmin==3.0.1",
"lxml>=5.3.0",
"pillow>=11.0.0",
"pytz>=2024.2",
"sentry-sdk>=2.19.2",
"setuptools>=75.6.0", # Needed by django-bootstrap-form
"typenv>=0.2.0",
]

[project.optional-dependencies]
65 changes: 46 additions & 19 deletions src/comics/settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from pathlib import Path
from urllib.parse import urlsplit

import environ
import dj_database_url
import sentry_sdk
from django.core.management.utils import get_random_secret_key
from sentry_sdk.integrations.django import DjangoIntegration
from typenv import Env

# Paths
ROOT_DIR = Path(__file__).parents[2]
@@ -11,27 +14,31 @@


# Environment variables
env = environ.Env()
env.read_env(ROOT_DIR / ".env")
env = Env()
env.read_env(".env")


#: The Django secret key
SECRET_KEY = env.str(
"DJANGO_SECRET_KEY",
default="django-insecure-xe=$sh@*pt#46(_q(zy89quq&n1rut&o9b(qk1+o(^7exqqnj=",
default=get_random_secret_key(),
)

#: Debug mode. Keep off in production.
DEBUG = env.bool("DJANGO_DEBUG", default=False)
DEBUG = env.bool(
"DJANGO_DEBUG",
default=False,
)

#: Site admins
ADMINS = []
if "DJANGO_ADMIN" in env:
ADMINS.append(("Site admin", env.str("DJANGO_ADMIN")))
if admin_email := env.str("DJANGO_ADMIN", default=None):
ADMINS.append(("Site admin", admin_email))

#: Default from email
DEFAULT_FROM_EMAIL = env.str(
"DJANGO_DEFAULT_FROM_EMAIL", default="[email protected]"
"DJANGO_DEFAULT_FROM_EMAIL",
default="[email protected]",
)

SQLITE_FILE = str(RUN_DIR / "db.sqlite3")
@@ -40,7 +47,12 @@
#: Database settings. You will want to change this for production. See the
#: Django docs for details.
DATABASES = {
"default": env.db("DATABASE_URL", default=SQLITE_URL),
"default": dj_database_url.parse(
env.str(
"DATABASE_URL",
default=SQLITE_URL,
),
),
}

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
@@ -55,18 +67,30 @@
USE_TZ = True

#: Path on disk to where downloaded media will be stored and served from
MEDIA_ROOT = env.str("DJANGO_MEDIA_ROOT", default=str(RUN_DIR / "media"))
MEDIA_ROOT = env.str(
"DJANGO_MEDIA_ROOT",
default=str(RUN_DIR / "media"),
)
Path(MEDIA_ROOT).mkdir(parents=True, exist_ok=True)

#: URL to where downloaded media will be stored and served from
MEDIA_URL = env.str("DJANGO_MEDIA_URL", default="/media/")
MEDIA_URL = env.str(
"DJANGO_MEDIA_URL",
default="/media/",
)

#: Path on disk to where static files will be served from
STATIC_ROOT = env.str("DJANGO_STATIC_ROOT", default=str(RUN_DIR / "static"))
STATIC_ROOT = env.str(
"DJANGO_STATIC_ROOT",
default=str(RUN_DIR / "static"),
)
Path(STATIC_ROOT).mkdir(parents=True, exist_ok=True)

#: URL to where static files will be served from
STATIC_URL = env.str("DJANGO_STATIC_URL", default="/static/")
STATIC_URL = env.str(
"DJANGO_STATIC_URL",
default="/static/",
)

STATICFILES_DIRS = [
str(SRC_DIR / "comics" / "static"),
@@ -164,11 +188,14 @@
"OPTIONS": {"MAX_ENTRIES": 1000},
}
}
if "CACHE_URL" in env:
CACHES["default"] = env.cache(
"CACHE_URL", backend="django.core.cache.backends.memcached.PyMemcacheCache"
)
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
if cache_url := env.str("CACHE_URL", default=None):
parts = urlsplit(cache_url)
if parts.scheme == "memcache":
CACHES["default"] = {
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
"LOCATION": parts.netloc,
}
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"

CACHE_MIDDLEWARE_SECONDS = 300
CACHE_MIDDLEWARE_KEY_PREFIX = "comics"
@@ -185,7 +212,7 @@

WSGI_APPLICATION = "comics.wsgi.application"

ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default="*")
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["*"])


# ### djang-debug-toolbar settings
49 changes: 38 additions & 11 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b4f278

Please sign in to comment.