123456789101112131415161718192021222324252627282930 |
- from django.db.models import CharField
- from django.db.models.functions import Cast
- from rest_framework import viewsets, permissions
- from rest_framework.response import Response
- from metaservicesynced.serializer import FrequentaddressSerializer
- from metaservicesynced.models import Frequentaddress
- class FrequentaddressMVS(viewsets.ModelViewSet):
- queryset = Frequentaddress.objects.all()
- serializer_class = FrequentaddressSerializer
- permission_classes = [permissions.IsAuthenticated]
- def list(self, request, *args, **kwargs):
- client_id = self.request.query_params.get('client_id')
- queryset = Frequentaddress.objects.all()
- if client_id is not None:
- queryset = queryset.annotate(client_id_int=Cast('client_id', output_field=CharField())).filter(client_id_int=client_id)
- serializer = self.get_serializer(queryset, many=True)
- return Response(serializer.data)
-
- def delete(self, request, *args, **kwargs):
- client_id = self.request.query_params.get('client_id')
- queryset = Frequentaddress.objects.all()
- if not client_id:
- return Response({'error': 'client_id parameter is required'}, status=status.HTTP_400_BAD_REQUEST)
-
- queryset = queryset.annotate(client_id_int=Cast('client_id', output_field=CharField())).filter(client_id_int=client_id)
- queryset.delete()
- return Response({'message': f'Objects with client_id {client_id} were deleted'}, status=status.HTTP_204_NO_CONTENT)
|