From 8942892a5cd091ddfcedcb2d59a12cdd6b7e8506 Mon Sep 17 00:00:00 2001 From: Lacey Williams Henschel Date: Wed, 3 Jan 2024 13:52:53 -0800 Subject: [PATCH] Add event caching, add event docs --- ak/views.py | 13 +++++++++++++ config/settings.py | 2 ++ docs/README.md | 1 + docs/calendar.md | 9 +++++++++ docs/env_vars.md | 13 ++++++++++++- 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 docs/calendar.md diff --git a/ak/views.py b/ak/views.py index 0639aab3..e186345f 100644 --- a/ak/views.py +++ b/ak/views.py @@ -1,5 +1,7 @@ import requests import structlog +from django.conf import settings +from django.core.cache import cache from django.core.exceptions import PermissionDenied from django.http import Http404, HttpResponse from django.shortcuts import render @@ -38,6 +40,11 @@ class HomepageView(TemplateView): return context def get_events(self): + """Returns the events from the Boost Google Calendar.""" + cached_events = cache.get(settings.EVENTS_CACHE_KEY) + if cached_events: + return cached_events + try: raw_event_data = get_calendar() except Exception: @@ -49,6 +56,12 @@ class HomepageView(TemplateView): events = extract_calendar_events(raw_event_data) sorted_events = events_by_month(events) + cache.set( + settings.EVENTS_CACHE_KEY, + dict(sorted_events), + settings.EVENTS_CACHE_TIMEOUT, + ) + return dict(sorted_events) def get_featured_library(self, latest_version): diff --git a/config/settings.py b/config/settings.py index d1240689..e6ecb675 100755 --- a/config/settings.py +++ b/config/settings.py @@ -466,3 +466,5 @@ MINIMUM_BOOST_VERSION = "1.31.0" # Boost Google Calendar BOOST_CALENDAR = "5rorfm42nvmpt77ac0vult9iig@group.calendar.google.com" CALENDAR_API_KEY = env("CALENDAR_API_KEY", default="changeme") +EVENTS_CACHE_KEY = "homepage_events" +EVENTS_CACHE_TIMEOUT = 300 # 5 min diff --git a/docs/README.md b/docs/README.md index 11397249..2ffa778b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,6 +4,7 @@ - [Dependency Management](./dependencies.md) - [Development Setup Notes](./development_setup_notes.md) - [Environment Variables](./env_vars.md) +- [Events Calendar](./calendar.md) - [Example Files](./examples/README.md) - Contains samples of `libraries.json`. `.gitmodules`, and other files that Boost data depends on - [Hosting](./hosting/README.md) - [Mailman](./mailman/README.md) diff --git a/docs/calendar.md b/docs/calendar.md new file mode 100644 index 00000000..153b931e --- /dev/null +++ b/docs/calendar.md @@ -0,0 +1,9 @@ +# Events Calendar + +The "Schedule of Events" section on the homepage dynamically display upcoming events from the Boost Google Calendar. See the settings in [Environment Variables](./env_vars.md). + +- See the settings in [Environment Variables](./env_vars.md). +- Main code for event retrieval is in `core/calendar.py` +- Raw data is extracted from the Google Calendar API +- The raw data is processed to extract the event data that we want to display, and sorted into the order we want +- The home page caches the result, and attempts to retrieve the events from the cache before hitting the Google Calendar API diff --git a/docs/env_vars.md b/docs/env_vars.md index dba7d825..05cfcb38 100644 --- a/docs/env_vars.md +++ b/docs/env_vars.md @@ -36,8 +36,19 @@ This project uses environment variables to configure certain aspects of the appl - For **local development**, obtain valid value from the Boost team. - In **deployed environments**, the valid value is set in `kube/boost/values.yaml` (or the environment-specific yaml file). -## `CALENDAR_API_KEY` +## Boost Google Calendar settings + +### `BOOST_CALENDAR` + +- Address for the Boost Google Calendar +- Hard-coded in `settings.py` in all environments +### `CALENDAR_API_KEY` - API key for the Boost Google calendar - For **local development**, obtain valid value from the Boost team. - In **deployed environments**, the valid value is set in `kube/boost/values.yaml` (or the environment-specific yaml file). + +### `EVENTS_CACHE_KEY` and `EVENTS_CACHE_TIMEOUT` + +- The cache key and timeout length for the Google Calendar events +- Hard-coded in `settings.py` in all environments