Python django.core.files.storage.get_storage_class() Examples

The following are 30 code examples of django.core.files.storage.get_storage_class(). 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 , or try the search function .
Example #1
Source File: test_exports.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_graceful_exit_on_export_delete_if_file_doesnt_exist(self):
        self._publish_transportation_form()
        self._submit_transport_instance()
        export = generate_export(Export.XLS_EXPORT, 'xls', self.user.username,
                                 self.xform.id_string)
        storage = get_storage_class()()
        # delete file
        storage.delete(export.filepath)
        self.assertFalse(storage.exists(export.filepath))
        # clear filename, like it would be in an incomplete export
        export.filename = None
        export.filedir = None
        export.save()
        # delete export record, which should try to delete file as well
        delete_url = reverse(delete_export, kwargs={
            'username': self.user.username,
            'id_string': self.xform.id_string,
            'export_type': 'xls'
        })
        post_data = {'export_id': export.id}
        response = self.client.post(delete_url, post_data)
        self.assertEqual(response.status_code, 302) 
Example #2
Source File: middleware.py    From django-cloudflare-push with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def storage_factory(collector):
    class DebugConfiguredStorage(LazyObject):
        def _setup(self):
            configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)

            class DebugStaticFilesStorage(configured_storage_cls):

                def __init__(self, collector, *args, **kwargs):
                    super(DebugStaticFilesStorage, self).__init__(*args, **kwargs)
                    self.collector = collector

                def url(self, path):
                    self.collector.collect(path)
                    return super(DebugStaticFilesStorage, self).url(path)

            self._wrapped = DebugStaticFilesStorage(collector)
    return DebugConfiguredStorage 
Example #3
Source File: mediabackup.py    From django-dbbackup with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle(self, **options):
        self.verbosity = options.get('verbosity')
        self.quiet = options.get('quiet')
        self._set_logger_level()

        self.encrypt = options.get('encrypt', False)
        self.compress = options.get('compress', False)
        self.servername = options.get('servername')

        self.filename = options.get('output_filename')
        self.path = options.get('output_path')
        try:
            self.media_storage = get_storage_class()()
            self.storage = get_storage()
            self.backup_mediafiles()
            if options.get('clean'):
                self._cleanup_old_backups(servername=self.servername)

        except StorageError as err:
            raise CommandError(err) 
Example #4
Source File: mediarestore.py    From django-dbbackup with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle(self, *args, **options):
        """Django command handler."""
        self.verbosity = int(options.get('verbosity'))
        self.quiet = options.get('quiet')
        self._set_logger_level()

        self.servername = options.get('servername')
        self.decrypt = options.get('decrypt')
        self.uncompress = options.get('uncompress')

        self.filename = options.get('input_filename')
        self.path = options.get('input_path')

        self.replace = options.get('replace')
        self.passphrase = options.get('passphrase')
        self.interactive = options.get('interactive')

        self.storage = get_storage()
        self.media_storage = get_storage_class()()
        self._restore_backup() 
Example #5
Source File: storage.py    From django-dbbackup with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, storage_path=None, **options):
        """
        Initialize a Django Storage instance with given options.

        :param storage_path: Path to a Django Storage class with dot style
                             If ``None``, ``settings.DBBACKUP_STORAGE`` will
                             be used.
        :type storage_path: str
        """
        self._storage_path = storage_path or settings.STORAGE
        options = options.copy()
        options.update(settings.STORAGE_OPTIONS)
        options = dict([(key.lower(), value) for key, value in options.items()])
        self.storageCls = get_storage_class(self._storage_path)
        self.storage = self.storageCls(**options)
        self.name = self.storageCls.__name__ 
Example #6
Source File: wagtail_hooks.py    From wagtail-torchbox with MIT License 6 votes vote down vote up
def serve_document_from_s3(document, request):
    # Skip this hook if not using django-storages boto3 backend.
    if not issubclass(get_storage_class(), S3Boto3Storage):
        return

    # Send document_served signal.
    document_served.send(sender=get_document_model(), instance=document,
                         request=request)

    # Get direct S3 link.
    file_url = document.file.url

    # Generate redirect response and add never_cache headers.
    response = redirect(file_url)
    del response['Cache-control']
    add_never_cache_headers(response)
    return response 
Example #7
Source File: tools.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_media_file_response(metadata):
    if metadata.data_file:
        file_path = metadata.data_file.name
        filename, extension = os.path.splitext(file_path.split('/')[-1])
        extension = extension.strip('.')
        dfs = get_storage_class()()

        if dfs.exists(file_path):
            response = response_with_mimetype_and_name(
                metadata.data_file_type,
                filename, extension=extension, show_date=False,
                file_path=file_path, full_mime=True)

            return response
        else:
            return HttpResponseNotFound()
    else:
        return HttpResponseRedirect(metadata.data_value) 
