python apiview view

Solutions on MaxInterview for python apiview view by the best coders in the world

showing results for - "python apiview view"
Frieda
11 Nov 2018
1from rest_framework.views import APIView
2from rest_framework.response import Response
3from rest_framework import authentication, permissions
4from django.contrib.auth.models import User
5
6class ListUsers(APIView):
7    """
8    View to list all users in the system.
9
10    * Requires token authentication.
11    * Only admin users are able to access this view.
12    """
13    authentication_classes = [authentication.TokenAuthentication]
14    permission_classes = [permissions.IsAdminUser]
15
16    def get(self, request, format=None):
17        """
18        Return a list of all users.
19        """
20        usernames = [user.username for user in User.objects.all()]
21        return Response(usernames)