GetTogether/simple_ga/api.py

36 lines
1.1 KiB
Python

class GAFailure(Exception):
pass
def add_event(request, action, category=None, label=None, value=None, fail_silently=False):
"""
Attempt to add a message to the request using the 'messages' app.
"""
try:
events = request._ga_events
except AttributeError:
if not hasattr(request, 'META'):
raise TypeError(
"add_message() argument must be an HttpRequest object, not "
"'%s'." % request.__class__.__name__
)
if not fail_silently:
raise GAFailure(
'You cannot add messages without installing '
'django.contrib.messages.middleware.MessageMiddleware'
)
else:
return events.add(action, category, label, value)
def get_events(request):
"""
Return the message storage on the request if it exists, otherwise return
an empty list.
"""
if not hasattr(request, '_ga_events'):
return []
next_event = request._ga_events.pop()
while next_event is not None:
yield next_event
next_event = request._ga_events.pop();