Skip to content

Commit

Permalink
Replaced iso3166 with django-countries
Browse files Browse the repository at this point in the history
This change ensures the country names in the CSVs match those displayed in Insights.
  • Loading branch information
Clinton Blackburn committed Dec 2, 2014
1 parent bf4f5ba commit 891d814
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
29 changes: 28 additions & 1 deletion analytics_data_api/constants/country.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
from iso3166 import Country
"""
This file holds constants and helper methods related to countries. All codes are assumed to be valid ISO 3166 country
codes.
"""
from collections import namedtuple
from django_countries import countries

Country = namedtuple('Country', 'name, alpha2, alpha3, numeric')

UNKNOWN_COUNTRY_CODE = u'UNKNOWN'
UNKNOWN_COUNTRY = Country(UNKNOWN_COUNTRY_CODE, None, None, None)


def _get_country_property(code, property_name):
return unicode(getattr(countries, property_name)(code))


def get_country(code):
if not code:
return UNKNOWN_COUNTRY

name = _get_country_property(code, 'name')
if not name:
return UNKNOWN_COUNTRY

args = []
properties = ['alpha2', 'alpha3', 'numeric']
for property_name in properties:
args.append(_get_country_property(code, property_name))

return Country(name, *args)
16 changes: 15 additions & 1 deletion analytics_data_api/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.contrib.auth.models import User
from django.core.management import call_command, CommandError
from django.test import TestCase

from django_dynamic_fixture import G
from rest_framework.authtoken.models import Token

from analytics_data_api.constants.country import get_country, UNKNOWN_COUNTRY

from analytics_data_api.utils import delete_user_auth_token, set_user_auth_token


Expand Down Expand Up @@ -77,3 +78,16 @@ def test_set_key_conflict(self):
self.assertFalse(Token.objects.filter(user=user2).exists())
call_command('set_api_key', user2.username, key)
self.assertFalse(Token.objects.filter(user=user2).exists())


class CountryTests(TestCase):
def test_get_country(self):
# Countries should be accessible 2 or 3 digit country code
self.assertEqual(get_country('US'), get_country('USA'))

# Use common name for Taiwan
self.assertEqual(get_country('TW').name, 'Taiwan')

# Return unknown country if code is invalid
self.assertEqual(get_country('A1'), UNKNOWN_COUNTRY)
self.assertEqual(get_country(None), UNKNOWN_COUNTRY)
7 changes: 1 addition & 6 deletions analytics_data_api/v0/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import models
from iso3166 import countries

from analytics_data_api.constants import country, genders

Expand Down Expand Up @@ -120,11 +119,7 @@ def country(self):
"""
Returns a Country object representing the country in this model's country_code.
"""
try:
return countries.get(self.country_code)
except (KeyError, ValueError, AttributeError):
# Country code is not valid ISO-3166
return country.UNKNOWN_COUNTRY
return country.get_country(self.country_code)

class Meta(BaseCourseEnrollment.Meta):
db_table = 'course_enrollment_location_current'
Expand Down
5 changes: 2 additions & 3 deletions analytics_data_api/v0/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from django.test import TestCase
from django_dynamic_fixture import G
from iso3166 import countries

from analytics_data_api.v0 import models
from analytics_data_api.constants.country import UNKNOWN_COUNTRY
from analytics_data_api.constants.country import UNKNOWN_COUNTRY, get_country


class CourseEnrollmentByCountryTests(TestCase):
def test_country(self):
country = countries.get('US')
country = get_country('US')
self.assertEqual(country.alpha2, 'US')
instance = G(models.CourseEnrollmentByCountry, country_code=country.alpha2)
self.assertEqual(instance.country, country)
Expand Down
4 changes: 2 additions & 2 deletions analytics_data_api/v0/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from django.conf import settings
from django_dynamic_fixture import G
from iso3166 import countries
import pytz
from opaque_keys.edx.keys import CourseKey
from analytics_data_api.constants.country import get_country

from analytics_data_api.v0 import models
from analytics_data_api.constants import country, enrollment_modes, genders
Expand Down Expand Up @@ -550,7 +550,7 @@ def generate_data(self, course_id=None):

def setUp(self):
super(CourseEnrollmentByLocationViewTests, self).setUp()
self.country = countries.get('US')
self.country = get_country('US')
self.generate_data()


Expand Down
1 change: 1 addition & 0 deletions analyticsdataserver/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
'rest_framework',
'rest_framework.authtoken',
'rest_framework_swagger',
'django_countries',
)

LOCAL_APPS = (
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ djangorestframework==2.4.4 # BSD
ipython==2.1.0 # BSD
django-rest-swagger==0.1.14 # BSD
djangorestframework-csv==1.3.3 # BSD
iso3166==0.1 # MIT
django-countries==3.0.1 # MIT
-e git+https://github.com/edx/opaque-keys.git@d45d0bd8d64c69531be69178b9505b5d38806ce0#egg=opaque-keys

0 comments on commit 891d814

Please sign in to comment.