Python django.core.files.uploadedfile.InMemoryUploadedFile() Examples

The following are 30 code examples of django.core.files.uploadedfile.InMemoryUploadedFile(). 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.uploadedfile , or try the search function .
Example #1
Source File: tools.py    From kobo-predict with BSD 2-Clause "Simplified" License 15 votes vote down vote up
def sms_media_to_file(file_object, name):
    if isinstance(file_object, basestring):
        file_object = io.BytesIO(file_object)

    def getsize(f):
        f.seek(0)
        f.read()
        s = f.tell()
        f.seek(0)
        return s

    name = name.strip()
    content_type, charset = mimetypes.guess_type(name)
    size = getsize(file_object)
    return InMemoryUploadedFile(file=file_object, name=name,
                                field_name=None, content_type=content_type,
                                charset=charset, size=size) 
Example #2
Source File: forms.py    From dj4e-samples with MIT License 7 votes vote down vote up
def save(self, commit=True) :
        instance = super(CreateForm, self).save(commit=False)

        # We only need to adjust picture if it is a freshly uploaded file
        f = instance.picture   # Make a copy
        if isinstance(f, InMemoryUploadedFile):  # Extract data from the form to the model
            bytearr = f.read();
            instance.content_type = f.content_type
            instance.picture = bytearr  # Overwrite with the actual image data

        if commit:
            instance.save()

        return instance

# https://docs.djangoproject.com/en/3.0/topics/http/file-uploads/
# https://stackoverflow.com/questions/2472422/django-file-upload-size-limit
# https://stackoverflow.com/questions/32007311/how-to-change-data-in-django-modelform
# https://docs.djangoproject.com/en/3.0/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other 
Example #3
Source File: test_form_metadata.py    From kobo-predict with BSD 2-Clause "Simplified" License 7 votes vote down vote up
def test_windows_csv_file_upload(self):
        count = MetaData.objects.filter(data_type='media').count()
        media_file = os.path.join(
            self.this_directory, 'fixtures', 'transportation',
            'transportation.csv')
        f = InMemoryUploadedFile(open(media_file),
                                 'media',
                                 'transportation.csv',
                                 'application/octet-stream',
                                 2625,
                                 None)
        MetaData.media_upload(self.xform, f)
        media_list = MetaData.objects.filter(data_type='media')
        new_count = media_list.count()
        self.assertEqual(count + 1, new_count)
        media = media_list.get(data_value='transportation.csv')
        self.assertEqual(media.data_file_type, 'text/csv') 
Example #4
Source File: models.py    From niji with MIT License 7 votes vote down vote up
def save(self, *args, **kwargs):
        existing_avatar = ForumAvatar.objects.filter(user=self.user).first()
        if existing_avatar:
            self.id = existing_avatar.id
        if not self.image:
            self.use_gravatar = True
        else:
            i = Image.open(self.image)
            i.thumbnail((120, 120), Image.ANTIALIAS)
            i_io = BytesIO()
            i.save(i_io, format='PNG')
            self.image = InMemoryUploadedFile(
                i_io, None, '%s.png' % self.user_id, 'image/png', None, None
            )
        print(self.image)
        super(ForumAvatar, self).save(*args, **kwargs) 
Example #5
Source File: test_xform_submission_api.py    From kobo-predict with BSD 2-Clause "Simplified" License 7 votes vote down vote up
def test_post_submission_require_auth_anonymous_user(self):
        self.user.profile.require_auth = True
        self.user.profile.save()
        count = Attachment.objects.count()
        s = self.surveys[0]
        media_file = "1335783522563.jpg"
        path = os.path.join(self.main_directory, 'fixtures',
                            'transportation', 'instances', s, media_file)
        with open(path) as f:
            f = InMemoryUploadedFile(f, 'media_file', media_file, 'image/jpg',
                                     os.path.getsize(path), None)
            submission_path = os.path.join(
                self.main_directory, 'fixtures',
                'transportation', 'instances', s, s + '.xml')
            with open(submission_path) as sf:
                data = {'xml_submission_file': sf, 'media_file': f}
                request = self.factory.post('/submission', data)
                response = self.view(request)
                self.assertEqual(response.status_code, 401)
                response = self.view(request, username=self.user.username)
                self.assertEqual(response.status_code, 401)
                self.assertEqual(count, Attachment.objects.count()) 
