summaryrefslogtreecommitdiff
path: root/aggregator
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2020-09-08 11:58:00 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-09-09 10:45:17 +0200
commit34f9b3dbc6bd0f3cc55d1c873c131a25121a62e6 (patch)
tree078e08f0f7267a35e0bf3c52c0c4e7f09cbf0666 /aggregator
parent2b60ac0b1bbbb4177b62354ac6633549b85f5820 (diff)
Updated default Community section header to use Django's age.
Diffstat (limited to 'aggregator')
-rw-r--r--aggregator/context_processors.py39
1 files changed, 9 insertions, 30 deletions
diff --git a/aggregator/context_processors.py b/aggregator/context_processors.py
index 1948c6a8..27e897f0 100644
--- a/aggregator/context_processors.py
+++ b/aggregator/context_processors.py
@@ -1,37 +1,16 @@
-import requests
-from django.core.cache import cache
-from requests.exceptions import RequestException
+from datetime import date
-PEOPLE_STATS_URL = 'https://people.djangoproject.com/api/stats/'
-PACKAGES_STATS_URL = 'https://www.djangopackages.com/api/v3/packages/?limit=0'
-STATS_CACHE_KEY = 'community_stats'
+from django.utils.timesince import timesince
-
-def fetch(url):
- try:
- return requests.get(url, timeout=0.5).json()
- except (RequestException, ValueError):
- return {}
+# Take Django's DOB as from the first public blog post.
+# https://www.djangoproject.com/weblog/2005/jul/14/prelaunch/
+DJANGO_DOB = date(2005, 7, 14)
def community_stats(request):
"""
- Context processor to fetch community stats from Django people and
- Django packages.
-
- This caches the resulting dictionary to lower the chance
- of overwhelming those services.
+ Context processor to calculate Django's age for the community pages.
"""
- stats = cache.get(STATS_CACHE_KEY, None)
- if not stats:
-
- stats = fetch(PEOPLE_STATS_URL)
- packages_data = fetch(PACKAGES_STATS_URL)
- if 'meta' in packages_data:
- stats.update({'packages': packages_data['meta']['total_count']})
-
- stats = {'community_stats': stats}
-
- cache.add(STATS_CACHE_KEY, stats, 60 * 60 * 12) # for half a day
-
- return stats
+ # Django 3.2 introduces depth kwarg. Set timesince(..., depth=1) then.
+ stats = {'age': timesince(DJANGO_DOB)}
+ return {'community_stats': stats}