Exemplo n.º 1
0
    def create(self, request, *args, **kwargs):
        if not self.get_queryset():
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)
        else:
            self.kwargs['pk'] = self.get_queryset()[0].id
            _check_is_locked(request.data['project'], 1, self.kwargs['pk'])

            instance = self.get_object()
            filepath = os.path.join(MEDIA_ROOT, str(instance.file))
            if os.path.exists(filepath):
                os.remove(filepath)
            req_relation = request.data.get("relation", '')
            if not req_relation:
                request.data["relation"] = instance.relation

            partial = kwargs.pop('partial', False)
            serializer = self.get_serializer(instance,
                                             data=request.data,
                                             partial=partial)
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)
            if getattr(instance, '_prefetched_objects_cache', None):
                # If 'prefetch_related' has been applied to a queryset, we need to
                # forcibly invalidate the prefetch cache on the instance.
                instance._prefetched_objects_cache = {}

            return Response(serializer.data)
Exemplo n.º 2
0
 def destroy(self, request, *args, **kwargs):
     if kwargs.get('pk') and int(kwargs['pk']) != -1:
         _check_is_locked(request.query_params['project'], 1, kwargs['pk'])
         instance = self.get_object()
         filepath = os.path.join(MEDIA_ROOT, str(instance.file))
         if os.path.exists(filepath):
             os.remove(filepath)
         self.perform_destroy(instance)
     elif request.data:
         for content in request.data:
             self.kwargs['pk'] = content['id']
             try:
                 _check_is_locked(content['project'], 1, content['id'])
             except exceptions.NotAcceptable as e:
                 continue
             instance = self.get_object()
             filepath = os.path.join(MEDIA_ROOT, str(instance.file))
             if os.path.exists(filepath):
                 os.remove(filepath)
             self.perform_destroy(instance)
     return Response(status=status.HTTP_204_NO_CONTENT)