Example #6
Source File: fields.py    From django-croppie with MIT License 7 votes vote down vote up
def crop_image(self, data_image, ratio):
        image = Image.open(data_image)
        cropped_image = image.crop(ratio)

        # saving image to memory
        thumb_io = io.BytesIO()
        cropped_image.save(
            thumb_io,
            data_image.content_type.split('/')[-1].upper()
        )

        # creating new InMemoryUploadedFile() based on the modified file
        file = InMemoryUploadedFile(
            thumb_io,
            'photo',
            data_image._get_name(),
            data_image.content_type,
            None, None,
        )
        return file 
Example #7
Source File: common.py    From substra-backend with Apache License 2.0 7 votes vote down vote up
def get_sample_objective():

    dir_path = os.path.dirname(os.path.realpath(__file__))

    description_content = "Super objective"
    description_filename = "description.md"
    description = get_temporary_text_file(description_content, description_filename)

    metrics_filename = "metrics.zip"
    f = BytesIO(b'')
    with open(os.path.join(dir_path,
                           '../../../fixtures/chunantes/objectives/objective0/metrics.zip'), 'rb') as zip_file:
        flength = f.write(zip_file.read())
    metrics = InMemoryUploadedFile(f, None, metrics_filename,
                                   'application/zip', flength, None)
    metrics.seek(0)

    return description, description_filename, metrics, metrics_filename 
Example #8
Source File: cache.py    From telemetry-analysis-service with Mozilla Public License 2.0 7 votes vote down vote up
def retrieve(self, key, field_name):
        metadata = self.metadata(key)
        content = self.backend.get(self.prefix(key) + "_content")
        if metadata and content:
            out = BytesIO()
            out.write(content)
            upload = InMemoryUploadedFile(
                file=out,
                field_name=field_name,
                name=metadata["name"],
                content_type=metadata["content_type"],
                size=metadata["size"],
                charset=metadata["charset"],
            )
            upload.file.seek(0)
        else:
            upload = None
        return upload 
Example #9
Source File: serializers.py    From SchoolIdolAPI with Apache License 2.0 6 votes vote down vote up
def _tinypng_images(self, validated_data):
        idolName = self.context['request'].data.get('idol', None)
        if not idolName:
            idolName = self.instance.idol.name
        idolId = validated_data['id'] if 'id' in validated_data else self.instance.id
        for (field, value) in validated_data.items():
            if value and (isinstance(value, InMemoryUploadedFile) or isinstance(value, TemporaryUploadedFile)):
                filename = value.name
                value = shrinkImageFromData(value.read(), filename)
                validated_data[field] = value
                if field in models.cardsImagesToName:
                    value.name = models.cardsImagesToName[field]({
                        'id': idolId,
                        'firstname': idolName.split(' ')[-1] if idolName else 'Unknown',
                    })
        return validated_data 
Example #10
Source File: base.py    From django-versatileimagefield with MIT License 6 votes vote down vote up
def save_image(self, imagefile, save_path, file_ext, mime_type):
        """
        Save an image to self.storage at `save_path`.

        Arguments:
            `imagefile`: Raw image data, typically a BytesIO instance.
            `save_path`: The path within self.storage where the image should
                         be saved.
            `file_ext`: The file extension of the image-to-be-saved.
            `mime_type`: A valid image mime type (as found in
                         versatileimagefield.utils)
        """
        file_to_save = InMemoryUploadedFile(
            imagefile,
            None,
            'foo.%s' % file_ext,
            mime_type,
            imagefile.tell(),
            None
        )
        file_to_save.seek(0)
        self.storage.save(save_path, file_to_save) 
