Repository: carpentries/amy

Issue:
countries listed as filter options for instructor recruitment
In the instructor view, currently these five options show up as filter options.  Why are these five countries listed?  They do not represent countries that have active recruitment processes open.

![image](https://user-images.githubusercontent.com/829690/198036229-1478d7f0-1d62-4e66-8c7d-6a4ad2299f3e.png)



Discussion:
Should we rename "Event country" to “Region” and move it down
and add a new field to actually represent the countries?
The `country` field in the database represents actual countries, not regions.  The error is in how they are retrieved to build this drop down menu.  
This should be resolved so that only countries with active open recruitment processes are listed.

Resolving patch:
diff --git a/amy/dashboard/filters.py b/amy/dashboard/filters.py
--- a/amy/dashboard/filters.py
+++ b/amy/dashboard/filters.py
@@ -26,7 +26,9 @@ class UpcomingTeachingOpportunitiesFilter(AMYFilterSet):
     )
 
     country = AllCountriesMultipleFilter(
-        field_name="event__country", widget=Select2MultipleWidget
+        field_name="event__country",
+        widget=Select2MultipleWidget,
+        extend_countries=False,
     )
 
     curricula = filters.ModelMultipleChoiceFilter(
diff --git a/amy/workshops/filters.py b/amy/workshops/filters.py
--- a/amy/workshops/filters.py
+++ b/amy/workshops/filters.py
@@ -41,7 +41,7 @@ def extend_country_choices(
     For example this: ['AZ', 'BA', 'BY', 'FM', 'GD', 'W3']  # W3 is custom
     will be changes to this: ['AZ', 'BA', 'BY', 'FM', 'GD', ('W3', 'Online')]
     """
-    countries: list = list(choices)
+    countries: list[Union[str, tuple[str, str]]] = list(choices)
     common: set[str] = countries_override.keys() & set(choices)
     for country in common:
         countries.remove(country)
@@ -49,9 +49,13 @@ def extend_country_choices(
     return countries
 
 
-class AllCountriesFilter(django_filters.ChoiceFilter):
+class BaseCountriesFilter(django_filters.Filter):
+    def __init__(self, *args, **kwargs):
+        self.extend_countries = kwargs.pop("extend_countries", True)
+        super().__init__(*args, **kwargs)
+
     def _get_countries(self):
-        qs = self.model._default_manager.distinct()
+        qs = self.model._default_manager.distinct()  # type: ignore
         qs = qs.order_by(self.field_name).values_list(self.field_name, flat=True)
         choices = [o for o in qs if o]
         return choices
@@ -59,30 +63,29 @@ def _get_countries(self):
     @property
     def field(self):
         choices = self._get_countries()
-        overrides = extend_country_choices(choices, settings.COUNTRIES_OVERRIDE)
-        countries = Countries()
-        countries.only = overrides
+        if self.extend_countries:
+            only = extend_country_choices(choices, settings.COUNTRIES_OVERRIDE)
+        else:
+            only = {
+                abbrv: name
+                for abbrv, name in Countries().countries.items()
+                if abbrv in choices
+            }
 
+        countries = Countries()
+        countries.only = only  # type: ignore
         self.extra["choices"] = list(countries)
         return super().field
 
 
-class AllCountriesMultipleFilter(django_filters.MultipleChoiceFilter):
-    def _get_countries(self):
-        qs = self.model._default_manager.distinct()
-        qs = qs.order_by(self.field_name).values_list(self.field_name, flat=True)
-        choices = [o for o in qs if o]
-        return choices
+class AllCountriesFilter(BaseCountriesFilter, django_filters.ChoiceFilter):
+    pass
 
-    @property
-    def field(self):
-        choices = self._get_countries()
-        overrides = extend_country_choices(choices, settings.COUNTRIES_OVERRIDE)
-        countries = Countries()
-        countries.only = overrides
 
-        self.extra["choices"] = list(countries)
-        return super().field
+class AllCountriesMultipleFilter(
+    BaseCountriesFilter, django_filters.MultipleChoiceFilter
+):
+    pass
 
 
 class ForeignKeyAllValuesFilter(django_filters.ChoiceFilter):


Test patch:
diff --git a/amy/workshops/tests/test_filters.py b/amy/workshops/tests/test_filters.py
--- a/amy/workshops/tests/test_filters.py
+++ b/amy/workshops/tests/test_filters.py
@@ -57,7 +57,7 @@ def test_extra_choices(self):
         """Regression test for https://github.com/carpentries/amy/issues/1996."""
         # Arrange
         filter_ = AllCountriesFilter()
-        filter_._get_countries = MagicMock(return_value=["PL", "US", "GB", "W3", "EU"])
+        filter_._get_countries = MagicMock(return_value=["PL", "GB", "US"])
 
         # Act
         filter_.field
@@ -74,13 +74,31 @@ def test_extra_choices(self):
             ],
         )
 
+    def test_extra_choices_not_extended(self) -> None:
+        # Arrange
+        filter_ = AllCountriesFilter(extend_countries=False)
+        filter_._get_countries = MagicMock(return_value=["PL", "GB", "US"])
+
+        # Act
+        filter_.field
+
+        # Assert
+        self.assertEqual(
+            filter_.extra["choices"],
+            [
+                ("PL", "Poland"),
+                ("GB", "United Kingdom"),
+                ("US", "United States"),
+            ],
+        )
+
 
 class TestAllCountriesMultipleFilterCustomCountries(TestCase):
     def test_extra_choices(self):
         """Regression test for https://github.com/carpentries/amy/issues/1996."""
         # Arrange
         filter_ = AllCountriesMultipleFilter()
-        filter_._get_countries = MagicMock(return_value=["PL", "US", "GB", "W3", "EU"])
+        filter_._get_countries = MagicMock(return_value=["PL", "GB", "US"])
 
         # Act
         filter_.field
@@ -96,3 +114,21 @@ def test_extra_choices(self):
                 ("US", "United States"),
             ],
         )
+
+    def test_extra_choices_not_extended(self) -> None:
+        # Arrange
+        filter_ = AllCountriesMultipleFilter(extend_countries=False)
+        filter_._get_countries = MagicMock(return_value=["PL", "GB", "US"])
+
+        # Act
+        filter_.field
+
+        # Assert
+        self.assertEqual(
+            filter_.extra["choices"],
+            [
+                ("PL", "Poland"),
+                ("GB", "United Kingdom"),
+                ("US", "United States"),
+            ],
+        )