def application_detail(request, pk): if not Login.tokenAuth(request) and not Login.auth(request) == True: return Response(status=status.HTTP_401_UNAUTHORIZED) else: # If exists try: ap = Application.objects.get(id=pk) except Application.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ApplicationSerializer(ap, context={'request', request}) return Response(serializer.data) if request.method == 'PUT': serializer = ApplicationSerializer(ap, context={'request':request}, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': # If admin if Login.getCurrentUser(request).access_lvl == 1: ap.delete() return Response(status=status.HTTP_204_NO_CONTENT) else: Response(status=status.HTTP_401_UNAUTHORIZED) # If another is fail return Response(status=status.HTTP_400_BAD_REQUEST)
def applications_list(request): if not Login.tokenAuth(request) and not Login.auth(request) == True: return Response(status=status.HTTP_401_UNAUTHORIZED) else: if request.method == 'GET': aps = Application.objects.all() serializer = ApplicationSerializer(aps, many=True) return Response(serializer.data, status=status.HTTP_200_OK) if request.method == 'POST': serializer = ApplicationSerializer(context={'request': request}, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) # IF all another is fail return Response(status=status.HTTP_400_BAD_REQUEST)