Skip to content

Commit 4caa86e

Browse files
committed
Upgrade to Django 4.2
- Stop using pytz and use zoneinfo - Small changes to storages configuration - USE_L10N is deprecated (default on) and will be always on starting with Django 5
1 parent b311531 commit 4caa86e

File tree

7 files changed

+51
-33
lines changed

7 files changed

+51
-33
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ COPY . /code/
4040
RUN --mount=type=cache,target=/root/.npm npm install
4141
RUN npm run build
4242

43-
RUN python manage.py collectstatic --noinput
43+
RUN python manage.py collectstatic --noinput --clear
4444

4545
# Run the container unprivileged
4646
RUN addgroup www && useradd -g www www

config/settings/base.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Django settings for pythonsd project.
33
44
For more information on this file, see
5-
https://docs.djangoproject.com/en/3.2/topics/settings/
5+
https://docs.djangoproject.com/en/4.2/topics/settings/
66
77
For the full list of settings and their values, see
8-
https://docs.djangoproject.com/en/3.2/ref/settings/
8+
https://docs.djangoproject.com/en/4.2/ref/settings/
99
"""
1010

1111
import json
@@ -20,7 +20,7 @@
2020

2121

2222
# Quick-start development settings - unsuitable for production
23-
# https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
23+
# https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
2424

2525
# SECURITY WARNING: keep the secret key used in production secret!
2626
# SECURITY WARNING: don't run with debug turned on in production!
@@ -82,46 +82,50 @@
8282

8383

8484
# Database
85-
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
85+
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
8686
# --------------------------------------------------------------------------
8787
DATABASES = {"default": dj_database_url.config(default="sqlite:///db.sqlite3")}
8888
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
8989

9090

9191
# Internationalization
92-
# https://docs.djangoproject.com/en/3.2/topics/i18n/
92+
# https://docs.djangoproject.com/en/4.2/topics/i18n/
9393
# --------------------------------------------------------------------------
9494
LANGUAGE_CODE = "en-us"
9595

9696
TIME_ZONE = "America/Los_Angeles"
9797

9898
USE_I18N = True
9999

100-
USE_L10N = True
101-
102100
USE_TZ = True
103101

104102

105103
# Static files (CSS, JavaScript, Images)
106-
# https://docs.djangoproject.com/en/3.2/howto/static-files/
104+
# https://docs.djangoproject.com/en/4.2/howto/static-files/
105+
# https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES
107106
# --------------------------------------------------------------------------
108107
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
109108
STATIC_URL = "/static-files/"
110-
111-
# Due to a bug relating to the manifest not being generated before the tests run
112-
# We can't use CompressedManifestStaticFilesStorage (yet)
113-
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
114109
STATICFILES_DIRS = [
115110
os.path.join(BASE_DIR, "assets", "dist"),
116111
os.path.join(BASE_DIR, "pythonsd", "static"),
117112
]
118113

114+
STORAGES = {
115+
"default": {
116+
"BACKEND": "django.core.files.storage.FileSystemStorage",
117+
},
118+
"staticfiles": {
119+
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
120+
},
121+
}
122+
119123
MEDIA_URL = os.environ.get("MEDIA_URL", default="/media/")
120124
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
121125

122126

123127
# Email
124-
# https://docs.djangoproject.com/en/3.2/topics/email/
128+
# https://docs.djangoproject.com/en/4.2/topics/email/
125129
# --------------------------------------------------------------------------
126130
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
127131
DEFAULT_FROM_EMAIL = "[email protected]"
@@ -132,8 +136,8 @@
132136
# A sample logging configuration. The only tangible logging
133137
# performed by this configuration is to send an email to
134138
# the site admins on every HTTP 500 error when DEBUG=False.
135-
# http://docs.djangoproject.com/en/3.2/topics/logging
136-
# https://docs.djangoproject.com/en/3.2/ref/settings/#logging
139+
# http://docs.djangoproject.com/en/4.2/topics/logging
140+
# https://docs.djangoproject.com/en/4.2/ref/settings/#logging
137141
# --------------------------------------------------------------------------
138142
LOGGING = {
139143
"version": 1,

config/settings/prod.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .base import * # noqa
1313

1414

15-
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
15+
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
1616

1717
DEBUG = False
1818
SECRET_KEY = os.environ["SECRET_KEY"]
@@ -47,19 +47,19 @@
4747
# The endpoint URL is necessary for Cloudflare R2
4848
AWS_S3_ENDPOINT_URL = os.environ.get("AWS_S3_ENDPOINT_URL", default=None)
4949
if AWS_S3_ACCESS_KEY_ID and AWS_S3_SECRET_ACCESS_KEY and AWS_STORAGE_BUCKET_NAME:
50-
DEFAULT_FILE_STORAGE = "storages.backends.s3.S3Storage"
50+
STORAGES["default"]["BACKEND"] = "storages.backends.s3.S3Storage"
5151

5252

5353
# Database
54-
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
54+
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
5555
# --------------------------------------------------------------------------
5656
DATABASES = {"default": dj_database_url.config()}
5757
DATABASES["default"]["ATOMIC_REQUESTS"] = True
5858
DATABASES["default"]["CONN_MAX_AGE"] = 600
5959

6060

6161
# Caching
62-
# https://docs.djangoproject.com/en/3.2/ref/settings/#caches
62+
# https://docs.djangoproject.com/en/4.2/ref/settings/#caches
6363
# http://niwinz.github.io/django-redis/
6464
# --------------------------------------------------------------------------
6565
if "REDIS_URL" in os.environ:
@@ -76,7 +76,7 @@
7676

7777

7878
# Security
79-
# https://docs.djangoproject.com/en/3.2/topics/security/
79+
# https://docs.djangoproject.com/en/4.2/topics/security/
8080
# --------------------------------------------------------------------------
8181
if "SECURE_SSL_HOST" in os.environ:
8282
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
@@ -99,14 +99,14 @@
9999

100100

101101
# Sessions
102-
# https://docs.djangoproject.com/en/3.2/topics/http/sessions/
102+
# https://docs.djangoproject.com/en/4.2/topics/http/sessions/
103103
# Don't put sessions in the database
104104

105105
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
106106

107107

108108
# Email
109-
# https://docs.djangoproject.com/en/3.2/topics/email/
109+
# https://docs.djangoproject.com/en/4.2/topics/email/
110110
# https://anymail.readthedocs.io/en/stable/
111111
# --------------------------------------------------------------------------
112112
if "SENDGRID_API_KEY" in os.environ:
@@ -116,7 +116,7 @@
116116

117117

118118
# Logging
119-
# http://docs.djangoproject.com/en/3.2/topics/logging
119+
# http://docs.djangoproject.com/en/4.2/topics/logging
120120
# --------------------------------------------------------------------------
121121
LOGGING["loggers"][""]["level"] = "INFO"
122122
LOGGING["loggers"]["pythonsd"]["level"] = "INFO"

config/settings/test.py

+14
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,19 @@
77

88
TESTING = True
99

10+
STORAGES = {
11+
# In-memory storage makes tests marginally faster!
12+
"default": {
13+
"BACKEND": "django.core.files.storage.InMemoryStorage",
14+
},
15+
# Whitenoise relies on the manifest being present.
16+
# Which may not be there in testing
17+
# unless you run `collectstatic` before running tests
18+
"staticfiles": {
19+
"BACKEND": "whitenoise.storage.CompressedStaticFilesStorage",
20+
},
21+
}
22+
23+
1024
# Ignore whitenoise message about no static directory
1125
warnings.filterwarnings("ignore", message="No directory at", module="whitenoise.base")

config/wsgi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
88
"""
99