Example #11
Source File: uploadhandler.py    From luscan-devel with GNU General Public License v2.0 6 votes vote down vote up
def file_complete(self, file_size):
        """
        Return a file object if we're activated.
        """
        if not self.activated:
            return

        self.file.seek(0)
        return InMemoryUploadedFile(
            file = self.file,
            field_name = self.field_name,
            name = self.file_name,
            content_type = self.content_type,
            size = file_size,
            charset = self.charset
        ) 
Example #12
Source File: tests_query_datasample.py    From substra-backend with Apache License 2.0 6 votes vote down vote up
def test_add_data_sample_ko_already_exists(self):
        url = reverse('substrapp:data_sample-list')

        self.add_default_data_manager()

        file_mock = MagicMock(spec=InMemoryUploadedFile)
        file_mock.name = 'foo.zip'
        file_mock.read = MagicMock(return_value=self.data_file.file.read())
        file_mock.open = MagicMock(return_value=file_mock)

        _, datasamples_path_from_file = store_datasamples_archive(file_mock)

        d = DataSample(path=datasamples_path_from_file)
        # trigger pre save
        d.save()

        data = {
            'file': file_mock,
            'json': json.dumps({
                'data_manager_keys': [get_hash(self.data_data_opener)],
                'test_only': True,
            })
        }
        extra = {
            'HTTP_ACCEPT': 'application/json;version=0.0',
        }

        with mock.patch.object(zipfile, 'is_zipfile') as mis_zipfile:
            mis_zipfile.return_value = True
            response = self.client.post(url, data, format='multipart', **extra)
            r = response.json()
            self.assertEqual(r['message'],
                             [[{'pkhash': ['data sample with this pkhash already exists.']}]])
            self.assertEqual(response.status_code, status.HTTP_409_CONFLICT) 
Example #13
Source File: forms.py    From SchoolIdolAPI with Apache License 2.0 6 votes vote down vote up
def save(self, commit=True, use_card_filenames=False):
        instance = super(TinyPngForm, self).save(commit=False)
        for field in self.fields.keys():
            if (hasattr(instance, field)
                and field in dir(self.Meta.model)
                and type(self.Meta.model._meta.get_field(field)) == models.models.ImageField):
                image = self.cleaned_data[field]
                if image and (isinstance(image, InMemoryUploadedFile) or isinstance(image, TemporaryUploadedFile)):
                    filename = image.name
                    _, extension = os.path.splitext(filename)
                    if extension.lower() == '.png':
                        image = shrinkImageFromData(image.read(), filename)
                    if use_card_filenames and field in models.cardsImagesToName:
                        image.name = models.cardsImagesToName[field]({
                            'id': instance.id,
                            'firstname': instance.idol.name.split(' ')[-1] if instance.idol and instance.idol.name else 'Unknown',
                        })
                    else:
                        image.name = randomString(32) + extension
                    setattr(instance, field, image)
        if commit:
            instance.save()
        return instance 
Example #14
Source File: test_widgets.py    From django-content-gallery with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_render_with_uploaded_image(self):
        """
        Checks whether the template_with_initial is not affected by
        the render method if it has been called with just uploaded image
        """
        # set initial template_with_initial value
        self.widget.template_with_initial = "bar"
        # create a mock object of just uploaded image
        image = mock.MagicMock(spec=InMemoryUploadedFile)
        # patch the parent's render method
        with mock.patch.object(
            AdminFileWidget,
            'render',
            return_value='foo'
        ) as render:
            # call the method with just uploaded image mock
            result = widgets.ImageWidget.render(self.widget, 'name', image)
            # check whether the parent's method has been called
            # with the same arguments
            render.assert_called_with('name', image, None)
        # check whether the method returns the result of the parent's method
        self.assertEqual(result, 'foo')
        # check whether the template_with_initial has not been changed
        self.assertEqual(self.widget.template_with_initial, 'bar') 
Example #15
Source File: octoprint_views.py    From TheSpaghettiDetective with GNU Affero General Public License v3.0 6 votes vote down vote up
def cap_image_size(pic):
    im = Image.open(pic.file)
    if max(im.size) <= 1296:
        pic.file.seek(0)
        return pic

    im.thumbnail((1280, 960), Image.ANTIALIAS)
    output = io.BytesIO()
    im.save(output, format='JPEG')
    output.seek(0)
    return InMemoryUploadedFile(
        output,
        u"pic",
        'pic',
        pic.content_type,
        len(output.getbuffer()),
        None) 
