Ejemplo n.º 1
0
 def test_is_able_to_regenerate_the_request_files_dict(self):
     # Setup
     original_f1 = SimpleUploadedFile('file1.txt', force_bytes('file_content_1'))
     original_f2 = SimpleUploadedFile('file2.txt', force_bytes('file_content_2_long' * 300000))
     original_f2.charset = 'iso-8859-1'
     original_files = {'f1': original_f1, 'f2': original_f2}
     cache.set('mykey', original_files)
     # Run
     files = cache.get('mykey')
     assert 'f1' in files
     assert 'f2' in files
     f1 = files['f1']
     f2 = files['f2']
     assert isinstance(f1, InMemoryUploadedFile)
     assert f1.name == 'file1.txt'
     assert f1.file.read() == force_bytes('file_content_1')
     assert isinstance(f2, TemporaryUploadedFile)  # because of the size of the content of f2
     assert f2.name == 'file2.txt'
     assert f2.file.read() == force_bytes('file_content_2_long' * 300000)
     assert f2.charset == 'iso-8859-1'
Ejemplo n.º 2
0
 def test_is_able_to_regenerate_the_request_files_dict(self):
     # Setup
     original_f1 = SimpleUploadedFile('file1.txt', force_bytes('file_content_1'))
     original_f2 = SimpleUploadedFile('file2.txt', force_bytes('file_content_2_long' * 300000))
     original_f2.charset = 'iso-8859-1'
     original_files = {'f1': original_f1, 'f2': original_f2}
     cache.set('mykey', original_files)
     # Run
     files = cache.get('mykey')
     assert 'f1' in files
     assert 'f2' in files
     f1 = files['f1']
     f2 = files['f2']
     assert isinstance(f1, InMemoryUploadedFile)
     assert f1.name == 'file1.txt'
     assert f1.file.read() == force_bytes('file_content_1')
     assert isinstance(f2, TemporaryUploadedFile)  # because of the size of the content of f2
     assert f2.name == 'file2.txt'
     assert f2.file.read() == force_bytes('file_content_2_long' * 300000)
     assert f2.charset == 'iso-8859-1'
Ejemplo n.º 3
0
 def test_is_able_to_store_the_state_of_request_files(self):
     # Setup
     f1 = SimpleUploadedFile('file1.txt', force_bytes('file_content_1'))
     f2 = SimpleUploadedFile('file2.txt', force_bytes('file_content_2_long'))
     f2.charset = 'iso-8859-1'
     files = {'f1': f1, 'f2': f2}
     real_cache = cache.get_backend()
     # Run
     cache.set('mykey', files)
     states = real_cache.get('mykey')
     # Check
     assert states['f1']['name'] == 'file1.txt'
     assert states['f1']['content'] == force_bytes('file_content_1')
     assert states['f1']['charset'] is None
     assert states['f1']['content_type'] == 'text/plain'
     assert states['f1']['size'] == 14
     assert states['f2']['name'] == 'file2.txt'
     assert states['f2']['content'] == force_bytes('file_content_2_long')
     assert states['f2']['charset'] == 'iso-8859-1'
     assert states['f2']['content_type'] == 'text/plain'
     assert states['f2']['size'] == 19
Ejemplo n.º 4
0
 def test_is_able_to_store_the_state_of_request_files(self):
     # Setup
     f1 = SimpleUploadedFile('file1.txt', force_bytes('file_content_1'))
     f2 = SimpleUploadedFile('file2.txt', force_bytes('file_content_2_long'))
     f2.charset = 'iso-8859-1'
     files = {'f1': f1, 'f2': f2}
     real_cache = cache.get_backend()
     # Run
     cache.set('mykey', files)
     states = real_cache.get('mykey')
     # Check
     assert states['f1']['name'] == 'file1.txt'
     assert states['f1']['content'] == force_bytes('file_content_1')
     assert states['f1']['charset'] is None
     assert states['f1']['content_type'] == 'text/plain'
     assert states['f1']['size'] == 14
     assert states['f2']['name'] == 'file2.txt'
     assert states['f2']['content'] == force_bytes('file_content_2_long')
     assert states['f2']['charset'] == 'iso-8859-1'
     assert states['f2']['content_type'] == 'text/plain'
     assert states['f2']['size'] == 19
Ejemplo n.º 5
0
    def file_complete(self, file_size):
        """
        Signal that a file has completed.
        NOTE: When activated, file size DOES NOT corresponds to the actual size.
        NOTE: When activated, file size IS NOT accumulated by all the chunks.
        In this case, it represents the total content_length of the request.
        """
        if not self.activated:
            return

        # Create a concrete `UploadedFile` with empty content and `file_size` size
        uploaded_file = SimpleUploadedFile(
            name=self.file_name,
            content=b"",  # Empty Content
            content_type=self.content_type,
        )
        uploaded_file.field_name = self.field_name
        uploaded_file.size = file_size  # Set Size
        uploaded_file.charset = self.charset
        uploaded_file.content_type_extra = self.content_type_extra

        return uploaded_file