Repository: carpentries/amy

Issue:
Filter workshops by country in the API
We have this in the UI (#1151), could we have filtering by country via the API too in:
https://amy.software-carpentry.org/api/v1/events/ and https://amy.software-carpentry.org/api/v1/events/published/ ?


Discussion:

Resolving patch:
diff --git a/api/filters.py b/api/filters.py
--- a/api/filters.py
+++ b/api/filters.py
@@ -31,8 +31,10 @@ class Meta:
         model = Event
         fields = (
             'completed', 'tags',
+            'administrator', 'host',
             'start', 'start_before', 'start_after',
             'end', 'end_before', 'end_after',
+            'country',
         )
 
 
@@ -56,9 +58,6 @@ def filter_instructors(queryset, name, value):
         return queryset
 
 
-
-
-
 class PersonFilter(filters.FilterSet):
     is_instructor = filters.BooleanFilter(method=filter_instructors,
                                           label='Is instructor?')
@@ -73,7 +72,7 @@ class Meta:
         model = Person
         fields = (
             'badges', 'username', 'personal', 'middle', 'family', 'email',
-            'may_contact', 'publish_profile', 'github',
+            'may_contact', 'publish_profile', 'github', 'country',
         )
 
 
diff --git a/api/serializers.py b/api/serializers.py
--- a/api/serializers.py
+++ b/api/serializers.py
@@ -191,6 +191,7 @@ class Meta:
 class PersonSerializer(serializers.ModelSerializer):
     airport = serializers.HyperlinkedRelatedField(
         read_only=True, view_name='api:airport-detail', lookup_field='iata')
+    country = serializers.CharField()
     lessons = serializers.SlugRelatedField(
         many=True, read_only=True, slug_field='name')
     domains = serializers.SlugRelatedField(
@@ -212,7 +213,7 @@ class Meta:
         model = Person
         fields = (
             'personal', 'middle', 'family', 'email', 'gender', 'may_contact',
-            'publish_profile', 'airport',
+            'publish_profile', 'airport', 'country',
             'github', 'twitter', 'url', 'orcid', 'username', 'notes',
             'affiliation', 'badges', 'lessons', 'domains', 'awards', 'tasks',
         )
diff --git a/api/views.py b/api/views.py
--- a/api/views.py
+++ b/api/views.py
@@ -294,44 +294,12 @@ def get_object(self):
 
 class PublishedEvents(ListAPIView):
     """List published events."""
-
     # only events that have both a starting date and a URL
     permission_classes = (IsAuthenticatedOrReadOnly, )
     paginator = None  # disable pagination
-
     serializer_class = ExportEventSerializer
-
-    metadata_class = QueryMetadata
-
-    def get_queryset(self):
-        """Optionally restrict the returned event set to events hosted by
-        specific host or administered by specific admin."""
-        queryset = Event.objects.published_events()
-
-        administrator = self.request.query_params.get('administrator', None)
-        if administrator is not None:
-            queryset = queryset.filter(administrator__pk=administrator)
-
-        host = self.request.query_params.get('host', None)
-        if host is not None:
-            queryset = queryset.filter(host__pk=host)
-
-        tags = self.request.query_params.getlist('tag', None)
-        if tags:
-            tags = Tag.objects.filter(name__in=tags)
-            for tag in tags:
-                queryset = queryset.filter(tags=tag)
-
-        return queryset
-
-    def get_query_params_description(self):
-        return {
-            'administrator': 'ID of the organization responsible for admin '
-                             'work on events.',
-            'host': 'ID of the organization hosting the event.',
-            'tag': "Events' tag(s). You can use this parameter multiple "
-                   "times.",
-        }
+    filterset_class = EventFilter
+    queryset = Event.objects.published_events()
 
 
 class UserTodoItems(ListAPIView):


Test patch: