当前位置: 首页>>代码示例>>Python>>正文


Python uploadedfile.TemporaryUploadedFile类代码示例

本文整理汇总了Python中django.core.files.uploadedfile.TemporaryUploadedFile的典型用法代码示例。如果您正苦于以下问题:Python TemporaryUploadedFile类的具体用法?Python TemporaryUploadedFile怎么用?Python TemporaryUploadedFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了TemporaryUploadedFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: FileWriteTest

class FileWriteTest(TestCase):

    def setUp(self):
        self.text = "Spam Spam Spam Spam.\n"
        self.digest = hashlib.sha1(self.text).hexdigest()
        self.tempfile = TemporaryUploadedFile('spam4.txt', 'text/plain',
                                              len(self.text), 'utf-8')
        self.tempfile.file.write(self.text)
        self.tempfile.file.seek(0)

    def test(self):
        new_upload = Upload(file=self.tempfile)
        new_upload.save()

        # Upload has been saved to the database.
        self.assert_(new_upload.pk)

        # Upload contains correct content.
        self.assertEqual(new_upload.file.read(), self.text)

        # Filename is the hash of the file contents.
        self.assert_(new_upload.file.name.startswith(self.digest))

    def tearDown(self):
        self.tempfile.close()  # Also deletes the temp file.

        # Remove the upload in `MEDIA_ROOT`.
        directory = os.path.join(settings.MEDIA_ROOT, '24')
        if os.path.exists(directory):
            shutil.rmtree(directory)
开发者ID:Mondego,项目名称:pyreco,代码行数:30,代码来源:allPythonContent.py

示例2: TemporaryFileUploadHandler

class TemporaryFileUploadHandler(FileUploadHandler):
    """
    Upload handler that streams data into a temporary file.
    """

    def __init__(self, *args, **kwargs):
        super(TemporaryFileUploadHandler, self).__init__(*args, **kwargs)

    def new_file(self, file_name, *args, **kwargs):
        """
        Create the file object to append to as data is coming in.
        """
        super(TemporaryFileUploadHandler, self).new_file(
            file_name, *args, **kwargs)
        self.file = TemporaryUploadedFile(self.file_name, self.content_type, 0,
                                          self.charset,
                                          self.content_type_extra)

    def receive_data_chunk(self, raw_data, start):
        self.file.write(raw_data)

    def file_complete(self, file_size):
        self.file.seek(0)
        self.file.size = file_size
        return self.file
开发者ID:Nivial,项目名称:my-first-blog,代码行数:25,代码来源:uploadhandler.py

示例3: handle_filedrop_upload

def handle_filedrop_upload(request):
    """
    Squeeze out an UploadedFile from a request sent through FileDrop.js.

    FileDrop.js's AJAX mode passes the actual file data
    as an unembellished binary stream as the POST payload
    so we need to do some magic that normal (multipart/form-data)
    uploads would not require.

    Here's that magic.

    :param request: HTTP request.
    :type request: django.http.HttpRequest
    :return: Uploaded file.
    :rtype: django.core.files.uploadedfile.UploadedFile
    """

    content_type = request.META.get("HTTP_X_FILE_TYPE", "")
    filename = request.META["HTTP_X_FILE_NAME"]
    size = int(request.META["HTTP_X_FILE_SIZE"])

    if size >= settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
        upload_file = TemporaryUploadedFile(name=filename, content_type=content_type, size=size, charset="binary")
    else:
        upload_file = InMemoryUploadedFile(
            name=filename, content_type=content_type, size=size, charset="binary", field_name="none", file=BytesIO()
        )

    upload_file.write(request.read())
    return upload_file
开发者ID:noyonthe1,项目名称:shoop,代码行数:30,代码来源:views.py

示例4: decorate

 def decorate(request, *args, **kwargs):
     if request.method == 'POST' and 'HTTP_X_FILE_NAME' in request.META:
         tf = TemporaryUploadedFile('rawdata', request.META['HTTP_X_FILE_TYPE'], int(request.META['CONTENT_LENGTH']), None)
         chunk = ' '
         while len(chunk) > 0:
             chunk = request.read(1024)
             tf.write(chunk)
         tf.seek(0)
         request.FILES['file'] = tf
     return func(request, *args, **kwargs)
开发者ID:tpetr,项目名称:muxlist,代码行数:10,代码来源:decorators.py

示例5: _make_tempfile

 def _make_tempfile(self, filename, content):
     fileobj = TemporaryUploadedFile(
         name=filename + ".tempfile",
         content_type='text/plain',
         size=0,
         charset='utf8',
     )
     fileobj.write(content)
     fileobj.flush()
     return fileobj
开发者ID:subuk,项目名称:django-webdav-storage,代码行数:10,代码来源:tests.py

示例6: FeedUploadHandler

class FeedUploadHandler(TemporaryFileUploadHandler):
    """
    This handler specifically handles feed uploads
    """

    QUOTA = 42 * 2 ** 20  # 42 MB
    # doesn't seem to be a good way to identify zip files
    MIME_TYPES = ("application/zip", "application/x-zip", "application/x-gzip")

    def __init__(self, *args, **kwargs):
        super(FeedUploadHandler, self).__init__(*args, **kwargs)
        self.total_upload = 0
        self.file_name = ""

    def _validate_file(self):
        filename_re = re.compile(r'filename="(?P<name>[^"]+)"')
        content_type = str(self.request.META.get("CONTENT_TYPE", ""))
        content_length = int(self.request.META.get("CONTENT_LENGTH", 0))
        charset = "binary"

        m = filename_re.search(self.request.META.get("HTTP_CONTENT_DISPOSITION", ""))

        if content_type not in self.MIME_TYPES:
            raise IncorrectMimeTypeError("Incorrect mime type", connection_reset=True)
        if content_length > self.QUOTA:
            raise StopUpload(connection_reset=True)
        if not m:
            raise FileNameUnspecified("File name not specified", connection_reset=True)

        self.file_name = self.file_name = m.group("name")
        self.content_type = content_type
        self.content_length = content_length

    #        print content_length

    def new_file(self, file_name, *args, **kwargs):
        """
        Create the file object to append to as data is coming in.
        Ignores and overwrites most of the arguments and relies on exsiting request
        """
        super(FeedUploadHandler, self).new_file(file_name, *args, **kwargs)
        self._validate_file()
        self.file = TemporaryUploadedFile(self.file_name, self.content_type, 0, self.charset)

    def receive_data_chunk(self, raw_data, start):
        self.total_upload += len(raw_data)

        #        print "Total upload: {0}".format(self.total_upload)

        if self.total_upload >= self.QUOTA:
            raise StopUpload(connection_reset=True)

        self.file.write(raw_data)
开发者ID:votinginfoproject,项目名称:VAVE,代码行数:53,代码来源:handlers.py

示例7: parse_distutils_request

def parse_distutils_request(request):
    """
    Due to a bug in the Python distutils library, the request post is sent using \n
    as a separator instead of the \r\n that the HTTP spec demands. This breaks the Django
    form parser and therefore we have to write a custom parser.

    This bug was fixed in the Python 2.7.4 and 3.4:

    http://bugs.python.org/issue10510
    """
    if not request.body.endswith('\r\n'):
        sep = request.body.splitlines()[1]

        request.POST = QueryDict('', mutable=True)
        try:
            request._files = MultiValueDict()
        except Exception:
            pass

        for part in filter(lambda e: e.strip(), request.body.split(sep)):
            try:
                header, content = part.lstrip().split('\n', 1)
            except Exception:
                continue

            if content.startswith('\n'):
                content = content[1:]

            if content.endswith('\n'):
                content = content[:-1]

            headers = parse_header(header)

            if "name" not in headers:
                continue

            if "filename" in headers and headers['name'] == 'content':
                dist = TemporaryUploadedFile(name=headers["filename"],
                                             size=len(content),
                                             content_type="application/gzip",
                                             charset='utf-8')
                dist.write(content)
                dist.seek(0)
                request.FILES.appendlist('distribution', dist)
            else:
                request.POST.appendlist(headers["name"], content)
    else:
        request.FILES['distribution'] = request.FILES['content']

    # Distutils sends UNKNOWN for empty fields (e.g platform)
    for key, value in request.POST.items():
        if value == 'UNKNOWN':
            request.POST[key] = None
开发者ID:TargetHolding,项目名称:localshop,代码行数:53,代码来源:utils.py

示例8: test_save_tempfile

 def test_save_tempfile(self):
     with media_root():
         storage = SafeFileSystemStorage()
         content = 'Hello world!'
         f = TemporaryUploadedFile(name='filename',
                                   content_type='text/plain',
                                   size=len(content),
                                   charset='utf-8')
         f.write(content)
         f.seek(0)
         name = storage.save('hello.txt', f)
         self.assertEqual(name, 'hello.txt')
         self.assertEqual(open(storage.path(name)).read(), content)
开发者ID:Caramel,项目名称:django-randomfilenamestorage,代码行数:13,代码来源:tests.py

示例9: create_photo_versions

def create_photo_versions(sender, instance, **kwargs):
    """Create `PhotoVersion`` objects for the photo object defined by `instance`.

    A version is created for a bounding box defined by each PhotoSize instance.

    """    
    from photo.models import Photo, PhotoSize, PhotoVersion
    photo = instance
    ext = '.jpg'
    t = None
    try:
        pth = photo.image.path
    except NotImplementedError:
        from django.core.files.temp import NamedTemporaryFile
        t = NamedTemporaryFile(suffix=ext)
        ix = photo.image
        if ix.closed:
            # Reload from DB
            photo = Photo.objects.get(pk=photo.pk)
            ix = photo.image
        for d in ix.chunks(4000000):
            t.write(d)
        t.flush()
        t.seek(0)
        pth = t
    for size in PhotoSize.objects.all():
        # Create a suitable filename.
        filename = '%s-%s-%s%s' % (photo.pk, uuid4().hex[::7], slugify(size.name)[:10], ext)
        ctype = guess_type(filename)[0]
        temp_file = TemporaryUploadedFile(name=filename, content_type=ctype, size=0, charset=None)
        if t:
            t.seek(0)
        try:
            version = PhotoVersion.objects.get(photo=photo, size=size)
            remove_model_image(version, 'image')
            version.image = None
        except PhotoVersion.DoesNotExist:
            version = PhotoVersion(photo=photo, size=size)
        if size.do_crop:
            resize_to, crop_box, input_image = get_perfect_fit_resize_crop(size.bounding_box, (photo.width, photo.height))
        else:
            resize_to = size.bounding_box
            crop_box = None
        # Resize to a temporary location.
        resize(pth, resize_to, out_file_path=temp_file, crop=crop_box)
        # Save resized copy to `version` instance.
        temp_file.seek(0) # Prepare file for a re-read.
        version.image.save(name=filename, content=temp_file, save=True)
        temp_file.close()
    if t:
        t.close()
开发者ID:kabirh,项目名称:riotvine,代码行数:51,代码来源:utils.py

示例10: parse_distutils_request

def parse_distutils_request(request):
    """Parse the `request.raw_post_data` and return a `MultiValueDict`
    for the POST data and the FILES data.

    This method is taken from the chishop source.

    """

    try:
        sep = request.raw_post_data.splitlines()[1]
    except:
        raise ValueError('Invalid post data')

    request.POST = QueryDict('', mutable=True)
    try:
        request._files = MultiValueDict()
    except Exception:
        pass
    for part in filter(lambda e: e.strip(), request.raw_post_data.split(sep)):
        try:
            header, content = part.lstrip().split('\n', 1)
        except Exception:
            continue

        if content.startswith('\n'):
            content = content[1:]

        if content.endswith('\n'):
            content = content[:-1]

        headers = parse_header(header)

        if "name" not in headers:
            continue

        if "filename" in headers and headers['name'] == 'content':
            dist = TemporaryUploadedFile(name=headers["filename"],
                                         size=len(content),
                                         content_type="application/gzip",
                                         charset='utf-8')
            dist.write(content)
            dist.seek(0)
            request.FILES.appendlist('distribution', dist)
        else:
            # Distutils sends UNKNOWN for empty fields (e.g platform)
            # [[email protected]]
            if content == 'UNKNOWN':
                content = None
            request.POST.appendlist(headers["name"], content)
开发者ID:ba-amm,项目名称:localshop,代码行数:49,代码来源:utils.py

示例11: parse_distutils_request

def parse_distutils_request(request):
    """ This is being used because the built in request parser that Django uses,
    django.http.multipartparser.MultiPartParser is interperting the POST data
    incorrectly and/or the post data coming from distutils is invalid.

    One portion of this is the end marker: \r\n\r\n (what Django expects)
    versus \n\n (what distutils is sending).
    """

    sep = request.body.splitlines()[1]
    request.POST = QueryDict('', mutable=True)

    try:
        request._files = MultiValueDict()
    except Exception:
        pass

    for part in filter(lambda e: e.strip(), request.body.split(sep)):

        try:
            header, content = part.lstrip().split('\n', 1)
        except Exception:
            continue

        try:
            if content.startswith('\n'):
                content = content[1:]

            if content.endswith('\n'):
                content = content[:-1]

            headers = parse_header(header)

            if "name" not in headers:
                continue

            if "filename" in headers:
                dist = TemporaryUploadedFile(name=headers["filename"],
                                             size=len(content),
                                             content_type="application/gzip",
                                             charset='utf-8')
                dist.write(content)
                dist.seek(0)
                request.FILES.appendlist(headers['name'], dist)
            else:
                request.POST.appendlist(headers["name"], content)
        except Exception as e:
            print e
    return
开发者ID:callowayproject,项目名称:callowaysite,代码行数:49,代码来源:http.py

示例12: parse_distutils_request

