示例#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
示例#2
0
 def mkcol(self, request, path, *args, **kwargs):
     if self.resource.exists:
         return HttpResponseNotAllowed(self._allowed_methods())
     if not self.resource.get_parent().exists:
         return HttpResponseConflict()
     length = request.META.get('CONTENT_LENGTH', 0)
     if length and int(length) != 0:
         return HttpResponseMediatypeNotSupported()
     if not self.has_access(self.resource, 'write'):
         return self.no_access()
     self.resource.create_collection()
     self.__dict__['resource'] = self.get_resource(
         path=self.resource.get_path())
     return HttpResponseCreated()
示例#3
0
 def put(self, request, path, *args, **kwargs):
     parent = self.resource.get_parent()
     if not parent.exists:
         raise Http404("Resource doesn't exists")
     if self.resource.is_collection:
         return self.no_access()
     if not self.resource.exists and not self.has_access(parent, 'write'):
         return self.no_access()
     if self.resource.exists and not self.has_access(
             self.resource, 'write'):
         return self.no_access()
     created = not self.resource.exists
     self.resource.write(request)
     if created:
         self.__dict__['resource'] = self.get_resource(
             path=self.resource.get_path())
         return HttpResponseCreated()
     else:
         return HttpResponseNoContent()
示例#4
0
文件: views.py 项目: TZanke/djangodav
 def put(self, request, path, *args, **kwargs):
     parent = self.resource.get_parent()
     if not parent.exists:
         return HttpResponseConflict("Resource doesn't exists")
     if self.resource.is_collection:
         return HttpResponseNotAllowed(
             list(set(self._allowed_methods()) - set(['MKCOL', 'PUT'])))
     if not self.resource.exists and not self.has_access(parent, 'write'):
         return self.no_access()
     if self.resource.exists and not self.has_access(
             self.resource, 'write'):
         return self.no_access()
     created = not self.resource.exists
     self.resource.write(request)
     if created:
         self.__dict__['resource'] = self.get_resource(
             path=self.resource.get_path())
         return HttpResponseCreated()
     else:
         return HttpResponseNoContent()
示例#5
0
文件: views.py 项目: TZanke/djangodav
 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()
示例#6
0
    def put(self, request, path, *args, **kwargs):
        """
        Upload a new file
        :param request:
        :param path:
        :param args:
        :param kwargs:
        :return:
        """
        parent = self.resource.get_parent()
        if not parent.exists:
            return HttpResponseConflict("Resource doesn't exists")
        if self.resource.is_collection:
            return HttpResponseNotAllowed(
                list(set(self._allowed_methods()) - set(['MKCOL', 'PUT'])))
        if not self.resource.exists and not self.has_access(parent, 'write'):
            return self.no_access()
        if self.resource.exists and not self.has_access(
                self.resource, 'write'):
            return self.no_access()
        created = not self.resource.exists

        # check headers for X-File-Name
        range = request.META.get('HTTP_CONTENT_RANGE', None)
        if range == None:
            range_start = None
        else:
            m = PATTERN_CONTENT_RANGE.match(range)
            if not m: return HttpResponseBadRequest("Invalid Content-Range")
            range_start = int(m[1])

        self.resource.write(request, range_start=range_start)

        if created:
            self.__dict__['resource'] = self.get_resource(
                path=self.resource.get_path())
            return HttpResponseCreated()
        else:
            return HttpResponseNoContent()
示例#7
0
 def mkcol(self, request, path, *args, **kwargs):
     """
     Create a new collection (a directory)
     :param request:
     :param path:
     :param args:
     :param kwargs:
     :return:
     """
     if self.resource.exists:
         return HttpResponseNotAllowed(
             list(set(self._allowed_methods()) - set(['MKCOL', 'PUT'])))
     if not self.resource.get_parent().exists:
         return HttpResponseConflict()
     length = request.META.get('CONTENT_LENGTH', 0)
     if length and int(length) != 0:
         return HttpResponseMediatypeNotSupported()
     if not self.has_access(self.resource, 'write'):
         return self.no_access()
     self.resource.create_collection()
     self.__dict__['resource'] = self.get_resource(
         path=self.resource.get_path())
     return HttpResponseCreated()