def test_filecolumn_supports_fieldfile(column, storage): field = models.FileField(storage=storage) name = 'child/foo.html' fieldfile = FieldFile(instance=None, field=field, name=name) root = parse(column.render(value=fieldfile)) assert root.tag == 'a' assert root.attrib == { 'class': 'a exists', 'title': name, 'href': '/baseurl/child/foo.html' } assert root.text == 'foo.html' # Now try a file that doesn't exist name = 'child/does_not_exist.html' fieldfile = FieldFile(instance=None, field=field, name=name) html = column.render(value=fieldfile) root = parse(html) assert root.tag == 'a' assert root.attrib == { 'class': 'a missing', 'title': name, 'href': '/baseurl/child/does_not_exist.html' } assert root.text == 'does_not_exist.html'
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 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 filecolumn_supports_fieldfile(column, storage): field = models.FileField(storage=storage) name = "child/foo.html" fieldfile = FieldFile(instance=None, field=field, name=name) root = parse(column.render(value=fieldfile)) assert root.tag == "a" assert root.attrib == { "class": "a exists", "title": name, "href": "/baseurl/child/foo.html" } assert root.text == "foo.html" # Now try a file that doesn't exist name = "child/does_not_exist.html" fieldfile = FieldFile(instance=None, field=field, name=name) html = column.render(value=fieldfile) root = parse(html) assert root.tag == "a" assert root.attrib == { "class": "a missing", "title": name, "href": "/baseurl/child/does_not_exist.html" } assert root.text == "does_not_exist.html"
def __init__(self, instance, field, data): self.data = data self.image_data = self.data.get('original', dict()) if isinstance(self.image_data, basestring): #old style self.image_data = {'path':self.image_data} self.data['original'] = self.image_data name = self.image_data.get('path', None) FieldFile.__init__(self, instance, field, name)
def test_delete_file(self): self.mock.StubOutWithMock(FieldFile, 'delete') FieldFile.delete() item = PodcastItem( file=SimpleUploadedFile('/ep123.txt', 'file contents')) self.mock.ReplayAll() item.delete_file() self.mock.VerifyAll()
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 value_from_datadict(self, data, files, name): "File widgets take data from FILES, not POST" if data.get(name, None): file_path = data[name] file_field = self.get_file_field() file_obj = file_field.storage.open(file_path) file_copy = FieldFile(None, file_field, file_path)#file_field.attr_class(None, file_field, file_path) file_copy.file = file_obj file_copy._committed = True return file_copy return None
def my_decoder(obj): if '__type__' in obj: if obj['__type__'] == '__datetime__': return datetime.fromtimestamp(obj['epoch']) if obj['__type__'] == '__date__': return date.fromtimestamp(obj['epoch']) if obj['__type__'] == '__uuid__': return uuid.UUID('{%s}'%obj['tval'], version=obj['tversion']) if obj['__type__'] == '__fieldfile__': ff = FieldFile(None, type('field', (object,), {'storage':{}}), None) ff.name = obj['tval'] return ff return obj
def get_some_file(path): subdirs, files = field.storage.listdir(path) if files: result_file = random.choice(files) instance = field.storage.open("%s/%s" % (path, result_file)).file out = FieldFile(instance, field, result_file) out.width = 100 out.height = 100 return out for subdir in subdirs: result = get_some_file("%s/%s" % (path, subdir)) if result: return result
def __getattribute__(self, name): fallback = object.__getattribute__(self, 'fallback') attr = object.__getattribute__(self, name) opts = object.__getattribute__(self, '_meta') default_lang = object.__getattribute__(self, 'default_language_code') active_lang = get_language() if name not in opts.translatable_fields or default_lang == active_lang: return attr translated = object.__getattribute__(self, '_translated') if translated: if hasattr(translated, name): _field = self._meta.get_field(name) if isinstance(_field, ImageField): return ImageFieldFile( self, _field, getattr(translated, name, attr if fallback else '')) elif isinstance(_field, FileField): return FieldFile( self, _field, getattr(translated, name, attr if fallback else '')) else: return getattr(translated, name, attr if fallback else '') elif hasattr(translated, '__iter__') and (active_lang in translated or name in translated): try: return translated.get(active_lang).get(name, '') except Exception as e: print('exception happened in model getattribute', e) return attr
def _get_value(self): primitive_models_dict = MetaModel.get_primitive_models_dict() mtype = primitive_models_dict.get(self.model_id, None) if mtype: mtype = mtype.name if mtype == 'BooleanField': return bool(self.decimal_value) if mtype == 'CharField': return self.unicode_value if mtype == 'DateField': return date.fromordinal(self.decimal_value) if mtype == 'DateTimeField': epoch = datetime.utcfromtimestamp(0) delta = timedelta(seconds=int(self.decimal_value)) return epoch + delta if mtype == 'DecimalField': return self.decimal_value if mtype == 'FileField': return FieldFile(default_storage.open(self.unicode_value), FileField(), self.unicode_value) if mtype == 'IntegerField': return int(self.decimal_value) return self.__getattr__('value')
def setup_work_dir(file: FieldFile) -> Tuple[TemporaryDirectory, Path]: """Create a temporary directory as working directory and make the source file locally available.""" temp_dir = TemporaryDirectory() try: source_file_path = Path(file.path) except (AttributeError, NotImplementedError): source_file_path = Path(temp_dir.name) / Path(file.name).name with source_file_path.open('wb') as fp: if file.multiple_chunks(): for chunk in file.chunks(): fp.write(chunk) else: fp.write(file.read()) return temp_dir, source_file_path
def calculate_file_hash(file: FieldFile) -> str: # Together the MemoryFileUploadHandler and TemporaryFileUploadHandler # provide Django’s default file upload behavior of reading small files # into memory and large ones onto disk. # If a file is large file will be an instance of TemporaryUploadedFile, # TemporaryUploadedFile.temporary_file_path() returns the full path to the temporary uploaded file. # If a file is small file will be an instance of InMemoryUploadedFile. file_hash = sha256() # Returns True if the uploaded file is big enough to require reading in multiple chunks. # By default this will be any file larger than 2.5 megabytes, but that’s configurable; if not file.multiple_chunks(): file_hash.update(file.read()) else: for chunk in file.chunks(): file_hash.update(chunk) return file_hash.hexdigest()
def test_clearable_file_widget(self): field = RevisionFileField() doc = DocumentFactory() file_field = FieldFile(doc, field, 'revisions/toto.pdf') widget = PhaseClearableFileInput() rendered = widget.render('toto.pdf', file_field) self.assertTrue('>toto.pdf<' in rendered) self.assertTrue('>revisions/toto.pdf<' not in rendered)
def test_django_to_filetracker_path(self): storage = FiletrackerStorage(prefix='/foo', client=DummyClient()) field = FileField(storage=storage) value = FieldFile(None, field, 'bar') self.assertEqual(django_to_filetracker_path(value), '/foo/bar') with self.assertRaises(ValueError): django_to_filetracker_path(ContentFile('whatever', name='gizmo')) self.assertEqual('/foo/bar', django_to_filetracker_path( filetracker_to_django_file('/foo/bar', storage=storage)))
def get_preview(file: FieldFile) -> bytes: """ Retrieves frame from the middle of video as preview image """ temp_filename = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) with open(temp_filename, "wb") as _file: file.seek(0) _file.write(file.read()) capture = cv2.VideoCapture(temp_filename) frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) capture.set(cv2.CAP_PROP_POS_FRAMES, round(frame_count / 2)) capture.grab() retval, img = capture.retrieve(0) image_bytes = cv2.imencode("temp/preview.jpg", img)[1].tobytes() capture.release() os.remove(temp_filename) return image_bytes
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 set_alignment_file_contents(self, value): # cache the value self._alignment_file_contents = value # and save to permanent storage if not self.local_file: model_field = self.local_file.field # note this works even though self.local_file is falsy upload_filename = model_field.generate_filename( self, "%d.%s" % (self.id, self.format)) self.local_file = FieldFile(instance=self, field=model_field, name=upload_filename) self.save() # Django FieldFile open() method fails if the file doesn't exist in filesystem (even using mode 'w') upload_file = open(self.local_file.path, 'w') upload_file.write(value) upload_file.close()
def test_serialize_model_instance(self, model_to_dict): model_to_dict.return_value = { 'date': datetime.date(2014, 12, 31), 'time': datetime.time(0, 0, 0), 'decimal': Decimal(1.0), 'str': b'str', 'unicode': 'unicode', 'file': FieldFile(instance=None, field=MagicMock(), name='foo') } expected_result = { 'date': datetime.datetime(2014, 12, 31), 'time': datetime.datetime(1, 1, 1, 0, 0, 0), 'decimal': 1.0, 'str': 'str', 'unicode': 'unicode', 'file': 'foo' } serialized_instance = utils.serialize_model_instance(None) self.assertDictEqual(expected_result, serialized_instance)
def get_exif(file_: FieldFile) -> str: """ Use exiftool to extract exif data from the given file field. """ exiftool_path = shutil.which('exiftool') if not exiftool_path: raise ExifError('Could not find `exiftool`') if not file_._committed: # pipe file content to exiftool fo = file_._file fo.seek(0) else: fo = file_.open() process = subprocess.run( [exiftool_path, '-j', '-l', '-'], check=True, input=fo.read(), stdout=subprocess.PIPE, ) return process.stdout
def _get_url(self): if not self and self.field.no_image is not None: return self.field.no_image.url return FieldFile._get_url(self)
def __init__(self, instance, field, data, key): self.image_data = data[key] self.key = key name = self.image_data['path'] FieldFile.__init__(self, instance, field, name)
def __init__(self, instance, field, path): FieldFile.__init__(self, instance, field, path) FileObjectAPI.__init__(self, path or '')