diff --git a/CapX/urls.py b/CapX/urls.py index 185dc5d..879de7c 100644 --- a/CapX/urls.py +++ b/CapX/urls.py @@ -42,7 +42,7 @@ # Alternative version of views, read-only and only returns the __str__ with the id as the key router.register('list_language', ListLanguageViewSet, basename='list_language') -router.register('list_organizations', ListOrganizationViewSet, basename='list_organizations') +router.register('list_affiliation', ListOrganizationViewSet, basename='list_affiliation') router.register('list_skills', ListSkillViewSet, basename='list_skills') router.register('list_territory', ListTerritoryViewSet, basename='list_territory') router.register('list_wikimedia_project', ListWikimediaProjectViewSet, basename='list_wikimedia_project') diff --git a/users/views.py b/users/views.py index 638f38b..233fe34 100644 --- a/users/views.py +++ b/users/views.py @@ -98,15 +98,19 @@ def list(self, request, *args, **kwargs): return Response(response, status=status.HTTP_400_BAD_REQUEST) -# Class to list users by "tags" (skills, languages, territories, projects, affiliation) with format /tags/// +# Class to list users by "tags" (skills, languages, territories, wikimedia_project, affiliation) with format /tags/// # Example: /tags/project/1/ class UsersByTagViewSet(viewsets.ReadOnlyModelViewSet): queryset = Profile.objects.all() serializer_class = ProfileSerializer def retrieve(self, request, *args, **kwargs): - tag_type = self.kwargs['tag_type'] - tag_id = self.kwargs['tag_id'] + tag_type = self.kwargs.get('tag_type') + tag_id = self.kwargs.get('tag_id') + + if tag_type and not tag_id: + response = {'message': 'Please provide a valid tag id.'} + return Response(response, status=status.HTTP_400_BAD_REQUEST) if tag_type == 'skill': known_users = Profile.objects.filter(skills_known=tag_id) @@ -123,7 +127,7 @@ def retrieve(self, request, *args, **kwargs): elif tag_type == 'territory': users = Profile.objects.filter(territory=tag_id) data = [{'id': user.id, 'display_name': user.display_name, 'username': user.user.username, 'profile_image': user.profile_image} for user in users] - elif tag_type == 'project': + elif tag_type == 'wikimedia_project': users = Profile.objects.filter(wikimedia_project=tag_id) data = [{'id': user.id, 'display_name': user.display_name, 'username': user.user.username, 'profile_image': user.profile_image} for user in users] elif tag_type == 'affiliation': @@ -136,5 +140,5 @@ def retrieve(self, request, *args, **kwargs): return Response(data) def list(self, request, *args, **kwargs): - response = {'message': 'Please provide a tag type and a tag id.'} + response = {'message': 'Please provide a tag type and a tag id. Options are: skill, language, territory, wikimedia_project, affiliation.'} return Response(response, status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file