Commit 0674b790 by Clinton Blackburn

Merge pull request #19 from edx/geo-filter

Excluding non-ISO-3166-compliant data
parents 77c70aa1 c38e220a
...@@ -104,7 +104,11 @@ class CourseEnrollmentByCountry(BaseCourseEnrollment): ...@@ -104,7 +104,11 @@ class CourseEnrollmentByCountry(BaseCourseEnrollment):
""" """
Returns a Country object representing the country in this model's country_code. Returns a Country object representing the country in this model's country_code.
""" """
return countries.get(self.country_code) try:
return countries.get(self.country_code)
except (KeyError, ValueError):
# Country code is not valid ISO-3166
return None
class Meta(BaseCourseEnrollment.Meta): class Meta(BaseCourseEnrollment.Meta):
db_table = 'course_enrollment_location_current' db_table = 'course_enrollment_location_current'
......
...@@ -23,3 +23,13 @@ class CourseEnrollmentByCountryTests(TestCase): ...@@ -23,3 +23,13 @@ class CourseEnrollmentByCountryTests(TestCase):
self.assertEqual(country.alpha2, 'US') self.assertEqual(country.alpha2, 'US')
instance = G(models.CourseEnrollmentByCountry, country_code=country.alpha2) instance = G(models.CourseEnrollmentByCountry, country_code=country.alpha2)
self.assertEqual(instance.country, country) self.assertEqual(instance.country, country)
def test_invalid_country(self):
instance = G(models.CourseEnrollmentByCountry, country_code='')
self.assertIsNone(instance.country)
instance = G(models.CourseEnrollmentByCountry, country_code='A1')
self.assertIsNone(instance.country)
instance = G(models.CourseEnrollmentByCountry, country_code='GobbledyGoop!')
self.assertIsNone(instance.country)
...@@ -299,6 +299,7 @@ class CourseEnrollmentByLocationViewTests(TestCaseWithAuthentication, CourseEnro ...@@ -299,6 +299,7 @@ class CourseEnrollmentByLocationViewTests(TestCaseWithAuthentication, CourseEnro
model = models.CourseEnrollmentByCountry model = models.CourseEnrollmentByCountry
def get_expected_response(self, *args): def get_expected_response(self, *args):
args = [arg for arg in args if arg.country_code not in ['', 'A1', 'A2', 'AP', 'EU', 'O1', 'UNKNOWN']]
args = sorted(args, key=lambda item: (item.date, item.course_id, item.country.alpha3)) args = sorted(args, key=lambda item: (item.date, item.course_id, item.country.alpha3))
return [ return [
{'course_id': str(ce.course_id), 'count': ce.count, 'date': ce.date.strftime(settings.DATE_FORMAT), {'course_id': str(ce.course_id), 'count': ce.count, 'date': ce.date.strftime(settings.DATE_FORMAT),
...@@ -313,3 +314,9 @@ class CourseEnrollmentByLocationViewTests(TestCaseWithAuthentication, CourseEnro ...@@ -313,3 +314,9 @@ class CourseEnrollmentByLocationViewTests(TestCaseWithAuthentication, CourseEnro
G(cls.model, course_id=cls.course_id, country_code='US', count=455, date=cls.date) G(cls.model, course_id=cls.course_id, country_code='US', count=455, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='CA', count=356, date=cls.date) G(cls.model, course_id=cls.course_id, country_code='CA', count=356, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='IN', count=12, date=cls.date - datetime.timedelta(days=29)) G(cls.model, course_id=cls.course_id, country_code='IN', count=12, date=cls.date - datetime.timedelta(days=29))
G(cls.model, course_id=cls.course_id, country_code='', count=356, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='A1', count=1, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='A2', count=2, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='AP', count=1, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='EU', count=4, date=cls.date)
G(cls.model, course_id=cls.course_id, country_code='O1', count=7, date=cls.date)
...@@ -214,3 +214,14 @@ class CourseEnrollmentByLocationView(BaseCourseEnrollmentView): ...@@ -214,3 +214,14 @@ class CourseEnrollmentByLocationView(BaseCourseEnrollmentView):
serializer_class = serializers.CourseEnrollmentByCountrySerializer serializer_class = serializers.CourseEnrollmentByCountrySerializer
model = models.CourseEnrollmentByCountry model = models.CourseEnrollmentByCountry
def get_queryset(self):
queryset = super(CourseEnrollmentByLocationView, self).get_queryset()
# Remove all items where country is None
items = [item for item in queryset.all() if item.country is not None]
# Note: We are returning a list, instead of a queryset. This is
# acceptable since the consuming code simply expects the returned
# value to be iterable, not necessarily a queryset.
return items
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment