def load(self, file_storage): field_dict = self.data field_dict = field_dict.copy() tmp_name = field_dict.pop('tmp_name') file_obj = UploadedFile(file=file_storage.open(tmp_name), **field_dict) file_obj.url = file_storage.url(tmp_name) return file_obj
def load(self, file_storage): field_dict = self.data field_dict = field_dict.copy() tmp_name = field_dict.pop('tmp_name') file_obj = UploadedFile(file=file_storage.open(tmp_name), **field_dict) file_obj.url = file_storage.url(tmp_name) return file_obj
def value_from_datadict(self, data, files, name): # we cache the return value of this function, since it is called a # bunch of time, and is expensive if self._cached_value is self._sentinel: upload = super().value_from_datadict(data, files, name) if upload != FILE_INPUT_CONTRADICTION: self.signed_path = data.get(self.signed_path_field_name(name), None) data_uri = data.get(self.data_uri_field_name(name)) has_file = (upload or data_uri) # the path to the cached uploaded file path = None # if we have a cache key, and no file, fetch the file from the cache if self.signed_path and not has_file: try: path = self.signer.unsign(self.signed_path) except BadSignature: # False means the field value should be cleared, which # is the best thing we can do in this case. It # shouldn't happen anyways. self.signed_path = "" self._cached_value = None return self._cached_value elif has_file: # we have a file, so write it to disk, just in case form validation fails with NamedTemporaryFile(prefix="".join( CHOICES[x % 64] for x in os.urandom(16)), suffix=".jpg", dir=self.tmp_dir, delete=False) as f: # write the uploaded file to disk, or the data from the dataURI try: if upload: f.write(upload.read()) else: f.write( b64decode(data_uri[data_uri.find(",") + 1:])) except Error: pass else: path = f.name self.signed_path = self.signer.sign(f.name) if path: upload = UploadedFile(open(path, "rb"), name=path, size=os.path.getsize(path)) # tack on a URL attribute so the parent Widget thinks it # has an initial value upload.url = settings.MEDIA_URL + os.path.relpath( upload.file.name, settings.MEDIA_ROOT) self._cached_value = upload return self._cached_value
def value_from_datadict(self, data, files, name): # we cache the return value of this function, since it is called a # bunch of time, and is expensive if self._cached_value is self._sentinel: upload = super().value_from_datadict(data, files, name) if upload != FILE_INPUT_CONTRADICTION: self.signed_path = data.get(self.signed_path_field_name(name), None) data_uri = data.get(self.data_uri_field_name(name)) has_file = (upload or data_uri) # the path to the cached uploaded file path = None # if we have a cache key, and no file, fetch the file from the cache if self.signed_path and not has_file: try: path = self.signer.unsign(self.signed_path) except BadSignature: # False means the field value should be cleared, which # is the best thing we can do in this case. It # shouldn't happen anyways. self.signed_path = "" self._cached_value = None return self._cached_value elif has_file: # we have a file, so write it to disk, just in case form validation fails with NamedTemporaryFile(prefix="".join(CHOICES[x % 64] for x in os.urandom(16)), suffix=".jpg", dir=self.tmp_dir, delete=False) as f: # write the uploaded file to disk, or the data from the dataURI try: if upload: f.write(upload.read()) else: f.write(b64decode(data_uri[data_uri.find(",")+1:])) except Error: pass else: path = f.name self.signed_path = self.signer.sign(f.name) if path: upload = UploadedFile(open(path, "rb"), name=path, size=os.path.getsize(path)) # tack on a URL attribute so the parent Widget thinks it # has an initial value upload.url = settings.MEDIA_URL + os.path.relpath(upload.file.name, settings.MEDIA_ROOT) self._cached_value = upload return self._cached_value