Example #8
Source File: test_csv_export.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_dotted_fields_csv_export_output(self):
        path = os.path.join(os.path.dirname(__file__), 'fixtures', 'userone',
                            'userone_with_dot_name_fields.xls')
        self._publish_xls_file_and_set_xform(path)
        path = os.path.join(os.path.dirname(__file__), 'fixtures', 'userone',
                            'userone_with_dot_name_fields.xml')
        self._make_submission(
            path, forced_submission_time=self._submission_time)
        # test csv
        export = generate_export(Export.CSV_EXPORT, 'csv', self.user.username,
                                 'userone')
        storage = get_storage_class()()
        self.assertTrue(storage.exists(export.filepath))
        path, ext = os.path.splitext(export.filename)
        self.assertEqual(ext, '.csv')
        with open(os.path.join(
                os.path.dirname(__file__), 'fixtures', 'userone',
                'userone_with_dot_name_fields.csv')) as f1:
            with storage.open(export.filepath) as f2:
                expected_content = f1.read()
                actual_content = f2.read()
                self.assertEquals(actual_content, expected_content) 
Example #9
Source File: utils.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def image_urls_dict(instance):
    default_storage = get_storage_class()()
    urls = dict()
    suffix = settings.THUMB_CONF['medium']['suffix']
    for a in instance.attachments.all():
        filename = a.media_file.name
        if default_storage.exists(get_path(a.media_file.name, suffix)):
            url = default_storage.url(
                get_path(a.media_file.name, suffix))
        else:
            url = a.media_file.url
        file_basename = os.path.basename(filename)
        if url.startswith('/'):
            url = settings.KOBOCAT_URL + url
        urls[file_basename] = url
    return urls 
Example #10
Source File: test_csv_export.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_csv_export_output(self):
        path = os.path.join(self.fixture_dir, 'tutorial_w_repeats.xls')
        self._publish_xls_file_and_set_xform(path)
        path = os.path.join(self.fixture_dir, 'tutorial_w_repeats.xml')
        self._make_submission(
            path, forced_submission_time=self._submission_time)
        # test csv
        export = generate_export(Export.CSV_EXPORT, 'csv', self.user.username,
                                 'tutorial_w_repeats')
        storage = get_storage_class()()
        self.assertTrue(storage.exists(export.filepath))
        path, ext = os.path.splitext(export.filename)
        self.assertEqual(ext, '.csv')
        with open(os.path.join(
                self.fixture_dir, 'tutorial_w_repeats.csv')) as f1:
            with storage.open(export.filepath) as f2:
                expected_content = f1.read()
                actual_content = f2.read()
                self.assertEquals(actual_content, expected_content) 
Example #11
Source File: djanguistorage.py    From django-djangui with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class('django.core.files.storage.FileSystemStorage')() 
Example #12
Source File: storage.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)() 
Example #13
Source File: model_helper.py    From cadasta-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
def create_resource(self, data, user, project, content_object=None):
        Storage = get_storage_class()
        file = data.file.read()
        try:
            if file == b'':
                Resource.objects.get(
                    name=data.name,
                    contributor=user,
                    mime_type=data.content_type,
                    project=project,
                    original_file=data.name
                ).content_objects.create(
                    content_object=content_object
                )
            else:
                url = Storage().save('resources/' + data.name, file)
                Resource.objects.create(
                    name=data.name,
                    file=url,
                    content_object=content_object,
                    mime_type=data.content_type,
                    contributor=user,
                    project=project,
                    original_file=data.name
                ).full_clean()
        except Exception as e:
            raise InvalidXMLSubmission(_("{}".format(e))) 
Example #14
Source File: configuration.py    From jet-bridge with MIT License 5 votes vote down vote up
def __init__(self):
        models = apps.get_models()
        self.model_classes = dict(map(lambda x: (x._meta.db_table, x), models))
        self.models = dict(map(lambda x: (self.model_key(x), self.serialize_model(x)), models))

        for model in models:
            for related_model in self.get_related_models(model):
                if self.model_key(related_model) in self.models:
                    continue
                self.models[self.model_key(related_model)] = self.serialize_model(related_model)

        self.media_storage = get_storage_class(settings.JET_MEDIA_FILE_STORAGE)() 
