Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from edx/geo-filter
Browse files Browse the repository at this point in the history
Excluding non-ISO-3166-compliant data
  • Loading branch information
clintonb committed Aug 28, 2014
2 parents 77c70aa + c38e220 commit 0674b79
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion analytics_data_api/v0/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ def country(self):
"""
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):
db_table = 'course_enrollment_location_current'
Expand Down
10 changes: 10 additions & 0 deletions analytics_data_api/v0/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ def test_country(self):
self.assertEqual(country.alpha2, 'US')
instance = G(models.CourseEnrollmentByCountry, country_code=country.alpha2)
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)
7 changes: 7 additions & 0 deletions analytics_data_api/v0/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ class CourseEnrollmentByLocationViewTests(TestCaseWithAuthentication, CourseEnro
model = models.CourseEnrollmentByCountry

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))
return [
{'course_id': str(ce.course_id), 'count': ce.count, 'date': ce.date.strftime(settings.DATE_FORMAT),
Expand All @@ -313,3 +314,9 @@ def setUpClass(cls):
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='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)
11 changes: 11 additions & 0 deletions analytics_data_api/v0/views/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,14 @@ class CourseEnrollmentByLocationView(BaseCourseEnrollmentView):

serializer_class = serializers.CourseEnrollmentByCountrySerializer
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

0 comments on commit 0674b79

Please sign in to comment.