Example #16
Source File: test_protocol.py    From dissemin with GNU Affero General Public License v3.0 6 votes vote down vote up
def setUpForProtocol(self, protocol_class, repository):

        self.oaisource, _ = OaiSource.objects.get_or_create(
            identifier='deposit_oaisource',
            name='Repository OAI source',
            default_pubtype='preprint')
        logo = InMemoryUploadedFile(
                BytesIO(simple_png_image),
                None, 'logo.png',
                'image/png', len(simple_png_image), None, None)
        self.repo = repository
        self.repo.oaisource = self.oaisource
        self.repo.logo = logo
        if not self.repo.description:
            self.repo.description = 'brsuatiercs'
        if not self.repo.name:
            self.repo.name = 'Test Repository'
        self.repo.protocol = protocol_class.__name__
        self.repo.save()
        protocol_registry.register(protocol_class)
        self.proto = protocol_class(self.repo)
        self.form = None 
Example #17
Source File: models.py    From django-favicon-plus with MIT License 6 votes vote down vote up
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 #18
Source File: biz.py    From meeting with GNU General Public License v3.0 6 votes vote down vote up
def get_wxa_code_unlimited_file(file_name, scene, **kwargs):
    file = BytesIO()
    kw = dict()
    for k in ('width', 'auto_color', 'line_color', 'page', 'is_hyaline'):
        if k in kwargs:
            kw[k] = kwargs[k]
    content = wechat.wxa.get_wxa_code_unlimited(scene, **kw)
    file.write(content.content)
    file.seek(0)
    return InMemoryUploadedFile(
        file=file,
        field_name="",
        name=file_name,
        content_type="image/jpeg",
        size=0,
        charset="",
        content_type_extra=""
    ) 
Example #19
Source File: meta_data.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def create_media(media):
    """Download media link"""
    if is_valid_url(media.data_value):
        filename = media.data_value.split('/')[-1]
        data_file = NamedTemporaryFile()
        content_type = mimetypes.guess_type(filename)
        with closing(requests.get(media.data_value, stream=True)) as r:
            for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
                if chunk:
                    data_file.write(chunk)
        data_file.seek(os.SEEK_SET, os.SEEK_END)
        size = os.path.getsize(data_file.name)
        data_file.seek(os.SEEK_SET)
        media.data_value = filename
        media.data_file = InMemoryUploadedFile(
            data_file, 'data_file', filename, content_type,
            size, charset=None)

        return media

    return None 
Example #20
Source File: test_uploaders_standard.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        self.file_id = _get_file_id()
        self.upload_id = _get_file_id()
        self.file_name = 'my_uploaded_file.txt'
        self.request = MagicMock(spec=Request)
        self.request.user = AnonymousUser()
        file_obj = MagicMock(spec=InMemoryUploadedFile)
        file_obj.name = self.file_name
        self.request.data = {'filepond': file_obj}
        self.uploader = FilepondStandardFileUploader() 
Example #21
Source File: test_event_views.py    From coding-events with MIT License 5 votes vote down vote up
def test_create_event_with_image(admin_user, admin_client, db):
    with open(local(__file__).dirname + '/../../static/img/team/alja.jpg') as fp:
        io = StringIO.StringIO()
        io.write(fp.read())
        uploaded_picture = InMemoryUploadedFile(
            io, None, "alja.jpg", "jpeg", io.len, None)
        uploaded_picture.seek(0)

    event_data = {
        'audience': [4, 5],
        'theme': [1, 2],
        'contact_person': u'test@example.com',
        'country': u'SI',
        'description': u'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\r\nquis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\r\nconsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\r\ncillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\r\nproident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
        'event_url': u'',
        'location': u'Ljubljana, Slovenia',
        'organizer': u'Mozilla Slovenija',
        'picture': uploaded_picture,
        'start_date': datetime.datetime.now(),
        'end_date': datetime.datetime.now() + datetime.timedelta(days=3, hours=3),
        'tags': [u'css', u'html', u'web'],
        'title': u'Webmaker Ljubljana',
        'user_email': u'test@example.com'
    }

    response = admin_client.post(reverse('web.add_event'), event_data)

    assert response.status_code == 302

    response = admin_client.get(response.url)
    assert 'event_picture/alja' in response.content 
