1
0

orders_local.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from django.http import Http404, HttpResponse
  2. from django.shortcuts import get_object_or_404
  3. from webservice_running.serializer import OrdersLocalSerializer
  4. from rest_framework import viewsets, permissions, status
  5. from rest_framework.response import Response
  6. from rest_framework.exceptions import NotFound
  7. from webservice_running.models import OrdersLocal
  8. # from dbsynce.models import Orders, Service, ServiceType, Provider, Client
  9. # from sharix_admin.models import SharixUser
  10. # from tickets.models import Task
  11. # from tickets.serializer import TaskSerializer
  12. class OrdersLocalMVS(viewsets.ModelViewSet):
  13. queryset = OrdersLocal.objects.all()
  14. serializer_class = OrdersLocalSerializer
  15. #permission_classes = [IsOwnerOrReadOnly]
  16. permission_classes = [permissions.IsAuthenticated]
  17. def get_object(self):
  18. """
  19. Returns the object the view is displaying.
  20. You may want to override this if you need to provide non-standard
  21. queryset lookups. Eg if objects are referenced using multiple
  22. keyword arguments in the url conf.
  23. """
  24. # queryset = self.filter_queryset(self.get_queryset())
  25. queryset = OrdersLocal.objects.select_related('order_synced__ticket')
  26. # Perform the lookup filtering.
  27. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
  28. assert lookup_url_kwarg in self.kwargs, (
  29. 'Expected view %s to be called with a URL keyword argument '
  30. 'named "%s". Fix your URL conf, or set the `.lookup_field` '
  31. 'attribute on the view correctly.' %
  32. (self.__class__.__name__, lookup_url_kwarg)
  33. )
  34. filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
  35. obj = get_object_or_404(queryset, **filter_kwargs)
  36. # May raise a permission denied
  37. self.check_object_permissions(self.request, obj)
  38. return obj
  39. def update(self, request, *args, **kwargs):
  40. partial = kwargs.pop('partial', False)
  41. instance = self.get_object()# OrdersLocal.objects.select_related('order_synced__ticket').get(id=1)
  42. serializer = self.get_serializer(instance, data=request.data, partial=partial)
  43. # for serializer in serializers:
  44. serializer.is_valid(raise_exception=True)
  45. self.perform_update(serializer)
  46. if getattr(instance, '_prefetched_objects_cache', None):
  47. # If 'prefetch_related' has been applied to a queryset, we need to
  48. # forcibly invalidate the prefetch cache on the instance.
  49. instance._prefetched_objects_cache = {}
  50. return Response(serializer.data)