Skip to content

Upgrade to Django 4.2 #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ COPY . /code/
RUN --mount=type=cache,target=/root/.npm npm install
RUN npm run build

RUN python manage.py collectstatic --noinput
RUN python manage.py collectstatic --noinput --clear
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is minor but fixes a warning when building the container locally. Since the static directory won't exist when GitHub Actions is building it for deployment, this only helps dev.


# Run the container unprivileged
RUN addgroup www && useradd -g www www
Expand All @@ -54,4 +54,4 @@ RUN date -u +'%Y-%m-%dT%H:%M:%SZ' > BUILD_DATE

EXPOSE 8000

CMD ["gunicorn", "--timeout", "15", "--bind", ":8000", "--workers", "2", "--max-requests", "10000", "--max-requests-jitter", "100", "--log-file", "-", "config.wsgi"]
CMD ["gunicorn", "--timeout", "15", "--bind", ":8000", "--workers", "2", "--max-requests", "10000", "--max-requests-jitter", "100", "--log-file", "-", "--access-logfile", "-", "config.wsgi"]
34 changes: 19 additions & 15 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Django settings for pythonsd project.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
https://docs.djangoproject.com/en/4.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

import json
Expand All @@ -20,7 +20,7 @@


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

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


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


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
# https://docs.djangoproject.com/en/4.2/topics/i18n/
# --------------------------------------------------------------------------
LANGUAGE_CODE = "en-us"

TIME_ZONE = "America/Los_Angeles"

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
# https://docs.djangoproject.com/en/4.2/howto/static-files/
# https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES
# --------------------------------------------------------------------------
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = "/static-files/"

# Due to a bug relating to the manifest not being generated before the tests run
# We can't use CompressedManifestStaticFilesStorage (yet)
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "assets", "dist"),
os.path.join(BASE_DIR, "pythonsd", "static"),
]

STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}

MEDIA_URL = os.environ.get("MEDIA_URL", default="/media/")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")


# Email
# https://docs.djangoproject.com/en/3.2/topics/email/
# https://docs.djangoproject.com/en/4.2/topics/email/
# --------------------------------------------------------------------------
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
DEFAULT_FROM_EMAIL = "noreply@sandiegopython.org"
Expand All @@ -132,8 +136,8 @@
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# http://docs.djangoproject.com/en/3.2/topics/logging
# https://docs.djangoproject.com/en/3.2/ref/settings/#logging
# http://docs.djangoproject.com/en/4.2/topics/logging
# https://docs.djangoproject.com/en/4.2/ref/settings/#logging
# --------------------------------------------------------------------------
LOGGING = {
"version": 1,
Expand Down
16 changes: 8 additions & 8 deletions config/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .base import * # noqa


# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

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


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


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


# Security
# https://docs.djangoproject.com/en/3.2/topics/security/
# https://docs.djangoproject.com/en/4.2/topics/security/
# --------------------------------------------------------------------------
if "SECURE_SSL_HOST" in os.environ:
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
Expand All @@ -99,14 +99,14 @@


# Sessions
# https://docs.djangoproject.com/en/3.2/topics/http/sessions/
# https://docs.djangoproject.com/en/4.2/topics/http/sessions/
# Don't put sessions in the database

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


# Email
# https://docs.djangoproject.com/en/3.2/topics/email/
# https://docs.djangoproject.com/en/4.2/topics/email/
# https://anymail.readthedocs.io/en/stable/
# --------------------------------------------------------------------------
if "SENDGRID_API_KEY" in os.environ:
Expand All @@ -116,7 +116,7 @@


# Logging
# http://docs.djangoproject.com/en/3.2/topics/logging
# http://docs.djangoproject.com/en/4.2/topics/logging
# --------------------------------------------------------------------------
LOGGING["loggers"][""]["level"] = "INFO"
LOGGING["loggers"]["pythonsd"]["level"] = "INFO"
14 changes: 14 additions & 0 deletions config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,19 @@

TESTING = True

STORAGES = {
# In-memory storage makes tests marginally faster!
"default": {
"BACKEND": "django.core.files.storage.InMemoryStorage",
},
# Whitenoise relies on the manifest being present.
# Which may not be there in testing
# unless you run `collectstatic` before running tests
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedStaticFilesStorage",
},
}


# Ignore whitenoise message about no static directory
warnings.filterwarnings("ignore", message="No directory at", module="whitenoise.base")
2 changes: 1 addition & 1 deletion config/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import os
Expand Down
11 changes: 6 additions & 5 deletions pythonsd/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import datetime
import zoneinfo
import logging

from django.conf import settings
from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView
from django.utils.decorators import method_decorator

import pytz
import requests
from defusedxml import ElementTree

Expand Down Expand Up @@ -69,9 +69,10 @@ def get_upcoming_events(self):
"link": e["link"],
"name": e["name"],
# Always show time in local San Diego time
"datetime": datetime.utcfromtimestamp(e["time"] // 1000)
.replace(tzinfo=pytz.utc)
.astimezone(pytz.timezone(settings.TIME_ZONE)),
"datetime": datetime.fromtimestamp(
e["time"] // 1000,
tz=zoneinfo.ZoneInfo(key=settings.TIME_ZONE),
),
"venue": e["venue"]["name"] if "venue" in e else None,
}
for e in resp.json()
Expand Down Expand Up @@ -132,7 +133,7 @@ def get_recent_videos(self):
# the stream was initialized in youtube, not when it was live
"datetime": datetime.fromisoformat(
entry.find("atom:updated", ns).text
).astimezone(pytz.timezone(settings.TIME_ZONE)),
).astimezone(zoneinfo.ZoneInfo(key=settings.TIME_ZONE)),
}
)
else:
Expand Down
5 changes: 2 additions & 3 deletions requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Helper for transforming a database URL envvar
# to a Django connection string
dj-database-url==2.1.0
dj-database-url==2.2.0

# This is a Django app
Django==3.2.25
pytz==2022.7.1
Django==4.2.13

# A zero dependency WSGI server
gunicorn==22.0.0
Expand Down
Loading