Example #22
Source File: test_bootsource.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_creates_boot_source_object_with_keyring_data(self):
        in_mem_file = InMemoryUploadedFile(
            BytesIO(sample_binary_data),
            name=factory.make_name("name"),
            field_name=factory.make_name("field-name"),
            content_type="application/octet-stream",
            size=len(sample_binary_data),
            charset=None,
        )
        params = {"url": "http://example.com/"}
        form = BootSourceForm(data=params, files={"keyring_data": in_mem_file})
        self.assertTrue(form.is_valid(), form._errors)
        boot_source = form.save()
        self.assertEqual(sample_binary_data, bytes(boot_source.keyring_data))
        self.assertAttributes(boot_source, params) 
Example #23
Source File: media.py    From coding-events with MIT License 5 votes vote down vote up
def process_image(image_file):
    """
    resize an uploaded image and convert to png format
    """
    size = 256, 512
    image_name = image_file.name
    image_basename, image_format = os.path.splitext(image_name)
    new_image_name = "%s_%s.png" % (slugify(image_basename), uuid.uuid4())

    try:
        im = PilImage.open(image_file)
        if max(im.size) > max(size):
            im.thumbnail(size, PilImage.ANTIALIAS)

        thumb_io = StringIO.StringIO()
        im.save(thumb_io, format='png')
        return InMemoryUploadedFile(
            thumb_io,
            None,
            new_image_name,
            'image/png',
            thumb_io.len,
            None)

    except IOError as e:
        msg = 'Failed while processing image (image_file=%s, image_name=%s, error_number=%s, error=%s).' \
              % (image_file, new_image_name, e.errno, e.strerror, )
        raise UploadImageError(msg) 
Example #24
Source File: image_strip.py    From iguana with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def create_img(in_memory_img, format_str, suffix_str, content_type):
    # remove any possible suffixes to avoid possible confusion
    img_name = in_memory_img.name.partition(".")[0]
    img = Image.open(in_memory_img)
    img_io_bytes = io.BytesIO()
    # img.save(img_io_bytes, format=format_str)
    # new_img = InMemoryUploadedFile(img_io_bytes, None, img_name+suffix_str, content_type,
    #                                img_io_bytes.getbuffer().nbytes, None)
    # store the image always as jpeg
    # transform the alpha channel to white
    if img.mode in ('RGBA', 'LA'):
        img_wo_alpha = Image.new(img.mode[:-1], img.size, '#ffffff')
        img_wo_alpha.paste(img, img.split()[-1])
        # TODO should img get closed?
        img = img_wo_alpha

    # TODO I'm not sure yet whether this sanitizes the image too
    img.convert('RGB').save(img_io_bytes, format="JPEG")
    new_img = InMemoryUploadedFile(img_io_bytes, None, img_name+".jpg", "image/jpeg",
                                   img_io_bytes.getbuffer().nbytes, None)
    new_img.seek(0)
    img.close()
    in_memory_img.close()
    return new_img


