diff bio/flags.py @ 609:678a1a2ef55a

For issue 16, add country flag icons to user profiles & forum posts.
author Brian Neal <bgneal@gmail.com>
date Sat, 28 Jul 2012 15:12:09 -0500
parents
children 89b240fe9297
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bio/flags.py	Sat Jul 28 15:12:09 2012 -0500
@@ -0,0 +1,32 @@
+"""
+This module contains country flag data & functions.
+
+"""
+from __future__ import with_statement
+import os.path
+import locale
+import logging
+
+import django.utils.simplejson as json
+
+
+# Read flag data from external JSON file:
+
+FLAG_DATA = {}
+
+datafile = os.path.join(os.path.split(__file__)[0], 'flag_data.json')
+
+try:
+    with open(datafile, 'r') as fp:
+        FLAG_DATA = json.load(fp, encoding='utf-8')
+except IOError:
+    FLAG_DATA = {}
+    logging.error("Could not load flag_data.json")
+
+# Build a choices list for use with Django models, etc.
+# The locale is set in order to sort the place names correctly:
+
+locale.setlocale(locale.LC_ALL, '')
+
+FLAG_CHOICES = sorted(FLAG_DATA.items(),
+        cmp=lambda lhs, rhs: locale.strcoll(lhs[1], rhs[1]))