Created
March 19, 2020 08:08
-
-
Save xb4dc0d3/bbed1832c61dec07e513238e2e1ac6fd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Bad | |
class LembagaViewByNama(RetrieveAPIView): | |
permission_classes = (IsAuthenticated, ) | |
authentication_class = JSONWebTokenAuthentication | |
def get(self, request, nama_lembaga): | |
nama = Lembaga.objects.get(nama_lembaga=nama_lembaga) | |
if nama != None: # jika semakin banyak conditional maka akan semakin sulit | |
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) | |
else: | |
response = { | |
'message': 'Lembaga does not exist', | |
} | |
return Response(response, status=status.HTTP_404_NOT_FOUND) | |
# Good | |
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