Skip to content

Instantly share code, notes, and snippets.

@moosh3
Created December 9, 2016 21:54
Show Gist options
  • Select an option

  • Save moosh3/bdf713caee167891b547998f342da1cf to your computer and use it in GitHub Desktop.

Select an option

Save moosh3/bdf713caee167891b547998f342da1cf to your computer and use it in GitHub Desktop.
Good example of using try/except
def message_count(request):
''' Provides count of unread messages from the message center for given person '''
person = None
try:
if request.user.is_authenticated():
person = request.user.person
except AttributeError:
pass
if person:
soa = AnalyteSOA(
settings.MESSAGE_CENTER_CONFIG,
settings.MESSAGE_CENTER_PACKAGE_LIST
)
all_messages = []
try:
all_messages = soa.message_center.get_messages(person)
return {'unread_count' : len([msg for msg in all_messages if msg['read_datetime'] is None])}
except (HTTPError, RequestException, AnalyteSOAException):
logging.getLogger('analyte.backend.libs').exception(
'Unable to retrieve messages'
)
return {'unread_count': 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment