frequentaddress.py 1.5 KB

123456789101112131415161718192021222324252627282930
  1. from django.db.models import CharField
  2. from django.db.models.functions import Cast
  3. from rest_framework import viewsets, permissions
  4. from rest_framework.response import Response
  5. from metaservicesynced.serializer import FrequentaddressSerializer
  6. from metaservicesynced.models import Frequentaddress
  7. class FrequentaddressMVS(viewsets.ModelViewSet):
  8. queryset = Frequentaddress.objects.all()
  9. serializer_class = FrequentaddressSerializer
  10. permission_classes = [permissions.IsAuthenticated]
  11. def list(self, request, *args, **kwargs):
  12. client_id = self.request.query_params.get('client_id')
  13. queryset = Frequentaddress.objects.all()
  14. if client_id is not None:
  15. queryset = queryset.annotate(client_id_int=Cast('client_id', output_field=CharField())).filter(client_id_int=client_id)
  16. serializer = self.get_serializer(queryset, many=True)
  17. return Response(serializer.data)
  18. def delete(self, request, *args, **kwargs):
  19. client_id = self.request.query_params.get('client_id')
  20. queryset = Frequentaddress.objects.all()
  21. if not client_id:
  22. return Response({'error': 'client_id parameter is required'}, status=status.HTTP_400_BAD_REQUEST)
  23. queryset = queryset.annotate(client_id_int=Cast('client_id', output_field=CharField())).filter(client_id_int=client_id)
  24. queryset.delete()
  25. return Response({'message': f'Objects with client_id {client_id} were deleted'}, status=status.HTTP_204_NO_CONTENT)