Skip to content

Instantly share code, notes, and snippets.

@xb4dc0d3
Last active March 9, 2020 18:52
Show Gist options
  • Save xb4dc0d3/fed1961f5cc39ac6a1dab5c8c3e2f08d to your computer and use it in GitHub Desktop.
Save xb4dc0d3/fed1961f5cc39ac6a1dab5c8c3e2f08d to your computer and use it in GitHub Desktop.
...
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