def parse_distutils_request(request):
    """Parse the `request.raw_post_data` and update the request POST and FILES
    attributes .

    """
    lines = request.raw_post_data.splitlines()
    seperator = next(line for line in lines if line.startswith('----'))
    request.POST = QueryDict('', mutable=True)

    raw_post = request.raw_post_data.split(seperator)

    raw_lines = [line.lstrip() for line in raw_post if line.lstrip()]
    try:
        request._files = MultiValueDict()
    except Exception:
        pass

    for line in raw_lines:

        line_content = line.lstrip().split('\n', 1)
        header = line_content[0]
        content = line_content[1]

        if content.startswith('\n'):
            content = content[1:]

        if content.endswith('\n'):
            content = content[:-1]

        headers = parse_header(header)

        if "name" not in headers:
            continue

        if "filename" in headers and headers['name'] == 'content':
            dist = TemporaryUploadedFile(name=headers["filename"],
                                         size=len(content),
                                         content_type="application/gzip",
                                         charset='utf-8')
            dist.write(content)
            dist.seek(0)
            request.FILES.appendlist('distribution', dist)
        else:
            # Distutils sends UNKNOWN for empty fields (e.g platform)
            # [[email protected]]
            if content == 'UNKNOWN':
                content = None
            request.POST.appendlist(headers["name"], content)
开发者ID:hzdg,项目名称:localshop,代码行数:48,代码来源:utils.py

示例13: value_from_datadict

    def value_from_datadict(self, data, files, name):
        """ Normally returns files.get(name, None). Here we also check `data`.
        -- if the appropriate hidden _sticky_file input is set, we can look for
        the temporary file instead and return that if it exists.

        This method seems to be called multiple times with the same arguments,
        so to prevent excessive storage activity the return value is cached
        and returned without processing on subsequent calls.

        There is an assumption that the arguments will not change between calls
        for any given instance, which appears to be valid, so no argument
        checks are performed.
        """
        if hasattr(self, '_value'):
            return self._value

        self.user_token = data.get('csrfmiddlewaretoken', None)

        # look for normal file
        value = super(
            StickyFileInput, self).value_from_datadict(data, files, name)

        if value and hasattr(value, 'name'):
            # got one, save a temporary copy just in case
            self.sticky_file_name = value.name
            self.sticky_session_id = '%.6f' % time.time()
            self.save_sticky_copy(value.file)
        else: # check for temporary copy
            self.sticky_file_name = (
                data.get(
                    self.get_hidden_input_name(name, 'sticky_file'), None))
            self.sticky_session_id = data.get(
                self.get_hidden_input_name(name, 'sticky_session_id'), None)
            sticky_copy = self.load_sticky_copy()
            if sticky_copy:
                sticky_copy.seek(0, 2) # seek to end
                value = TemporaryUploadedFile(
                    name = self.sticky_file_name,
                    content_type = None,
                    size = sticky_copy.tell(),
                    charset = None
                    )
                value.file = sticky_copy
                value.file.seek(0)
                value.temporary_file_path = lambda: self.get_sticky_path()

        setattr(self, '_value', value) # cache
        return self._value
开发者ID:kamalhg,项目名称:django-sticky-uploads,代码行数:48,代码来源:fields.py

示例14: process_request

    def process_request(self, request):
        file_name = request.META.get('HTTP_X_FILE_NAME')
        self.uploaded_file = None
        if ('application/octet-stream' in request.META.get('CONTENT_TYPE')
            and request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
            and request.method == 'POST'
            and file_name):

            initial_size = request._stream.remaining
            self.uploaded_file = TemporaryUploadedFile(
                name=unquote(file_name),
                content_type='application/octet-stream',
                size=initial_size,
                charset=None)

            size = 0
            while True:
                chunk = request._stream.read(self.chunk_size)
                if not chunk:
                    break
                size += len(chunk)
                self.uploaded_file.write(chunk)

            if size != initial_size:
                raise HttpResponseBadRequest

            self.uploaded_file.seek(0)
            self.uploaded_file.size = size

            request.FILES['file'] = self.uploaded_file
            request.POST = request.GET
开发者ID:xigurat,项目名称:sunshine,代码行数:31,代码来源:middleware.py

示例15: setUp

 def setUp(self):
     self.text = "Spam Spam Spam Spam.\n"
     self.digest = hashlib.sha1(self.text).hexdigest()
     self.tempfile = TemporaryUploadedFile('spam4.txt', 'text/plain',
                                           len(self.text), 'utf-8')
     self.tempfile.file.write(self.text)
     self.tempfile.file.seek(0)
开发者ID:Mondego,项目名称:pyreco,代码行数:7,代码来源:allPythonContent.py


注:本文中的django.core.files.uploadedfile.TemporaryUploadedFile类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。