# It is necessary to strip any meta information before the image is stored for the first time. In many other
# approaches those information are striped after the image has been stored. Hence a leak would be possible for a short
# amount of time. Therefore InMemory representation is used to prevent any leaks.
# \param in_memory_file the file that has been uploaded
# \param has_to_be_an_image bool that signalises if the file has been uploaded via the CustomImageField
#        or if it is a general attachment 
Example #25
Source File: test_events_processors.py    From coding-events with MIT License 5 votes vote down vote up
def test_create_event_from_dict_with_all_fields(self):
        with open(local(__file__).dirname + '/../../static/img/team/alja.jpg') as fp:
            io = StringIO.StringIO()
            io.write(fp.read())
            uploaded_picture = InMemoryUploadedFile(
                io, None, "alja.jpg", "jpeg", io.len, None)
            uploaded_picture.seek(0)

        event_data = {
            "end_date": datetime.datetime.now(),
            "start_date": datetime.datetime.now(),
            "organizer": "some organizer",
            "creator": User.objects.filter(pk=1)[0],
            "title": "event title",
            "pub_date": datetime.datetime.now(),
            "country": "SI",
            "geoposition": Geoposition(46.05528, 14.51444),
            "location": "Ljubljana",
            "audience": [1],
            "theme": [1],
            "tags": ["tag1", "tag2"],
            "picture": uploaded_picture
        }

        test_event = create_or_update_event(**event_data)
        self.assertEqual(2, test_event.pk)
        self.assertEqual("Ljubljana", test_event.location)
        self.assertEqual("46.05528", str(test_event.geoposition.latitude))
        self.assertIn("tag1", test_event.tags.names())
        self.assertIn("tag2", test_event.tags.names())

        assert 'event_picture/alja' in test_event.picture.path 
Example #26
Source File: test_save.py    From django-selectel-storage with MIT License 5 votes vote down vote up
def in_memory_file():
    def inner(filename, content):
        return uploadedfile.InMemoryUploadedFile(
            file=io.BytesIO(content),
            field_name='test_field',
            name='_save_new_file.txt',
            content_type='text/plain',
            size=0,
            charset='utf8'
        )
    return inner 
Example #27
Source File: test_uploaders_base.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_get_uploader_post_req_std(self):
        file_obj = MagicMock(spec=InMemoryUploadedFile)
        self.request.method = 'POST'
        self.request.data = {'filepond': file_obj}
        uploader = FilepondFileUploader.get_uploader(self.request)
        self.assertIsInstance(uploader, FilepondStandardFileUploader,
                              'Expected a FilepondStandardFileUploader but '
                              'the returned uploader is of a different type.') 
Example #28
Source File: test_views_api.py    From cadasta-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
def _make_form_file(self, content):
        return InMemoryUploadedFile(
            io.StringIO(content),
            field_name='test_standard_questionnaire',
            name='test_standard_questionnaire.xml',
            content_type='text/xml',
            size=len(content),
            charset='utf-8',
        ) 
Example #29
Source File: views.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def head(self, request):
        LOG.debug('Filepond API: Fetch view HEAD called...')
        result = self._process_request(request)
        if isinstance(result, tuple):
            buf, file_id, upload_file_name, content_type = result
        elif isinstance(result, Response):
            return result
        else:
            raise ValueError('process_request result is of an unexpected type')

        file_size = buf.seek(0, os.SEEK_END)
        buf.seek(0)

        # The addressing of filepond issue #154
        # (https://github.com/pqina/filepond/issues/154) means that fetch
        # can now store a file downloaded from a remote URL and return file
        # metadata in the header if a HEAD request is received. If we get a
        # GET request then the standard approach of proxying the file back
        # to the client is used.
        upload_id = _get_file_id()
        memfile = InMemoryUploadedFile(buf, None, file_id, content_type,
                                       file_size, None)
        tu = TemporaryUpload(upload_id=upload_id, file_id=file_id,
                             file=memfile, upload_name=upload_file_name,
                             upload_type=TemporaryUpload.URL,
                             uploaded_by=_get_user(request))
        tu.save()

        response = Response(status=status.HTTP_200_OK)
        response['Content-Type'] = content_type
        response['Content-Length'] = file_size
        response['X-Content-Transfer-Id'] = upload_id
        response['Content-Disposition'] = ('inline; filename=%s' %
                                           upload_file_name)
        return response 
Example #30
Source File: briefcase_client.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def django_file(file_obj, field_name, content_type):
    return InMemoryUploadedFile(
        file=file_obj,
        field_name=field_name,
        name=file_obj.name,
        content_type=content_type,
        size=file_obj.size,
        charset=None
    )