-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathevents.py
More file actions
77 lines (59 loc) · 2.46 KB
/
events.py
File metadata and controls
77 lines (59 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Provides the Meetup.com events that will be used to populate the site homepage.
"""
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured
from functools import wraps
from pythonkc_meetups import PythonKCMeetups
import logging
logger = logging.getLogger(__name__)
try:
api_key = settings.MEETUP_API_KEY
except AttributeError:
raise ImproperlyConfigured('MEETUP_API_KEY is required in settings')
num_past_events = getattr(settings, 'MEETUP_SHOW_PAST_EVENTS', 3)
meetups = PythonKCMeetups(api_key, num_past_events=num_past_events, http_timeout=6)
def memoize_in_cache(cache_key, cache_timeout_seconds=None):
"""
Caches the result of a no-args function. Further, a backup cache is
maintained, updated as often as the primary but waits much longer to expire
than the primary cache.
NOTE This should all go away once gondor gets schedule celery tasks that
can be used to periodically populate the meetup.com event cache (thereby
allowing visitor requests to avoid the hit).
"""
backup_cache_key = cache_key + '_backup'
backup_cache_timeout_seconds = (cache_timeout_seconds * 10000
if cache_timeout_seconds else
60 * 60 * 24)
def decorator(func):
@wraps(func)
def decorated():
value = cache.get(cache_key)
if not value:
try:
value = func()
cache.set(cache_key, value, cache_timeout_seconds)
cache.set(backup_cache_key, value,
backup_cache_timeout_seconds)
except:
value = cache.get(backup_cache_key)
if value:
logging.exception('Had to load backup value due to '
'exception')
else:
raise
return value
return decorated
return decorator
@memoize_in_cache('next_event', 60 * 5)
def get_next_event():
logging.info('Retrieving next event from Meetup.com')
upcoming_events = meetups.get_upcoming_events()
return upcoming_events[0] if upcoming_events else None
@memoize_in_cache('past_events', 60 * 60)
def get_past_events():
logging.info('Retrieving past events from Meetup.com')
return meetups.get_past_events()[:num_past_events]