Mercurial > public > sg101
annotate bio/flags.py @ 661:15dbe0ccda95
Prevent exceptions when viewing downloads in the admin when the file
doesn't exist on the filesystem. This is usually seen in development
but can also happen in production if the file is missing.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 14 May 2013 21:02:47 -0500 |
parents | 678a1a2ef55a |
children | 89b240fe9297 |
rev | line source |
---|---|
bgneal@609 | 1 """ |
bgneal@609 | 2 This module contains country flag data & functions. |
bgneal@609 | 3 |
bgneal@609 | 4 """ |
bgneal@609 | 5 from __future__ import with_statement |
bgneal@609 | 6 import os.path |
bgneal@609 | 7 import locale |
bgneal@609 | 8 import logging |
bgneal@609 | 9 |
bgneal@609 | 10 import django.utils.simplejson as json |
bgneal@609 | 11 |
bgneal@609 | 12 |
bgneal@609 | 13 # Read flag data from external JSON file: |
bgneal@609 | 14 |
bgneal@609 | 15 FLAG_DATA = {} |
bgneal@609 | 16 |
bgneal@609 | 17 datafile = os.path.join(os.path.split(__file__)[0], 'flag_data.json') |
bgneal@609 | 18 |
bgneal@609 | 19 try: |
bgneal@609 | 20 with open(datafile, 'r') as fp: |
bgneal@609 | 21 FLAG_DATA = json.load(fp, encoding='utf-8') |
bgneal@609 | 22 except IOError: |
bgneal@609 | 23 FLAG_DATA = {} |
bgneal@609 | 24 logging.error("Could not load flag_data.json") |
bgneal@609 | 25 |
bgneal@609 | 26 # Build a choices list for use with Django models, etc. |
bgneal@609 | 27 # The locale is set in order to sort the place names correctly: |
bgneal@609 | 28 |
bgneal@609 | 29 locale.setlocale(locale.LC_ALL, '') |
bgneal@609 | 30 |
bgneal@609 | 31 FLAG_CHOICES = sorted(FLAG_DATA.items(), |
bgneal@609 | 32 cmp=lambda lhs, rhs: locale.strcoll(lhs[1], rhs[1])) |