1010
import os

pythonsd/views.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from datetime import datetime
2+
import zoneinfo
23
import logging
34

45
from django.conf import settings
56
from django.views.decorators.cache import cache_page
67
from django.views.generic import TemplateView
78
from django.utils.decorators import method_decorator
89

9-
import pytz
1010
import requests
1111
from defusedxml import ElementTree
1212

@@ -69,9 +69,10 @@ def get_upcoming_events(self):
6969
"link": e["link"],
7070
"name": e["name"],
7171
# Always show time in local San Diego time
72-
"datetime": datetime.utcfromtimestamp(e["time"] // 1000)
73-
.replace(tzinfo=pytz.utc)
74-
.astimezone(pytz.timezone(settings.TIME_ZONE)),
72+
"datetime": datetime.fromtimestamp(
73+
e["time"] // 1000,
74+
tz=zoneinfo.ZoneInfo(key=settings.TIME_ZONE),
75+
),
7576
"venue": e["venue"]["name"] if "venue" in e else None,
7677
}
7778
for e in resp.json()
@@ -132,7 +133,7 @@ def get_recent_videos(self):
132133
# the stream was initialized in youtube, not when it was live
133134
"datetime": datetime.fromisoformat(
134135
entry.find("atom:updated", ns).text
135-
).astimezone(pytz.timezone(settings.TIME_ZONE)),
136+
).astimezone(zoneinfo.ZoneInfo(key=settings.TIME_ZONE)),
136137
}
137138
)
138139
else:

requirements/common.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Helper for transforming a database URL envvar
22
# to a Django connection string
3-
dj-database-url==2.1.0
3+
dj-database-url==2.2.0
44

55
# This is a Django app
6-
Django==3.2.25
7-
pytz==2022.7.1
6+
Django==4.2.13
87

98
# A zero dependency WSGI server
109
gunicorn==22.0.0

0 commit comments

Comments
 (0)