Python django.core.files.base.ContentFile() Examples
The following are 30
code examples of django.core.files.base.ContentFile().
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.base
, 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: briefcase_client.py From kobo-predict with BSD 2-Clause "Simplified" License | 7 votes |
def download_media_files(self, xml_doc, media_path): for media_node in xml_doc.getElementsByTagName('mediaFile'): filename_node = media_node.getElementsByTagName('filename') url_node = media_node.getElementsByTagName('downloadUrl') if filename_node and url_node: filename = filename_node[0].childNodes[0].nodeValue path = os.path.join(media_path, filename) if default_storage.exists(path): continue download_url = url_node[0].childNodes[0].nodeValue if self._get_media_response(download_url): download_res = self._current_response media_content = ContentFile(download_res.content) default_storage.save(path, media_content) self.logger.debug("Fetched %s." % filename) else: self.logger.error("Failed to fetch %s." % filename)
Example #3
Source File: test_model_anonymization.py From django-GDPR with MIT License | 6 votes |
def test_file_field(self): customer = Customer(**CUSTOMER__KWARGS) customer.save() avatar = Avatar() avatar.customer = customer avatar.image.save('test_file_secret_data', ContentFile('Super secret data'), save=False) avatar.save() avatar_2: Avatar = Avatar.objects.last() self.assertEqual(avatar_2.image.read(), b'Super secret data') avatar_2._anonymize_obj(base_encryption_key='LoremIpsumDolorSitAmet') avatar_3: Avatar = Avatar.objects.last() self.assertNotEqual(avatar_3.image.read(), b'Super secret data') # Cleanup avatar_3.image.delete() avatar_3.delete()
Example #4
Source File: test_storage.py From django-tenants with MIT License | 6 votes |
def test_file_save_with_path(self): """ Saving a pathname should create intermediate directories as necessary. """ self.assertFalse(self.storage.exists("path/to")) self.storage.save("path/to/test.file", ContentFile("file saved with path")) self.assertTrue(self.storage.exists("path/to")) with self.storage.open("path/to/test.file") as f: self.assertEqual(f.read(), b"file saved with path") self.assertTrue( os.path.exists( os.path.join( self.temp_dir, connection.schema_name, "path", "to", "test.file" ) ) ) self.storage.delete("path/to/test.file")
Example #5
Source File: conftest.py From django-webdav-storage with BSD 3-Clause "New" or "Revised" License | 6 votes |
def create_file(webdav_storage): """ Creates a file with a unique prefix in the WebDAV storage """ from django.core.files.base import ContentFile from django_webdav_storage.compat import PY3, TEXT_TYPE def inner(filename, content=b'', prefix=''): if all((PY3, isinstance(content, TEXT_TYPE))): content = content.encode('UTF-8') col = str(uuid.uuid4()) key = os.path.join(prefix.lstrip('/') or col, filename) webdav_storage.save(key, ContentFile(content, key)) return key return inner
Example #6
Source File: forms.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def save(self): photo = super(OrganizationForm, self).save() x = self.cleaned_data.get('x') y = self.cleaned_data.get('y') w = self.cleaned_data.get('width') h = self.cleaned_data.get('height') if x is not None and y is not None: image = Image.open(photo.logo) cropped_image = image.crop((x, y, w+x, h+y)) resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) # resized_image.save(photo.logo.path) resized_image_file = StringIO.StringIO() mime = mimetypes.guess_type(photo.logo.name)[0] plain_ext = mime.split('/')[1] resized_image.save(resized_image_file, plain_ext) default_storage.delete(photo.logo.name) default_storage.save(photo.logo.name, ContentFile(resized_image_file.getvalue())) resized_image_file.close() return photo
Example #7
Source File: test_stats_viewset.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _contributions_form_submissions(self): count = XForm.objects.count() path = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'forms', 'contributions') form_path = os.path.join(path, 'contributions.xml') f = open(form_path) xml_file = ContentFile(f.read()) f.close() xml_file.name = 'contributions.xml' self.xform = publish_xml_form(xml_file, self.user) self.assertTrue(XForm.objects.count() > count) instances_path = os.path.join(path, 'instances') for uuid in os.listdir(instances_path): s_path = os.path.join(instances_path, uuid, 'submission.xml') create_instance(self.user.username, open(s_path), []) self.assertEqual(self.xform.instances.count(), 6)
Example #8
Source File: test_form_show.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def test_publish_xml_xlsform_download(self): count = XForm.objects.count() path = os.path.join( self.this_directory, '..', '..', 'api', 'tests', 'fixtures', 'forms', 'contributions', 'contributions.xml') f = open(path) xml_file = ContentFile(f.read()) f.close() xml_file.name = 'contributions.xml' self.xform = publish_xml_form(xml_file, self.user) self.assertTrue(XForm.objects.count() > count) response = self.client.get(reverse(download_xlsform, kwargs={ 'username': self.user.username, 'id_string': 'contributions' }), follow=True) self.assertContains(response, 'No XLS file for your form ')
Example #9
Source File: image_tools.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _save_thumbnails(image, path, size, suffix): nm = NamedTemporaryFile(suffix='.%s' % settings.IMG_FILE_TYPE) default_storage = get_storage_class()() try: # Ensure conversion to float in operations image.thumbnail( get_dimensions(image.size, float(size)), Image.ANTIALIAS) except ZeroDivisionError: pass try: image.save(nm.name) except IOError: # e.g. `IOError: cannot write mode P as JPEG`, which gets raised when # someone uploads an image in an indexed-color format like GIF image.convert('RGB').save(nm.name) default_storage.save( get_path(path, suffix), ContentFile(nm.read())) nm.close()
Example #10
Source File: test_process_view.py From django-drf-filepond with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_upload_non_file_data(self): cf = ContentFile(self.test_data.read(), name='test.txt') upload_form = {'filepond': cf} enc_form = encode_multipart('abc', upload_form) rf = RequestFactory() req = rf.post(reverse('process'), data=enc_form, content_type='multipart/form-data; boundary=abc') req.FILES['filepond'] = cf pv = views.ProcessView.as_view() response = pv(req) self.assertEqual(response.status_code, 400, 'Expecting 400 error due' ' to non-file data being provided.') self.assertTrue('detail' in response.data, 'Error detail missing in response.') self.assertIn(response.data['detail'], ('Invalid data type has been ' 'parsed.')) # Based on the modification for issue #4, test that we can successfully # handle an upload that provides its data under a field name that is not # the default "filepond" and that an error is thrown when an invalid # name is expected
Example #11
Source File: test_edit_page.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_upload_file_publish(self): """ Check that file uploads work when directly publishing """ file_upload = ContentFile(b"A new file", name='published-file.txt') post_data = { 'title': 'New file', 'slug': 'new-file', 'file_field': file_upload, 'action-publish': "Publish", } response = self.client.post(reverse('wagtailadmin_pages:edit', args=[self.file_page.id]), post_data) # Should be redirected to explorer self.assertRedirects(response, reverse('wagtailadmin_explore', args=[self.root_page.id])) # Check the new file exists file_page = FilePage.objects.get() self.assertEqual(file_page.file_field.name, file_upload.name) self.assertTrue(os.path.exists(file_page.file_field.path)) self.assertEqual(file_page.file_field.read(), b"A new file")
Example #12
Source File: test_model_anonymization.py From django-GDPR with MIT License | 6 votes |
def test_file_field_real_file(self): anonymizer = anonymizer_register[Avatar] anonymizer.image.replacement_file = 'test_file' customer = Customer(**CUSTOMER__KWARGS) customer.save() avatar = Avatar() avatar.customer = customer avatar.image.save('test_file_real', ContentFile('Super secret data')) avatar_2: Avatar = Avatar.objects.last() self.assertEqual(avatar_2.image.read(), b'Super secret data') avatar_2._anonymize_obj(base_encryption_key='LoremIpsumDolorSitAmet') avatar_3: Avatar = Avatar.objects.last() self.assertNotEqual(avatar_3.image.read(), b'Super secret data') anonymizer.image.replacement_file = None # Cleanup avatar_3.image.delete() avatar_3.delete()
Example #13
Source File: test_views.py From ideascube with GNU Affero General Public License v3.0 | 6 votes |
def test_export_book_notices(staffapp, monkeypatch): book1 = BookFactory(isbn="123456", name="my book title") name_utf8 = u'النبي (كتاب)' BookFactory(isbn="654321", name=name_utf8) monkeypatch.setattr(BookExport, 'get_filename', lambda s: 'myfilename') resp = staffapp.get(reverse('library:book_export')) assert 'myfilename.zip' in resp['Content-Disposition'] content = ContentFile(resp.content) assert zipfile.is_zipfile(content) archive = zipfile.ZipFile(content) cover_name = '{}.jpg'.format(book1.pk) assert cover_name in archive.namelist() assert 'myfilename.csv' in archive.namelist() assert len(archive.namelist()) == 3 csv_content = archive.open('myfilename.csv').read().decode('utf-8') assert csv_content.startswith( 'isbn,authors,serie,name,subtitle,description,publisher,section,lang,' 'cover,tags\r\n') assert "my book title" in csv_content assert cover_name in csv_content assert name_utf8 in csv_content
Example #14
Source File: models.py From django-pyas2 with GNU General Public License v3.0 | 6 votes |
def create_from_as2mdn(self, as2mdn, message, status, return_url=None): """Create the MDN from the pyas2lib's MDN object""" signed = True if as2mdn.digest_alg else False mdn, _ = self.update_or_create( message=message, defaults=dict( mdn_id=as2mdn.message_id, status=status, signed=signed, return_url=return_url, ), ) filename = f"{uuid4()}.mdn" mdn.headers.save( name=f"{filename}.header", content=ContentFile(as2mdn.headers_str) ) mdn.payload.save(filename, content=ContentFile(as2mdn.content)) return mdn
Example #15
Source File: test_views.py From django-pyas2 with GNU General Public License v3.0 | 6 votes |
def setUp(self): self.user = User.objects.create(username="dummy") with open(os.path.join(TEST_DIR, "client_public.pem"), "rb") as fp: self.cert = PublicCertificate.objects.create( name="test-cert", certificate=fp.read() ) with open(os.path.join(TEST_DIR, "client_private.pem"), "rb") as fp: self.private_key = PrivateKey.objects.create( name="test-key", key=fp.read(), key_pass="test" ) with open(os.path.join(TEST_DIR, "testmessage.edi"), "rb") as fp: self.message = Message.objects.create( message_id="some-message-id", direction="IN", status="S", ) self.message.payload.save("testmessage.edi", fp) self.mdn = Mdn.objects.create( mdn_id="some-mdn-id", message=self.message, status="S" ) self.mdn.payload.save("some-mdn-id.mdn", ContentFile("MDN Content"))
Example #16
Source File: problem_data.py From online-judge with GNU Affero General Public License v3.0 | 6 votes |
def compile(self): from judge.models import problem_data_storage yml_file = '%s/init.yml' % self.problem.code try: init = yaml.safe_dump(self.make_init()) except ProblemDataError as e: self.data.feedback = e.message self.data.save() problem_data_storage.delete(yml_file) else: self.data.feedback = '' self.data.save() if init: problem_data_storage.save(yml_file, ContentFile(init)) else: # Don't write empty init.yml since we should be looking in manually managed # judge-server#670 will not update cache on empty init.yml, # but will do so if there is no init.yml, so we delete the init.yml problem_data_storage.delete(yml_file)
Example #17
Source File: facility.py From tom_base with GNU General Public License v3.0 | 6 votes |
def save_data_products(self, observation_record, product_id=None): from tom_dataproducts.models import DataProduct from tom_dataproducts.utils import create_image_dataproduct final_products = [] products = self.data_products(observation_record.observation_id, product_id) for product in products: dp, created = DataProduct.objects.get_or_create( product_id=product['id'], target=observation_record.target, observation_record=observation_record, ) if created: product_data = requests.get(product['url']).content dfile = ContentFile(product_data) dp.data.save(product['filename'], dfile) dp.save() logger.info('Saved new dataproduct: {}'.format(dp.data)) if AUTO_THUMBNAILS: create_image_dataproduct(dp) dp.get_preview() final_products.append(dp) return final_products
Example #18
Source File: filemodels.py From django-leonardo with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _copy_file(self, destination, overwrite=False): """ Copies the file to a destination files and returns it. """ if overwrite: # If the destination file already exists default storage backend # does not overwrite it but generates another filename. # TODO: Find a way to override this behavior. raise NotImplementedError src_file_name = self.file.name storage = self.file.storages['public' if self.is_public else 'private'] # This is needed because most of the remote File Storage backend do not # open the file. src_file = storage.open(src_file_name) src_file.open() return storage.save(destination, ContentFile(src_file.read()))
Example #19
Source File: downloader.py From arxiv-vanity with Apache License 2.0 | 6 votes |
def download_source_file(arxiv_id): """ Download the LaTeX source of this paper and returns as ContentFile. """ source_url = arxiv_id_to_source_url(arxiv_id) headers = {"User-Agent": "arXivVanity (https://www.arxiv-vanity.com)"} res = requests.get(source_url, headers=headers) res.raise_for_status() extension = guess_extension_from_headers(res.headers) if not extension: raise DownloadError( "Could not determine file extension from " "headers: Content-Type: {}; " "Content-Encoding: {}".format( res.headers.get("content-type"), res.headers.get("content-encoding") ) ) file = ContentFile(res.content) file.name = arxiv_id_to_source_file(arxiv_id) + extension return file
Example #20
Source File: test_fields.py From course-discovery with GNU Affero General Public License v3.0 | 5 votes |
def test_to_internal_value(self): base64_header = "data:image/jpeg;base64," base64_data = ( "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAKBueIx4ZKCMgoy0qqC+8P//8Nzc8P/////////////////////" "/////////////////////////////////////2wBDAaq0tPDS8P///////////////////////////////////////////////////////" "///////////////////////wgARCADIAMgDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECA//EABYBAQEBAAAAAAAAAAAAAAA" "AAAABAv/aAAwDAQACEAMQAAABwADU6ZMtZFujDQzNwzbowtMNCZ68gAAADTI1kLci3I1INMjTI0yNZAAAAAAAAAAAAAAABrOpQgACStQAl" "JrOoCUAACCyazpQgACDUWUSozrNKJQAAILJrOlGTUhAoClIsiCrZZQgACCyazSCgAALZRmiAtyjTJdIKyKiwAAACxQlEsAAAAAAAAAAAAA" "AAAAAAAAAAAAAAAAAAAAADcMtDLQy3DLpzDcMunMNwy6cwAABZTpEBQg3lC43g1Lk6cunM65sLz6cwAAAAAAAABQAAAAA/8QAIBAAAwABB" "AMBAQAAAAAAAAAAAAERQRAhMUACMEJwIP/aAAgBAQABBQL+ITabIhCEIJEIQhCE9Xz8rnOcZfHiYEZXOPTSl1pSlEylKUpS/gL7L7L7L7L" "7L/TYTdjQkNEIRaREJpCEXoXJlnkfL4M5Z4i5zkZz6FplmwuGUyPlC51u/e//xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAEDAQE/ASn/x" "AAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAECAQE/ASn/xAAUEAEAAAAAAAAAAAAAAAAAAACQ/9oACAEBAAY/Ahx//8QAJBAAAwABBAEEAwE" "AAAAAAAAAAAERMRAhMEEgQFFhcWBwwfD/2gAIAQEAAT8h8HKydh2CUwPuYUzlHCyUHA9uRUsmcMomOFaK1wLJ76HoHgjsLH/dn8mQ7D3Q9" "zDcSjEYjVC4FszMxkMZBZG66fUUKGVMMED6mYvgZ0yqQ9go+fxBZ4H6BizwP0JZ4H4QfIWeC+L0T4iz4UvGuEs6UvAtHquEs8r1XEvo6X9" "bLdkiwCJCEhErIKoYe5I6BqMoQ3sRo02HuSOgefPAdizIvyYeAeI/YzIw0lkPIfX2NGjYPPnkVW0qEZo67GWwdSDSobUFVt0HSQ0FS3p2V" "PsaQVW0e79HSopsVFKXSn//2gAMAwEAAgADAAAAEABMIOBEPCPCAAAEIAEIEEAAAAAAAAAAAAAAAAAAPP8A/wDwAIX/AP8A/wDD8/8A/wC" "DCpP/AP8A/wAPzQuBBS4D3/8A/DzpBAMAYIX+wyMAAAAYIU8J7f7AAAAQQUgEpAz7AAAAAAAAAAAAAAAEMMMEEMAMAAAAAsQIQo0YY8AAA" "AAAAAAA88cAA//EABwRAQADAAIDAAAAAAAAAAAAAAEAESAQMDFQYP/aAAgBAwEBPxD05h88mWGHkywly9GGD0DLly4/H//EABwRAQADAAI" "DAAAAAAAAAAAAAAEAESAQMDFQYP/aAAgBAgEBPxD07g5ckcHKZIypWnzgidCXKlSvkP/EACoQAQACAQIEBQUBAQEAAAAAAAEAESExQRAwU" "WEgcYGh4ZGxwdHwQGDx/9oACAEBAAE/EPBYOzt8x3PpGgv6S2l1HA3l2i6wFNoXNte3ep9EvT5l4lqmTmqlQRsZQnYdPmUi0vSCUmqmdQ9" "e0xiwTMXXpyBYO8RUbJ+4/wB9ZoekdD6R+Wh7B+Z7wj+PWbDGc/Wfg/MODgsxEVGyfuKyDGz/AGYwFXBrElSsZ87gIhTfXvBT2ACJSjt41" "Q1dS9msdL+IAsuefxAAV67/ABFQejK0FVF1gC7zOCyDzG1VG6N3eZOLuOgKA9ZfKsdL+JSU2LsghIu5iAHXvLaBUe135mOf+P0JUqVKlSp" "XA7R147TeVKlSpUqVKlTbNLkapv4jTk7Jpcg5m8CAVpKQVK05WyaXjWo2ZvM1LgrtxdTk7JpeBBGbXeXweAZmWIhmPB8jZNLg0inxXwEI+" "X2TSi0XNYTeb+J/swxMniqZr49kGm5nqRDZ478ThrBEzFtxpHgeC+kvFMtg1FvXlHA1m3A1/wAY8d+O1cp5RpDgsNeIWch5F8SHgC5ePET" "eP/HkEd5lq24NltMMOcYhJL14CFsEMBL1LhhN5m1XV/20uDpDSK+soRY3l9hYkSlOkFuy1BCXm/aXOq4KR05Ear1qWzdP1Prh1/8AJo+f7" "h9k/HGnDPuYF0DRte7Ajtt6+k1fP9TW8n7k+x+094/ea/40ZczOsQbFH39YEQ6+NAVmFRZC63iqltjLEY0iIsadJSBuWISs3LwNhNkaVGK" "Rs+IgtDMCx0ZaaM17RbT1ZXGhTcsRob+c2RpUdqf4wBVe3lOxv0lNz27fuXRceX0mbTpt53KUY26dv3KUeXQ6RGx7Hb+uCBXn+Ihuir7fM" "//Z" ) base64_full = base64_header + base64_data expected = ContentFile(base64.b64decode(base64_data), name='tmp.jpeg') assert list(StdImageSerializerField().to_internal_value(base64_full).chunks()) == list(expected.chunks())
Example #21
Source File: test_edit_page.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_upload_file_draft(self): """ Check that file uploads work when saving a draft """ file_upload = ContentFile(b"A new file", name='draft-file.txt') post_data = { 'title': 'New file', 'slug': 'new-file', 'file_field': file_upload, } response = self.client.post(reverse('wagtailadmin_pages:edit', args=[self.file_page.id]), post_data) # Should be redirected to edit page self.assertRedirects(response, reverse('wagtailadmin_pages:edit', args=[self.file_page.id])) # Check the file was uploaded file_path = os.path.join(settings.MEDIA_ROOT, file_upload.name) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as saved_file: self.assertEqual(saved_file.read(), b"A new file") # Publish the draft just created FilePage.objects.get().get_latest_revision().publish() # Get the file page, check the file is set file_page = FilePage.objects.get() self.assertEqual(file_page.file_field.name, file_upload.name) self.assertTrue(os.path.exists(file_page.file_field.path)) self.assertEqual(file_page.file_field.read(), b"A new file")
Example #22
Source File: test_import_utils.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_writing_file_with_format(self): f = "{}/files/example.csv".format(TEST_ROOT) (_, filename) = os.path.split(f) with open(f, "rb") as infile: content_orig = infile.read() upload_file = ContentFile(content_orig) import_formats = get_import_formats() import_formats = [ x for x in import_formats if x.__name__ == "CSV" ] input_format = import_formats[0]() file_storage = write_to_file_storage(upload_file, input_format) self.assertEqual(type(file_storage).__name__, "TempFolderStorage") file_orig_checksum = hashlib.md5() file_orig_checksum.update(content_orig) file_orig_checksum = file_orig_checksum.hexdigest() file_new_checksum = hashlib.md5() with open(file_storage.get_full_path(), "rb") as file_new: file_new_checksum.update(file_new.read()) file_new_checksum = file_new_checksum.hexdigest() self.assertEqual(file_orig_checksum, file_new_checksum)
Example #23
Source File: conftest.py From django-selectel-storage with MIT License | 5 votes |
def create_file(selectel_storage): """ Creates a file with a unique prefix in the Selectel Cloud Storage container and then deletes it (the file) after a test case finished """ from django.core.files.base import ContentFile from django_selectel_storage.compat import PY3, TEXT_TYPE created_records = [] def file_creator(filename, content=b'', prefix=''): if all(( PY3, isinstance(content, TEXT_TYPE) )): content = content.encode('UTF-8') container = str(uuid.uuid4()) key = os.path.join(prefix.lstrip('/') or container, filename) selectel_storage.save(key, ContentFile(content, key)) created_records.append(key) return key yield file_creator for key in created_records: selectel_storage.delete(key)
Example #24
Source File: download_course_images.py From course-discovery with GNU Affero General Public License v3.0 | 5 votes |
def handle(self, *args, **options): courses = Course.objects.filter(card_image_url__isnull=False).exclude(card_image_url='').order_by('key') if not options['overwrite_existing']: courses = courses.filter(image='') count = courses.count() if count < 1: logger.info('All courses are up to date.') return logger.info('Retrieving images for [%d] courses...', count) for course in courses: logger.info('Retrieving image for course [%s] from [%s]...', course.key, course.card_image_url) try: response = requests.get(course.card_image_url) if response.status_code == requests.codes.ok: # pylint: disable=no-member content_type = response.headers['Content-Type'].lower() extension = IMAGE_TYPES.get(content_type) if extension: filename = '{uuid}.{extension}'.format(uuid=str(course.uuid), extension=extension) course.image.save(filename, ContentFile(response.content)) logger.info('Image for course [%s] successfully updated.', course.key) else: # pylint: disable=line-too-long msg = 'Image retrieved for course [%s] from [%s] has an unknown content type [%s] and will not be saved.' logger.error(msg, course.key, course.card_image_url, content_type) else: msg = 'Failed to download image for course [%s] from [%s]! Response was [%d]:\n%s' logger.error(msg, course.key, course.card_image_url, response.status_code, response.content) except Exception: # pylint: disable=broad-except logger.exception('An unknown exception occurred while downloading image for course [%s]', course.key)
Example #25
Source File: test_views.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): self.document = models.Document(title="Test document") self.filename = 'docs\u0627\u0644\u0643\u0627\u062a\u062f\u0631\u0627' '\u064a\u064a\u0629_\u0648\u0627\u0644\u0633\u0648\u0642' try: self.document.file.save(self.filename, ContentFile("A boring example document")) except UnicodeEncodeError: raise unittest.SkipTest("Filesystem doesn't support unicode filenames")
Example #26
Source File: fields.py From course-discovery with GNU Affero General Public License v3.0 | 5 votes |
def to_internal_value(self, data): """ Save base 64 encoded images """ # SOURCE: http://matthewdaly.co.uk/blog/2015/07/04/handling-images-as-base64-strings-with-django-rest-framework/ if not data: return None if isinstance(data, str) and data.startswith('data:image'): # base64 encoded image - decode file_format, imgstr = data.split(';base64,') # format ~= data:image/X;base64,/xxxyyyzzz/ ext = file_format.split('/')[-1] # guess file extension data = ContentFile(base64.b64decode(imgstr), name='tmp.' + ext) return super(StdImageSerializerField, self).to_internal_value(data)
Example #27
Source File: fields.py From django-GDPR with MIT License | 5 votes |
def get_replacement_file(self): if self.replacement_file is not None: return File(open(self.replacement_file, "rb")) elif getattr(settings, "GDPR_REPLACE_FILE_PATH", None) is not None: return File(open(getattr(settings, "GDPR_REPLACE_FILE_PATH"), "rb")) else: return ContentFile("THIS FILE HAS BEEN ANONYMIZED.")
Example #28
Source File: storage.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
def save_manifest(self): payload = {'paths': self.hashed_files, 'version': self.manifest_version} if self.exists(self.manifest_name): self.delete(self.manifest_name) contents = json.dumps(payload).encode() self._save(self.manifest_name, ContentFile(contents))
Example #29
Source File: test_file_field.py From django-localized-fields with MIT License | 5 votes |
def test_pre_save(cls): """Tests whether the :see:pre_save function works properly.""" instance = cls.FileFieldModel() instance.file = {"en": ContentFile("test", "testfilename")} instance._meta.get_field("file").pre_save(instance, False) assert instance.file.en._committed is True
Example #30
Source File: test_file_field.py From django-localized-fields with MIT License | 5 votes |
def test_assign(cls): """Tests whether the :see:LocalizedFileValueDescriptor works properly.""" temp_file = tempfile.NamedTemporaryFile(dir=MEDIA_ROOT) instance = cls.FileFieldModel() instance.file = {"en": temp_file.name} assert isinstance(instance.file.en, LocalizedFieldFile) assert instance.file.en.name == temp_file.name field_dump = pickle.dumps(instance.file) instance = cls.FileFieldModel() instance.file = pickle.loads(field_dump) assert instance.file.en.field == instance._meta.get_field("file") assert instance.file.en.instance == instance assert isinstance(instance.file.en, LocalizedFieldFile) instance = cls.FileFieldModel() instance.file = {"en": ContentFile("test", "testfilename")} assert isinstance(instance.file.en, LocalizedFieldFile) assert instance.file.en.name == "testfilename" another_instance = cls.FileFieldModel() another_instance.file = {"ro": instance.file.en} assert another_instance == another_instance.file.ro.instance assert another_instance.file.ro.lang == "ro"