Last active
March 9, 2020 18:52
-
-
Save xb4dc0d3/fed1961f5cc39ac6a1dab5c8c3e2f08d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
factory = APIRequestFactory() | |
request = factory.get('/') | |
serializer_context = { | |
'request': Request(request), | |
} | |
class LembagaView(RetrieveAPIView): | |
permission_classes = (IsAuthenticated, ) | |
authentication_class = JSONWebTokenAuthentication | |
def get(self, request): | |
lembaga = Lembaga.objects.all() | |
serializer = LembagaSerializer(lembaga, many=True, context=serializer_context) | |
response = { | |
'success': 'true', | |
'message': 'All lembaga\'s fetched successfully', | |
'data': serializer.data | |
} | |
return Response(response, status=status.HTTP_200_OK) | |
class LembagaViewByNama(RetrieveAPIView): | |
permission_classes = (IsAuthenticated, ) | |
authentication_class = JSONWebTokenAuthentication | |
def get(self, request, nama_lembaga): | |
try: | |
nama = Lembaga.objects.get(nama_lembaga=nama_lembaga) | |
serializer = LembagaSerializer(nama, context=serializer_context) | |
response = { | |
'success': 'true', | |
'message': 'Lembaga {} fetched successfully'.format(nama.get_nama()), | |
'data': serializer.data | |
} | |
return Response(response, status=status.HTTP_200_OK) | |
except Lembaga.DoesNotExist: | |
response = { | |
'message': 'Lembaga does not exist', | |
} | |
return Response(response, status=status.HTTP_404_NOT_FOUND) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment