Exemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        critical = request.GET.get('critical')
        important = request.GET.get('important')
        normal = request.GET.get('normal')
        deviceName = request.GET.get('deviceName')
        deviceType = request.GET.get('deviceType')
        parentMoid = request.GET.get('parentMoid')
        startTime = request.GET.get('starttime')
        startTime = startTime.replace("+ ", " ")
        stopTime = request.GET.get('stoptime')
        stopTime = stopTime.replace("+ ", " ")
        warningType = request.GET.get('warningType')

        if parentMoid == '':
            user = getattr(request, 'sso_user', None)
            if user is not None: 
                parentMoid = user['data']['accountDomainMoid']
                userMoid = user['data']['moid']
                userDomainMoid = user['data']['accountDomainMoid']
            else:
                response = {'success': 0, 'message':'未登陆用户'}
                return Response(response)

        logger.info('[ServerWarningRepairedList] critical:%s, important:%s, normal:%s' % (critical,important,normal))
        logger.info('[ServerWarningRepairedList] deviceName:%s, deviceType:%s, parentMoid:%s' % (deviceName,deviceType,parentMoid))
        logger.info('[ServerWarningRepairedList] startTime:%s, stopTime:%s' % (startTime,stopTime))

        result = warning.export_warning_info_list(critical, important, normal, deviceName, deviceType, parentMoid, startTime, stopTime, warningType, userDomainMoid, userMoid)
        if result == 1:
            f = open('/opt/data/nms_webserver/inspect/%s.xls' % parentMoid, 'rb')
            response = FileResponse(f)
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment;filename=inspect_result.xls'
            return response
        else:
            response = HttpResponse()
            response.status_code = 500
            return response
Exemplo n.º 2
0
    def get(self, request, path, head=False, *args, **kwargs):
        """
        GET a resource

        If head=True, only the headers are returned

        This method also handles X-Accel-Redirect headers

        :param request:
        :param path:
        :param head:
        :param args:
        :param kwargs:
        :return:
        """
        if not self.resource.exists:
            # Resource does not exist
            if head:
                return HttpResponse(content='', status=404)
            raise Http404("Resource doesn't exists")
        if not path.endswith("/") and self.resource.is_collection:
            # make sure collections always end with a slash
            return HttpResponseRedirect(request.build_absolute_uri() + "/")
        if path.endswith("/") and self.resource.is_object:
            # make sure files do not end with a slash
            return HttpResponseRedirect(
                request.build_absolute_uri().rstrip("/"))

        # make sure the user has access
        if not self.has_access(self.resource, 'read'):
            return self.no_access()

        # construct a response
        response = FileResponse()

        # set default content length to 0 - we can still overwrite it later
        response['Content-Length'] = 0

        # it's either an object or a collection
        if self.resource.is_object:
            # is an object
            response['Content-Type'] = self.resource.content_type
            response['ETag'] = self.resource.etag
            response['Content-Length'] = self.resource.getcontentlength
            response['Accept-Ranges'] = 'bytes'
            response['Cache-Control'] = 'must-revalidate'

            etags = request.META.get('HTTP_IF_NONE_MATCH', None)
            if etags \
                and (self.resource.etag in (e.strip(' ').strip('"') for e in etags.split(','))):
                response.status_code = 304
                return response
            if not head:
                # not a head request, so we can actually return a response
                if DJANGODAV_X_REDIRECT:
                    # Using X-Accel-Redirect
                    # create a new response that handles x-accel-redirect
                    response = HttpResponse()

                    # get the path to the requested file
                    current_path_to_file = self.resource.read().name

                    # make sure the path is relative
                    if current_path_to_file.startswith("/"):
                        # absolute path - convert it into a path relative to the resources root path
                        relpath = os.path.relpath(self.resource.read().name,
                                                  self.resource.root)
                    else:
                        # it's already a relative path, everything is fine
                        relpath = self.resource.read().name

                    # we are not allowed to send utf8 headers, so we need to make sure to quote it
                    response['X-Accel-Redirect'] = urlquote(
                        # join url with the DAV prefix
                        url_join(DJANGODAV_X_REDIRECT_PREFIX, relpath))
                    # set the display name as the content disposition header, acting as the download name of the file
                    response[
                        'Content-Disposition'] = rfc5987_content_disposition(
                            self.resource.displayname)
                    response['Content-Type'] = self.resource.content_type

                    # Unfortunately, setting content-length, last-modified and etag does not work with nginx, as those
                    # are overwritten by nginx, see https://forum.nginx.org/read.php?2,205636,205665#msg-205665
                    # Therefore we need to set them with a prefix, e.g., X-Accel-, and handle it with nginx
                    # add_header and $upstream_http_*
                    response[
                        'X-Accel-Content-Length'] = self.resource.getcontentlength
                    response[
                        'X-Accel-Last-Modified'] = self.resource.get_modified(
                        ).ctime()
                    response['X-Accel-ETag'] = self.resource.etag

                    return response
                else:
                    # try to read the resource and return it in response
                    response.streaming_content = self.resource.read()
        elif not head:
            # not a head request, and not an object -> render index.html
            response = super(DavView, self).get(request, *args, **kwargs)

        # set last modified field of response, so browsers and other tools can properly handle caching
        response['Last-Modified'] = self.resource.getlastmodified

        return response