Exemple #1
0
    def relocate(self, request, path, method, *args, **kwargs):
        """
        Relocate an element

        Handles copying and moving of all sorts of elements
        :param request:
        :param path:
        :param method:
        :param args:
        :param kwargs:
        :return:
        """
        if not self.resource.exists:
            raise Http404("Resource doesn't exists")
        if not self.has_access(self.resource, 'read'):
            return self.no_access()
        # need to double unquote the HTTP_DESTINATION header (Windows 7 Compatibility)
        dst = urlparse.unquote(
            urlparse.unquote(request.META.get(
                'HTTP_DESTINATION', '')))  # .decode(self.xml_encoding)
        if not dst:
            return HttpResponseBadRequest('Destination header missing.')

        original_dst = dst

        dparts = urlparse.urlparse(dst)
        sparts = urlparse.urlparse(request.build_absolute_uri())
        if sparts.scheme != dparts.scheme or sparts.hostname != dparts.hostname:
            return HttpResponseBadGateway(
                'Source and destination must have the same scheme and host.')
        # adjust path for our base url:
        dst = self.get_resource(path=dparts.path[len(self.base_url):])
        if not dst.get_parent().exists:
            return HttpResponseConflict()
        if not self.has_access(self.resource, 'write'):
            return self.no_access()
        overwrite = request.META.get('HTTP_OVERWRITE', 'T')
        if overwrite not in ('T', 'F'):
            return HttpResponseBadRequest('Overwrite header must be T or F.')
        overwrite = (overwrite == 'T')
        if not overwrite and dst.exists:
            return HttpResponsePreconditionFailed(
                'Destination exists and overwrite False.')
        dst_exists = dst.exists
        if dst_exists:
            self.lock_class(self.resource).del_locks()
            self.lock_class(dst).del_locks()
            dst.delete()
        errors = getattr(self.resource, method)(dst, *args, **kwargs)
        if errors:
            print(errors)
            return self.build_xml_response(
                response_class=HttpResponseMultiStatus)  # WAT?
        if dst_exists:
            return HttpResponseNoContent()

        # return a response with the new location
        response = HttpResponseCreated()
        response['Location'] = original_dst
        return response
Exemple #2
0
 def relocate(self, request, path, method, *args, **kwargs):
     if not self.resource.exists:
         raise Http404("Resource doesn't exists")
     if not self.has_access(self.resource, 'read'):
         return self.no_access()
     dst = urlparse.unquote(request.META.get('HTTP_DESTINATION',
                                             '')).decode(self.xml_encoding)
     if not dst:
         return HttpResponseBadRequest('Destination header missing.')
     dparts = urlparse.urlparse(dst)
     sparts = urlparse.urlparse(request.build_absolute_uri())
     if sparts.scheme != dparts.scheme or sparts.hostname != dparts.hostname:
         return HttpResponseBadGateway(
             'Source and destination must have the same scheme and host.')
     # adjust path for our base url:
     dst = self.get_resource(path=dparts.path[len(self.base_url):])
     if not dst.get_parent().exists:
         return HttpResponseConflict()
     if not self.has_access(self.resource, 'write'):
         return self.no_access()
     overwrite = request.META.get('HTTP_OVERWRITE', 'T')
     if overwrite not in ('T', 'F'):
         return HttpResponseBadRequest('Overwrite header must be T or F.')
     overwrite = (overwrite == 'T')
     if not overwrite and dst.exists:
         return HttpResponsePreconditionFailed(
             'Destination exists and overwrite False.')
     dst_exists = dst.exists
     if dst_exists:
         self.lock_class(self.resource).del_locks()
         self.lock_class(dst).del_locks()
         dst.delete()
     errors = getattr(self.resource, method)(dst, *args, **kwargs)
     if errors:
         return self.build_xml_response(
             response_class=HttpResponseMultiStatus)  # WAT?
     if dst_exists:
         return HttpResponseNoContent()
     return HttpResponseCreated()