Example #15
Source File: storage.py    From bioforum with MIT License 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)() 
Example #16
Source File: __init__.py    From django-more with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def make_storage(storage_name):
    conf = settings.STORAGES.get(storage_name)
    with suppress(ImportError, KeyError):
        klass = get_storage_class(conf.pop("class"))
        return type(storage_name, (klass, ), conf)
    raise ImproperlyConfigured("Storage '{sn}' is not correctly declared".format(sn=storage_name)) 
Example #17
Source File: test_exports.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_generate_csv_zip_export(self):
        # publish xls form
        self._publish_transportation_form_and_submit_instance()
        # create export db object
        export = generate_export(
            Export.CSV_ZIP_EXPORT, "zip", self.user.username,
            self.xform.id_string, group_delimiter='/',
            split_select_multiples=True)
        storage = get_storage_class()()
        self.assertTrue(storage.exists(export.filepath))
        path, ext = os.path.splitext(export.filename)
        self.assertEqual(ext, '.zip') 
Example #18
Source File: test_exports.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _get_xls_data(self, filepath):
        storage = get_storage_class()()
        with storage.open(filepath) as f:
            workbook = open_workbook(file_contents=f.read())
        transportation_sheet = workbook.sheet_by_name("transportation")
        self.assertTrue(transportation_sheet.nrows > 1)
        headers = transportation_sheet.row_values(0)
        column1 = transportation_sheet.row_values(1)
        return dict(zip(headers, column1)) 
Example #19
Source File: test_exports.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _get_csv_data(self, filepath):
        storage = get_storage_class()()
        csv_file = storage.open(filepath)
        reader = csv.DictReader(csv_file)
        data = reader.next()
        csv_file.close()
        return data 
Example #20
Source File: storage.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)() 
Example #21
Source File: export.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def full_filepath(self):
        if self.filepath:
            default_storage = get_storage_class()()
            try:
                return default_storage.path(self.filepath)
            except NotImplementedError:
                # read file from s3
                name, ext = os.path.splitext(self.filepath)
                tmp = NamedTemporaryFile(suffix=ext, delete=False)
                f = default_storage.open(self.filepath)
                tmp.write(f.read())
                tmp.close()
                return tmp.name
        return None 
Example #22
Source File: views.py    From kpi with GNU Affero General Public License v3.0 5 votes vote down vote up
def retrieve_user_report(request, base_filename):
    filename = _base_filename_to_full_filename(
        base_filename, request.user.username)
    default_storage = get_storage_class()()
    if not default_storage.exists(filename):
        raise Http404
    # File is intentionally left open so it can be read by the streaming
    # response
    f = default_storage.open(filename)
    response = StreamingHttpResponse(f, content_type='text/csv')
    response['Content-Disposition'] = 'attachment;filename="{}"'.format(
        base_filename)
    return response 
Example #23
Source File: storage.py    From python2017 with MIT License 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)() 
Example #24
Source File: wooeystorage.py    From Wooey with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        if os.environ.get('TESTING', False):
            from .tests import config
            kwargs['location'] = config.WOOEY_TEST_REMOTE_STORAGE_DIR
        super(CachedS3Boto3Storage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class('django.core.files.storage.FileSystemStorage')() 
Example #25
Source File: wooeystorage.py    From Wooey with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        from .tests import config
        kwargs['location'] = config.WOOEY_TEST_REMOTE_STORAGE_PATH
        super(FakeRemoteStorage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class('django.core.files.storage.FileSystemStorage')() 
Example #26
Source File: test_mediabackup.py    From django-dbbackup with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        HANDLED_FILES.clean()
        self.command = DbbackupCommand()
        self.command.servername = 'foo-server'
        self.command.storage = get_storage()
        self.command.stdout = DEV_NULL
        self.command.compress = False
        self.command.encrypt = False
        self.command.path = None
        self.command.media_storage = get_storage_class()()
        self.command.filename = None 
Example #27
Source File: storage.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.COMPRESS_STORAGE)() 
Example #28
Source File: uploads.py    From djangocms-forms with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.DJANGOCMS_FORMS_FILE_STORAGE)() 
Example #29
Source File: storage.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def _setup(self):
        self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)() 
Example #30
Source File: viewer_tools.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def image_urls(instance):
    default_storage = get_storage_class()()
    urls = []
    suffix = settings.THUMB_CONF['medium']['suffix']
    for a in instance.attachments.all():
        if default_storage.exists(get_path(a.media_file.name, suffix)):
            url = default_storage.url(
                get_path(a.media_file.name, suffix))
        else:
            url = a.media_file.url
        urls.append(url)
    return urls