Repository: ocadotechnology/codeforlife-portal

Issue:
Find out how many users are registered in UK and outside of the UK



Discussion:

Resolving patch:
diff --git a/portal/urls.py b/portal/urls.py
--- a/portal/urls.py
+++ b/portal/urls.py
@@ -41,7 +41,7 @@
 from two_factor.views import DisableView, BackupTokensView, SetupCompleteView, SetupView, \
     ProfileView, QRGeneratorView
 
-from portal.views.api import registered_users, last_connected_since
+from portal.views.api import registered_users, last_connected_since, number_users_per_country
 from portal.views.admin import aggregated_data, schools_map, admin_login
 from portal.views.teacher.solutions_level_selector import levels
 from portal.permissions import teacher_verified
@@ -158,5 +158,6 @@
     url(r'^api/', include([
         url(r'^registered/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', registered_users, name="registered-users"),
         url(r'^lastconnectedsince/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', last_connected_since, name="last-connected-since"),
+        url(r'^userspercountry/(?P<country>(AF|AX|AL|DZ|AS|AD|AO|AI|AQ|AG|AR|AM|AW|AU|AT|AZ|BS|BH|BD|BB|BY|BE|BZ|BJ|BM|BT|BO|BQ|BA|BW|BV|BR|IO|BN|BG|BF|BI|KH|CM|CA|CV|KY|CF|TD|CL|CN|CX|CC|CO|KM|CG|CD|CK|CR|CI|HR|CU|CW|CY|CZ|DK|DJ|DM|DO|EC|EG|SV|GQ|ER|EE|ET|FK|FO|FJ|FI|FR|GF|PF|TF|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GU|GT|GG|GN|GW|GY|HT|HM|VA|HN|HK|HU|IS|IN|ID|IR|IQ|IE|IM|IL|IT|JM|JP|JE|JO|KZ|KE|KI|KP|KR|KW|KG|LA|LV|LB|LS|LR|LY|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MH|MQ|MR|MU|YT|MX|FM|MD|MC|MN|ME|MS|MA|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NU|NF|MP|NO|OM|PK|PW|PS|PA|PG|PY|PE|PH|PN|PL|PT|PR|QA|RE|RO|RU|RW|BL|SH|KN|LC|MF|PM|VC|WS|SM|ST|SA|SN|RS|SC|SL|SG|SX|SK|SI|SB|SO|ZA|GS|SS|ES|LK|SD|SR|SJ|SZ|SE|CH|SY|TW|TJ|TZ|TH|TL|TG|TK|TO|TT|TN|TR|TM|TC|TV|UG|UA|AE|GB|US|UM|UY|UZ|VU|VE|VN|VG|VI|WF|EH|YE|ZM|ZW))/$', number_users_per_country, name="number_users_per_country"),
     ])),
 )
diff --git a/portal/views/api.py b/portal/views/api.py
--- a/portal/views/api.py
+++ b/portal/views/api.py
@@ -42,6 +42,7 @@
 from rest_framework.response import Response
 
 from django.contrib.auth.models import User
+from portal.models import Teacher, Student
 
 
 @api_view(('GET',))
@@ -60,3 +61,13 @@ def last_connected_since(request, year, month, day):
         return Response(nbr_active_users)
     except ValueError:
         return HttpResponse(status=404)
+
+
+@api_view(('GET',))
+def number_users_per_country(request, country):
+    try:
+        nbr_reg = Teacher.objects.filter(school__country__exact=country).count() + \
+                  Student.objects.filter(class_field__teacher__school__country__exact=country).count()
+        return Response(nbr_reg)
+    except ValueError:
+        return HttpResponse(status=404)


Test patch:
diff --git a/portal/tests/test_api.py b/portal/tests/test_api.py
--- a/portal/tests/test_api.py
+++ b/portal/tests/test_api.py
@@ -66,6 +66,12 @@ def test_invalid_date_lastconnectedsince(self):
         response = self.client.get(url)
         assert_that(response, has_status_code(status.HTTP_404_NOT_FOUND))
 
+    def test_valid_country_userspercountry(self):
+        url = reverse('number_users_per_country', kwargs={'country': 'GB'})
+        response = self.client.get(url)
+        assert_that(response, has_status_code(status.HTTP_200_OK))
+        assert_that(isinstance(response.data, int))
+
 
 def has_status_code(status_code):
     return HasStatusCode(status_code)