Repository: carpentries/amy

Issue:
Airports should be sorted
I just went to update my user profile, and having moved, needed to update my nearest airport. Unfortunately, the drop-down list of airports seems to be listed in database insertion order (rather than alphabetical), so I had to meticulously read each of the dozens of entries to find my regional airport.

It should be pretty easy to update the Django Airport model [here](https://github.com/swcarpentry/amy/blob/develop/workshops/models.py#L213) to [sort](https://docs.djangoproject.com/en/1.11/ref/models/options/#ordering) by IATA code by default. Something like this:

```python
class Airport(models.Model):
    ...
    class Meta:
        ordering = ['iata']
```


Airports should be sorted
I just went to update my user profile, and having moved, needed to update my nearest airport. Unfortunately, the drop-down list of airports seems to be listed in database insertion order (rather than alphabetical), so I had to meticulously read each of the dozens of entries to find my regional airport.

It should be pretty easy to update the Django Airport model [here](https://github.com/swcarpentry/amy/blob/develop/workshops/models.py#L213) to [sort](https://docs.djangoproject.com/en/1.11/ref/models/options/#ordering) by IATA code by default. Something like this:

```python
class Airport(models.Model):
    ...
    class Meta:
        ordering = ['iata']
```




Discussion:

Resolving patch:
diff --git a/workshops/migrations/0128_auto_20170710_1130.py b/workshops/migrations/0128_auto_20170710_1130.py
new file mode 100644
--- /dev/null
+++ b/workshops/migrations/0128_auto_20170710_1130.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.13 on 2017-07-10 16:30
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('workshops', '0127_make_family_name_optional'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='airport',
+            options={'ordering': ('iata',)},
+        ),
+    ]
diff --git a/workshops/models.py b/workshops/models.py
--- a/workshops/models.py
+++ b/workshops/models.py
@@ -226,6 +226,11 @@ def __str__(self):
     def get_absolute_url(self):
         return reverse('airport_details', args=[str(self.iata)])
 
+    class Meta:
+        ordering = (
+            'iata',
+        )
+
 #------------------------------------------------------------
 
 class PersonManager(BaseUserManager):


Test patch:
diff --git a/workshops/test/base.py b/workshops/test/base.py
--- a/workshops/test/base.py
+++ b/workshops/test/base.py
@@ -88,6 +88,10 @@ def _setUpOrganizations(self):
     def _setUpAirports(self):
         '''Set up airport objects.'''
 
+        self.airport_0_10 = Airport.objects.create(
+            iata='ZZZ', fullname='Airport 0x10',
+            latitude=0.0, longitude=10.0,
+        )
         self.airport_0_0 = Airport.objects.create(
             iata='AAA', fullname='Airport 0x0', country='AL',  # AL for Albania
             latitude=0.0, longitude=0.0,
diff --git a/workshops/test/test_airport.py b/workshops/test/test_airport.py
--- a/workshops/test/test_airport.py
+++ b/workshops/test/test_airport.py
@@ -47,3 +47,9 @@ def test_airport_delete(self):
 
             with self.assertRaises(Airport.DoesNotExist):
                 Airport.objects.get(iata=iata)
+
+    def test_airport_ordering(self):
+        """Make sure that airports are listed in alphabetical order.
+        See #1193."""
+        first_airport = Airport.objects.all()[0]
+        assert first_airport.iata == 'AAA'