Python django.core.files.storage.default_storage.open() Examples
The following are 30
code examples of django.core.files.storage.default_storage.open().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.core.files.storage.default_storage
, or try the search function
.
Example #1
Source File: briefcase_client.py From kobo-predict with BSD 2-Clause "Simplified" License | 7 votes |
def _upload_instance(self, xml_file, instance_dir_path, files): xml_doc = clean_and_parse_xml(xml_file.read()) xml = StringIO() de_node = xml_doc.documentElement for node in de_node.firstChild.childNodes: xml.write(node.toxml()) new_xml_file = ContentFile(xml.getvalue()) new_xml_file.content_type = 'text/xml' xml.close() attachments = [] for attach in de_node.getElementsByTagName('mediaFile'): filename_node = attach.getElementsByTagName('filename') filename = filename_node[0].childNodes[0].nodeValue if filename in files: file_obj = default_storage.open( os.path.join(instance_dir_path, filename)) mimetype, encoding = mimetypes.guess_type(file_obj.name) media_obj = django_file(file_obj, 'media_files[]', mimetype) attachments.append(media_obj) create_instance(self.user.username, new_xml_file, attachments)
Example #2
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #3
Source File: test_azure.py From django-storages with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_open_read(self): self.storage.location = 'root' stream = io.BytesIO(b'Im a stream') name = self.storage.save('path/some file.txt', stream) fh = self.storage.open(name, 'r+b') try: self.assertEqual(fh.read(), b'Im a stream') finally: fh.close() stream = io.BytesIO() self.storage.service.get_blob_to_stream( container_name=self.storage.azure_container, blob_name='root/path/some file.txt', stream=stream, max_connections=1, timeout=10) stream.seek(0) self.assertEqual(stream.read(), b'Im a stream')
Example #4
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #5
Source File: test_azure.py From django-storages with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_open_read_write(self): self.storage.location = 'root' stream = io.BytesIO(b'Im a stream') name = self.storage.save('file.txt', stream) fh = self.storage.open(name, 'r+b') try: self.assertEqual(fh.read(), b'Im a stream') fh.write(b' foo') fh.seek(0) self.assertEqual(fh.read(), b'Im a stream foo') finally: fh.close() stream = io.BytesIO() self.storage.service.get_blob_to_stream( container_name=self.storage.azure_container, blob_name='root/file.txt', stream=stream, max_connections=1, timeout=10) stream.seek(0) self.assertEqual(stream.read(), b'Im a stream foo')
Example #6
Source File: test_azure.py From django-storages with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_model_form(self): files = { 'foo_file': SimpleUploadedFile( name='foo.pdf', content=b'foo content', content_type='application/pdf')} form = FooFileModelForm(data={}, files=files) self.assertTrue(form.is_valid()) form.save() self.assertTrue(default_storage.exists('foo_uploads/foo.pdf')) # check file content was saved fh = default_storage.open('foo_uploads/foo.pdf', 'r+b') try: self.assertEqual(fh.read(), b'foo content') finally: fh.close()
Example #7
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #8
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #9
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #10
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #11
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #12
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #13
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #14
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #15
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #16
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #17
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #18
Source File: models.py From django-flowjs with MIT License | 6 votes |
def _join_chunks(self): try: temp_file = default_storage.open(self.path, 'wb') for chunk in self.chunks.all(): chunk_bytes = chunk.file.read() temp_file.write(chunk_bytes) temp_file.close() self.final_file = self.path self.state = self.STATE_COMPLETED super(FlowFile, self).save() # send ready signal if self.join_in_background: file_is_ready.send(self) if FLOWJS_AUTO_DELETE_CHUNKS: self.delete_chunks() except Exception as e: self.state = self.STATE_JOINING_ERROR super(FlowFile, self).save() # send error signal if self.join_in_background: file_joining_failed.send(self)
Example #19
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #20
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #21
Source File: forms.py From Django-3-Web-Development-Cookbook-Fourth-Edition with MIT License | 6 votes |
def save(self, commit=True): instance = super().save(commit=False) picture_path = self.cleaned_data["picture_path"] if picture_path: temporary_image_path = os.path.join("temporary-uploads", picture_path) file_obj = default_storage.open(temporary_image_path) instance.picture.save(picture_path, file_obj, save=False) default_storage.delete(temporary_image_path) latitude = self.cleaned_data["latitude"] longitude = self.cleaned_data["longitude"] if latitude is not None and longitude is not None: instance.set_geoposition(longitude=longitude, latitude=latitude) if commit: instance.save() self.save_m2m() return instance
Example #22
Source File: forms.py From Django-3-Web-Development-Cookbook-Fourth-Edition with MIT License | 6 votes |
def save(self, commit=True): instance = super().save(commit=False) picture_path = self.cleaned_data["picture_path"] if picture_path: temporary_image_path = os.path.join("temporary-uploads", picture_path) file_obj = default_storage.open(temporary_image_path) instance.picture.save(picture_path, file_obj, save=False) default_storage.delete(temporary_image_path) latitude = self.cleaned_data["latitude"] longitude = self.cleaned_data["longitude"] if latitude is not None and longitude is not None: instance.set_geoposition(longitude=longitude, latitude=latitude) if commit: instance.save() self.save_m2m() return instance
Example #23
Source File: forms.py From Django-3-Web-Development-Cookbook-Fourth-Edition with MIT License | 6 votes |
def save(self, commit=True): instance = super().save(commit=False) picture_path = self.cleaned_data["picture_path"] if picture_path: temporary_image_path = os.path.join("temporary-uploads", picture_path) file_obj = default_storage.open(temporary_image_path) instance.picture.save(picture_path, file_obj, save=False) default_storage.delete(temporary_image_path) latitude = self.cleaned_data["latitude"] longitude = self.cleaned_data["longitude"] if latitude is not None and longitude is not None: instance.set_geoposition(longitude=longitude, latitude=latitude) if commit: instance.save() self.save_m2m() return instance
Example #24
Source File: briefcase_client.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _upload_instances(self, path): instances_count = 0 dirs, not_in_use = default_storage.listdir(path) for instance_dir in dirs: instance_dir_path = os.path.join(path, instance_dir) i_dirs, files = default_storage.listdir(instance_dir_path) xml_file = None if 'submission.xml' in files: file_obj = default_storage.open( os.path.join(instance_dir_path, 'submission.xml')) xml_file = file_obj if xml_file: try: self._upload_instance(xml_file, instance_dir_path, files) except ExpatError: continue except Exception: pass else: instances_count += 1 return instances_count
Example #25
Source File: forms.py From Django-3-Web-Development-Cookbook-Fourth-Edition with MIT License | 6 votes |
def save(self, commit=True): instance = super().save(commit=False) picture_path = self.cleaned_data["picture_path"] if picture_path: temporary_image_path = os.path.join("temporary-uploads", picture_path) file_obj = default_storage.open(temporary_image_path) instance.picture.save(picture_path, file_obj, save=False) default_storage.delete(temporary_image_path) latitude = self.cleaned_data["latitude"] longitude = self.cleaned_data["longitude"] if latitude is not None and longitude is not None: instance.set_geoposition(longitude=longitude, latitude=latitude) if commit: instance.save() self.save_m2m() return instance
Example #26
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True
Example #27
Source File: models.py From django-favicon-plus with MIT License | 6 votes |
def get_favicon(self, size, rel, update=False): """ get or create a favicon for size, rel(attr) and uploaded favicon optional: update=True """ fav, _ = FaviconImg.objects.get_or_create( faviconFK=self, size=size, rel=rel) if update and fav.faviconImage: fav.del_image() if self.faviconImage and not fav.faviconImage: tmp = Image.open(storage.open(self.faviconImage.name)) tmp.thumbnail((size, size), Image.ANTIALIAS) tmpIO = BytesIO() tmp.save(tmpIO, format='PNG') tmpFile = InMemoryUploadedFile( tmpIO, None, 'fav-%s.png' % (size,), 'image/png', sys.getsizeof(tmpIO), None) fav.faviconImage = tmpFile fav.save() return fav
Example #28
Source File: test_fields.py From django-spillway with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_shapefile(self): base = 'dir/geofield.shp' path = default_storage.path(base) os.mkdir(os.path.dirname(path)) write_shp(path, _geom) b = io.BytesIO() with zipfile.ZipFile(b, 'w') as zf: for ext in ('dbf', 'prj', 'shp', 'shx'): fname = base.replace('shp', ext) with default_storage.open(fname) as fp: zf.writestr(fname, fp.read()) shutil.rmtree(os.path.dirname(path)) upfile = SimpleUploadedFile('geofield.zip', b.getvalue()) b.close() result = self.field.to_python(upfile) self.assertIsInstance(result, OGRGeometry) self.assertIsNotNone(result.srs)
Example #29
Source File: test_models.py From django-spillway with BSD 3-Clause "New" or "Revised" License | 6 votes |
def create_image(multiband=False): tmpname = os.path.join( upload_to.path, os.path.basename(tempfile.mktemp(prefix='tmin_', suffix='.tif'))) fp = default_storage.open(tmpname, 'w+b') shape = (5, 5) if multiband: shape += (3,) b = bytearray(range(reduce(operator.mul, shape))) ras = raster.frombytes(bytes(b), shape) ras.affine = (-120, 2, 0, 38, 0, -2) ras.sref = 4326 ras.save(fp) ras.close() fp.seek(0) return fp
Example #30
Source File: models.py From Django-2-Web-Development-Cookbook-Third-Edition with MIT License | 6 votes |
def create_thumbnail(self): if not self.picture: return False picture_path, thumbnail_path = self.get_picture_paths() if thumbnail_path and not storage.exists(thumbnail_path): try: picture_file = storage.open(picture_path, "r") image = Image.open(picture_file) image = image.crop(get_square_crop_points(image)) image = image.resize((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS) image.save(thumbnail_path) except (IOError, KeyError, UnicodeDecodeError): return False return True