Created
December 9, 2016 21:54
-
-
Save moosh3/bdf713caee167891b547998f342da1cf to your computer and use it in GitHub Desktop.
Good example of using try/except
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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