def get_form_kwargs(self): """ Get the kwargs to pass to the form for this script. By default, returns the task arguments. - 'data' list entries converted into a querydict. - 'files' list entries converted to File objects """ kwargs = self.task.arguments.copy() # don't modify self.task.arguments['data'] if 'data' in kwargs: d = QueryDict('').copy() for k, v in kwargs['data'].iteritems(): if isinstance(v, list): d.setlist(k, v) else: d[k] = v kwargs['data'] = d # Convert file dictionaries (as supplied by get_temporary_file_dict) to # SimpleUploadedFile objects which Django understands. if 'files' in kwargs: files = MultiValueDict(kwargs['files']) for filedict_list in files.viewvalues(): for i, fdict in enumerate(filedict_list): if isinstance(fdict, dict): fdict = dict(fdict) fdict["content"] = open(fdict["path"], "rb").read() filedict_list[i] = SimpleUploadedFile.from_dict(fdict) kwargs['files'] = files return kwargs
def setUp(self): self.file_attrs = { 'filename': '°ßäöü.txt', 'content': 'โจ'.encode('utf-8'), 'content-type': 'text/utf8', } self.test_file = SimpleUploadedFile.from_dict(self.file_attrs) self.follow_up = models.FollowUp(ticket=models.Ticket(queue=models.Queue()))
def setUp(self): self.file_attrs = { 'filename': '°ßäöü.txt', 'content': 'โจ'.encode('utf-8'), 'content-type': 'text/utf8', } self.test_file = SimpleUploadedFile.from_dict(self.file_attrs) self.follow_up = models.FollowUp(ticket=models.Ticket(queue=models.Queue()))
def createUploadedFile(self, test_file): return SimpleUploadedFile.from_dict({ 'filename': test_file['filename'], 'content-type': test_file['content-type'], 'content': test_file['content'], })
def get_file(self, filename): test_file = path.join(path.dirname(__file__), "test_files/factivia", filename) return SimpleUploadedFile.from_dict({ "filename": filename, "content": open(test_file, "rb").read(), "content_type": "text/html" })
def make_uploaded_zip_file(self, filename): path = os.path.join(TESTFIXTURES_DIR, filename) content_type = 'application/zip' content = '' with open(path) as f: content = f.read() return SimpleUploadedFile.from_dict({ 'filename': filename, 'content': content, 'content-type': content_type, })
def make_uploaded_zip_file(self, filename): path = os.path.join(TESTFIXTURES_DIR, filename) content_type = 'application/zip' content = '' with open(path) as f: content = f.read() return SimpleUploadedFile.from_dict({ 'filename': filename, 'content': content, 'content-type': content_type, })
def _create_image_file(validated_data): """ Create an Image file with a thumbnail """ name = 'thumbnail_%s' % validated_data["file"].name thumbnail = SimpleUploadedFile.from_dict({ 'filename': name, 'content': b'' }) image = Image.open(validated_data["file"]) image.thumbnail(THUMBNAIL_SIZE) image.save(thumbnail, "JPEG") obj = ImageFile.objects.create(thumbnail=thumbnail, **validated_data) return obj
def get_scraper(self, filename): test_file = path.join(path.dirname(__file__), "test_files/factivia", filename) return Factivia(options={ 'project': amcattest.create_test_project().id, 'articlesets': [amcattest.create_test_set().id], 'file': SimpleUploadedFile.from_dict({ "filename": filename, "content": open(test_file).read(), "content_type": "text/html" }) })
def test_load_patient_data_csv_with_data(self): f = SimpleUploadedFile.from_dict( dict( filename='test.csv', content_type='text/csv', content=( '1,2,3\n\n\n\n1,2,3\n2,2,3') )) request = self.factory.post( "/weights/loadquestions/", dict(csvfile=f)) response = load_patient_data(request) self.assertEqual(response.status_code, 200) self.assertEqual(response['content-type'], 'application/javascript')
def db_value_to_formfield_value(self, value): fileinfo = json.loads(value) if fileinfo is False: return None filename = os.path.join(settings.MEDIA_ROOT, fileinfo['mediafilename']) content = open(filename, 'rb').read() suf = SimpleUploadedFile.from_dict({ 'filename': fileinfo['filename'], 'content': content, 'content-type': fileinfo['content_type'] }) # Note that next line is needed, or "clear file" won't work: suf.url = os.path.join(settings.MEDIA_URL, fileinfo['mediafilename']) return suf
def test_import_config_csv_nodata(self): f = SimpleUploadedFile.from_dict( dict( filename='test.csv', content_type='text/csv', content='id,row\n' )) request = self.factory.post( "/weights/import/", dict(csvfile=f)) request.user = UserFactory() response = import_config(request) self.assertEqual(response.status_code, 302) r = Configuration.objects.filter(name='test') self.assertEqual(r.count(), 1)
def test_load_questions_csv_nodata(self): f = SimpleUploadedFile.from_dict( dict( filename='test.csv', content_type='text/csv', content=( '"question text,question number,answers,' 'numerical equivalent,module"\n') )) request = self.factory.post( "/weights/loadquestions/", dict(csvfile=f)) request.user = UserFactory() response = load_questions(request) self.assertEqual(response.status_code, 302)
def get_photos(self, message_parts): """save an attachment as a single photo """ # Get Maxium attachment size # max_size = self.options['max-attachment-size'] status = '' results = {} for part in message_parts: # Skip text body parts if not isinstance(part, tuple): continue (original, filename, part) = part # Skip html attachments and the like. if not part.get_content_type().startswith('image'): continue text = part.get_payload(decode=1) if not text: continue file_size = len(text) # Check if the attachment size is allowed # if (max_size != -1) and (file_size > max_size): status = '%s\nFile %s is larger than allowed attachment size (%d > %d)\n\n' \ %(status, original, file_size, max_size) continue # We use SimpleUploadedFile because it conveniently # supports the subset of file-like behavior needed by # poster. Too bad, that's the last reason we really need # to import anything from django. results[u'photo'] = SimpleUploadedFile.from_dict( {'filename': filename, 'content': text, 'content-type': part.get_content_type()}) # XXX what to do if there's more than one attachment? # we just ignore 'em. break return results
def _wrapped_view(request, username, *args, **kwargs): print("i'm here. checking *fieldnames:") for fieldname in fieldnames: print(fieldname) if fieldname in request.ANY and request.ANY[fieldname]: fieldvalue = request.ANY[fieldname] content, content_type, extension = parse_dataURI(fieldvalue) if content and content_type: request.FILES[fieldname] = SimpleUploadedFile.from_dict({ 'filename': '%s%s' % (fieldname, extension), 'content': content, 'content-type': content_type }) else: qd = getattr(request, request.method).copy() qd['%s-clear' % fieldname] = 'on' setattr(request, request.method, qd) return view_func(request, username, *args, **kwargs)
def test_import_config_csv_with_data(self): m = ModuleFactory() q = QuestionFactory() f = SimpleUploadedFile.from_dict( dict( filename='test.csv', content_type='text/csv', content='id,row\nModule %s,1\nQuestion %d,2' % ( m.name, q.number) )) request = self.factory.post( "/weights/import/", dict(csvfile=f)) request.user = UserFactory() response = import_config(request) self.assertEqual(response.status_code, 302) r = Configuration.objects.filter(name='test') self.assertEqual(r.count(), 1) r = ModuleWeight.objects.filter(module=m, weight=1) self.assertEqual(r.count(), 1) r = Weight.objects.filter(question=q, weight=2) self.assertEqual(r.count(), 1)
def attachments(self, message_parts): """save an attachment as a single photo """ # Get Maxium attachment size # max_size = self.MAX_ATTACHMENT_SIZE status = "" results = {} for part in message_parts: # Skip text body parts if not isinstance(part, tuple): continue (original, filename, part) = part text = part.get_payload(decode=1) if not text: continue file_size = len(text) # Check if the attachment size is allowed # if (max_size != -1) and (file_size > max_size): status = "%s\nFile %s is larger than allowed attachment size (%d > %d)\n\n" % ( status, original, file_size, max_size, ) continue results[u"photo"] = SimpleUploadedFile.from_dict( {"filename": filename, "content": text, "content-type": "image/jpeg"} ) # XXX what to do if there's more than one attachment? # we just ignore 'em. break return results
def to_django(self, *args, **kwargs): return SimpleUploadedFile.from_dict(self.value) if self.value else None
def _save_FIELD_file(self, field, filename, raw_field, save=True): # Create the upload directory if it doesn't already exist directory = os.path.join(settings.MEDIA_ROOT, field.get_directory_name()) if not os.path.exists(directory): os.makedirs(directory) elif not os.path.isdir(directory): raise IOError('%s exists and is not a directory' % directory) # Check for old-style usage (files-as-dictionaries). Warn here first # since there are multiple locations where we need to support both new # and old usage. if isinstance(raw_field, dict): import warnings warnings.warn( message= "Representing uploaded files as dictionaries is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.", category=DeprecationWarning, stacklevel=2) from django.core.files.uploadedfile import SimpleUploadedFile raw_field = SimpleUploadedFile.from_dict(raw_field) elif isinstance(raw_field, str): import warnings warnings.warn( message= "Representing uploaded files as strings is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.", category=DeprecationWarning, stacklevel=2) from django.core.files.uploadedfile import SimpleUploadedFile raw_field = SimpleUploadedFile(filename, raw_field) if filename is None: filename = raw_field.file_name filename = field.get_filename(filename) # If the filename already exists, keep adding an underscore to the name # of the file until the filename doesn't exist. while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)): try: dot_index = filename.rindex('.') except ValueError: # filename has no dot. filename += '_' else: filename = filename[:dot_index] + '_' + filename[dot_index:] # Save the file name on the object and write the file to disk. setattr(self, field.attname, filename) full_filename = self._get_FIELD_filename(field) if hasattr(raw_field, 'temporary_file_path'): # This file has a file path that we can move. raw_field.close() file_move_safe(raw_field.temporary_file_path(), full_filename) else: # This is a normal uploadedfile that we can stream. fp = open(full_filename, 'wb') locks.lock(fp, locks.LOCK_EX) for chunk in raw_field.chunks(): fp.write(chunk) locks.unlock(fp) fp.close() # Save the width and/or height, if applicable. if isinstance(field, ImageField) and \ (field.width_field or field.height_field): from django.utils.images import get_image_dimensions width, height = get_image_dimensions(full_filename) if field.width_field: setattr(self, field.width_field, width) if field.height_field: setattr(self, field.height_field, height) # Save the object because it has changed, unless save is False. if save: self.save()
def _save_FIELD_file(self, field, filename, raw_field, save=True): # Create the upload directory if it doesn't already exist directory = os.path.join(settings.MEDIA_ROOT, field.get_directory_name()) if not os.path.exists(directory): os.makedirs(directory) elif not os.path.isdir(directory): raise IOError('%s exists and is not a directory' % directory) # Check for old-style usage (files-as-dictionaries). Warn here first # since there are multiple locations where we need to support both new # and old usage. if isinstance(raw_field, dict): import warnings warnings.warn( message = "Representing uploaded files as dictionaries is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.", category = DeprecationWarning, stacklevel = 2 ) from django.core.files.uploadedfile import SimpleUploadedFile raw_field = SimpleUploadedFile.from_dict(raw_field) elif isinstance(raw_field, str): import warnings warnings.warn( message = "Representing uploaded files as strings is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.", category = DeprecationWarning, stacklevel = 2 ) from django.core.files.uploadedfile import SimpleUploadedFile raw_field = SimpleUploadedFile(filename, raw_field) if filename is None: filename = raw_field.file_name filename = field.get_filename(filename) # If the filename already exists, keep adding an underscore to the name # of the file until the filename doesn't exist. while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)): try: dot_index = filename.rindex('.') except ValueError: # filename has no dot. filename += '_' else: filename = filename[:dot_index] + '_' + filename[dot_index:] # Save the file name on the object and write the file to disk. setattr(self, field.attname, filename) full_filename = self._get_FIELD_filename(field) if hasattr(raw_field, 'temporary_file_path'): # This file has a file path that we can move. raw_field.close() file_move_safe(raw_field.temporary_file_path(), full_filename) else: # This is a normal uploadedfile that we can stream. fp = open(full_filename, 'wb') locks.lock(fp, locks.LOCK_EX) for chunk in raw_field.chunks(): fp.write(chunk) locks.unlock(fp) fp.close() # Save the width and/or height, if applicable. if isinstance(field, ImageField) and \ (field.width_field or field.height_field): from django.utils.images import get_image_dimensions width, height = get_image_dimensions(full_filename) if field.width_field: setattr(self, field.width_field, width) if field.height_field: setattr(self, field.height_field, height) # Save the object because it has changed, unless save is False. if save: self.save()
def test_file(): """create a file ready to upload.""" return SimpleUploadedFile.from_dict( dict(filename='test.txt', content=bytes('abc', 'UTF-8')) )
def createUploadedFile(self, test_file): return SimpleUploadedFile.from_dict({ 'filename': test_file['filename'], 'content-type': test_file['content-type'], 'content': test_file['content'], })
def to_django(self, *args, **kwargs): return SimpleUploadedFile.from_dict(self.value) if self.value else None