Repository: carpentries/amy

Issue:
airport IATA code should be case insensitive
title says it all
airport IATA code should be case insensitive
title says it all


Discussion:
@maneesha Can you point out where does the case of the IATA causes problems?
Look at this profile update request: 
https://amy.software-carpentry.org/workshops/profile_update/480/

"dca" is not accepted - but DCA airport exisits.
@maneesha Can you point out where does the case of the IATA causes problems?
Look at this profile update request: 
https://amy.software-carpentry.org/workshops/profile_update/480/

"dca" is not accepted - but DCA airport exisits.

Resolving patch:
diff --git a/api/views.py b/api/views.py
--- a/api/views.py
+++ b/api/views.py
@@ -688,5 +688,6 @@ class AirportViewSet(viewsets.ReadOnlyModelViewSet):
     permission_classes = (IsAuthenticated, IsAdmin)
     queryset = Airport.objects.all()
     serializer_class = AirportSerializer
-    lookup_field = 'iata'
+    lookup_field = 'iata__iexact'
+    lookup_url_kwarg = 'iata'
     pagination_class = StandardResultsSetPagination
diff --git a/workshops/views.py b/workshops/views.py
--- a/workshops/views.py
+++ b/workshops/views.py
@@ -2261,7 +2261,7 @@ def profileupdaterequest_details(request, request_id):
         ).exists()
 
     try:
-        airport = Airport.objects.get(iata=update_request.airport_iata)
+        airport = Airport.objects.get(iata__iexact=update_request.airport_iata)
     except Airport.DoesNotExist:
         airport = None
 
@@ -2314,7 +2314,7 @@ def profileupdaterequest_accept(request, request_id, person_id=None):
     """
     profileupdate = get_object_or_404(ProfileUpdateRequest, active=True,
                                       pk=request_id)
-    airport = get_object_or_404(Airport, iata=profileupdate.airport_iata)
+    airport = get_object_or_404(Airport, iata__iexact=profileupdate.airport_iata)
 
     if person_id is None:
         person = Person()


Test patch:
diff --git a/workshops/test/test_profile_update_request.py b/workshops/test/test_profile_update_request.py
--- a/workshops/test/test_profile_update_request.py
+++ b/workshops/test/test_profile_update_request.py
@@ -3,7 +3,13 @@
 from django.core.urlresolvers import reverse
 
 from .base import TestBase
-from ..models import ProfileUpdateRequest, Person, KnowledgeDomain, Lesson
+from ..models import (
+    ProfileUpdateRequest,
+    Person,
+    KnowledgeDomain,
+    Lesson,
+    Airport,
+)
 
 
 class TestProfileUpdateRequest(TestBase):
@@ -174,6 +180,42 @@ def test_request_accepted_new_person_added(self):
                          set(Lesson.objects.all()[0:2]))
 
 
+class TestUpdateRequestWithLowercasedAirport(TestBase):
+    """Regression tests for [#1109] bug -- airport IATA code should be case
+    insensitive.
+
+    [#1109]: https://github.com/swcarpentry/amy/issues/1109"""
+
+    def setUp(self):
+        self._setUpAirports()
+        self._setUpLessons()
+        self._setUpBadges()
+        self._setUpInstructors()
+        self._setUpUsersAndLogin()
+
+        self.request = ProfileUpdateRequest.objects.create(
+            personal='Harry', family='Potter', email='harry@potter.com',
+            airport_iata='aaa', affiliation='Hogwarts', active=True,
+        )
+
+    def test_the_airport_is_matched_when_displaying_the_request(self):
+        rv = self.client.get(reverse('profileupdaterequest_details',
+                                     args=[self.request.pk]))
+        self.assertEqual(rv.context['airport'],
+                         Airport.objects.get(iata='AAA'))
+
+    def test_the_aiport_is_matched_when_accepting_the_request(self):
+        expected_airport = Airport.objects.get(iata='AAA')
+
+        person = Person.objects.get(personal='Harry', family='Potter')
+        self.assertNotEqual(person.airport, expected_airport)
+
+        self.client.get(reverse('profileupdaterequest_accept',
+                                args=[self.request.pk, person.pk]))
+        person.refresh_from_db()
+        self.assertEqual(person.airport, expected_airport)
+
+
 @patch('workshops.github_auth.github_username_to_uid', lambda username: None)
 class TestProfileUpdateRequestsViews(TestBase):
     def setUp(self):