micro_service_placeholder/microService/api/baseViewSet.py

51 lines
2.3 KiB
Python
Raw Normal View History

2024-11-30 05:34:13 +00:00
from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend
from api.helper import Helper
class BaseViewSet(viewsets.ModelViewSet):
filter_backends = [DjangoFilterBackend, filters.SearchFilter]
def list(self, request, *args, **kwargs):
helper = Helper()
if helper.checkValidUser(request):
queryset = self.get_queryset()
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
return Response({"message": "Invalid User"}, status=status.HTTP_401_UNAUTHORIZED)
def retrieve(self, request, *args, **kwargs):
helper = Helper()
if helper.checkValidUser(request):
return super().retrieve(request, *args, **kwargs)
return Response({"message": "Invalid User"}, status=status.HTTP_401_UNAUTHORIZED)
def create(self, request, *args, **kwargs):
helper = Helper()
if helper.checkValidUser(request):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response({"message": "Invalid User"}, status=status.HTTP_401_UNAUTHORIZED)
def update(self, request, *args, **kwargs):
helper = Helper()
if helper.checkValidUser(request):
return super().update(request, *args, **kwargs)
return Response({"message": "Invalid User"}, status=status.HTTP_401_UNAUTHORIZED)
def partial_update(self, request, *args, **kwargs):
helper = Helper()
if helper.checkValidUser(request):
return super().partial_update(request, *args, **kwargs)
return Response({"message": "Invalid User"}, status=status.HTTP_401_UNAUTHORIZED)
def destroy(self, request, *args, **kwargs):
helper = Helper()
if helper.checkValidUser(request):
return super().destroy(request, *args, **kwargs)
return Response({"message": "Invalid User"}, status=status.HTTP_401_UNAUTHORIZED)