def pre_save(self, model_instance, add): the_file = super(models.FileField, self).pre_save(model_instance, add) real_file = the_file # If the file provided is a Temporary One if the_file and hasattr(the_file, 'instance') and isinstance(the_file.instance, TemporaryFileWrapper): path, filename = os.path.split(the_file.name) new_file = FieldFile(model_instance, self.get_field_pointer(model_instance), filename) image_file = ContentFile(the_file.file.read(), the_file.name) new_file.save(filename, image_file, save=False) real_file = new_file elif the_file and not the_file._committed and not getattr(self.widget, 'is_tgm_widget', False): # Commit the file to storage prior to saving the model # This makes this model work correctly with other widgets # (e.g. a plain image upload in admin) the_file.save(the_file.name, the_file, save=False) if self.post_link(model_instance, the_file.instance if the_file else the_file, real_file): if the_file and isinstance(the_file.instance, TemporaryFileWrapper): the_file.instance.linked = True the_file.instance.save() return real_file
def pre_save(self, model_instance, add): "Returns field's value just before saving." file = FieldFile(model_instance, self, super(FileField, self).pre_save(model_instance, add).meta_content) tag = super(FileField, self).pre_save(model_instance, add) # Let's hack this if hasattr(tag, '_inmemoryuploadedfile') and getattr(tag, '_inmemoryuploadedfile'): file.save(file.name, tag._inmemoryuploadedfile, save=False) return file
def test_generic(self): field = FileField(name='test', upload_to=self.upload_to) fake_model = type(str('fake'), (object,), {field.name: None}) _file = FieldFile(field=field, instance=fake_model(), name='') _file._committed = False with open(self.archive, 'rb') as f: _file.save('test.zip', ContentFile(f.read()), save=False) self.assertEqual(_file.name, 'archive/da39a3ee5e6b4b0d3255bfef95601890afd80709.zip') _file.delete(save=False)
def pre_save(self, model_instance, add): "Returns field's value just before saving." file = FieldFile( model_instance, self, super(FileField, self).pre_save(model_instance, add).meta_content) tag = super(FileField, self).pre_save(model_instance, add) # Let's hack this if hasattr(tag, '_inmemoryuploadedfile') and getattr( tag, '_inmemoryuploadedfile'): file.save(file.name, tag._inmemoryuploadedfile, save=False) return file
def test_generic(self): field = FileField(name='test', upload_to=self.upload_to) fake_model = type(str('fake'), (object, ), {field.name: None}) _file = FieldFile(field=field, instance=fake_model(), name='') _file._committed = False with open(self.archive, 'rb') as f: _file.save('test.zip', ContentFile(f.read()), save=False) self.assertEqual( _file.name, 'archive/da39a3ee5e6b4b0d3255bfef95601890afd80709.zip') _file.delete(save=False)
def output_path_helper(filename: str, output: FieldFile): workdir = getattr(settings, 'GEODATA_WORKDIR', None) with tempfile.TemporaryDirectory(dir=workdir) as tmpdir: output_path = os.path.join(tmpdir, filename) try: # Yield the path for the user to perform a task yield output_path except Exception as e: raise e else: # Save the file contents to the output field only on success with open(output_path, 'rb') as f: output.save(os.path.basename(output_path), f)
def save(self, name, content, save=True): password = TemporaryKeyHandler.getFileKey(name) return FieldFile.save(self, name, EncryptedFile(content, bytes(password, "utf8")), save=save)
def input_output_path_helper( source, output: FieldFile, prefix: str = '', suffix: str = '', vsi: bool = False ): """Yield source and output paths between a ChecksumFile and a FileFeild. The output path is saved to the output field after yielding. """ filename = prefix + os.path.basename(source.name) + suffix with source.yield_local_path(vsi=vsi) as file_path: filename = prefix + os.path.basename(source.name) + suffix with output_path_helper(filename, output) as output_path: try: # Yield the paths for the user to perform a task yield (file_path, output_path) except Exception as e: raise e else: # Save the file contents to the output field only on success with open(output_path, 'rb') as f: output.save(os.path.basename(output_path), f)
def save(self, name, content, save=True): if self.key is None: key_id = getattr(self.instance, "pk") from django.db import connection with connection.cursor() as cursor: cursor.execute("select key from key_store where id = %s::text", (key_id,)) row = cursor.fetchone() if row is None: self.key = Encryption.generate_from_str(str(key_id)) else: self.key = row[0] return FieldFile.save( self, name, EncryptedFile(content, password=self.key.encode('utf-8')), save=save )
def save(self, name, content, save=True): return FieldFile.save(self, name, EncryptedFile(content), save=save)