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

The following are 30 code examples of django.core.files.storage.FileSystemStorage(). 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: views.py    From Gerapy with MIT License 8 votes vote down vote up
def project_upload(request):
    """
    upload project
    :param request: request object
    :return: json
    """
    if request.method == 'POST':
        file = request.FILES['file']
        file_name = file.name
        fs = FileSystemStorage(PROJECTS_FOLDER)
        zip_file_name = fs.save(file_name, file)
        logger.debug('zip file name %s', zip_file_name)
        # extract zip file
        with zipfile.ZipFile(join(PROJECTS_FOLDER, zip_file_name), 'r') as zip_ref:
            zip_ref.extractall(PROJECTS_FOLDER)
        logger.debug('extracted files to %s', PROJECTS_FOLDER)
        return JsonResponse({'status': True}) 
Example #2
Source File: ar_elections_2015_import_candidates.py    From yournextrepresentative with GNU Affero General Public License v3.0 6 votes vote down vote up
def enqueue_image(person, user, image_url):
    r = requests.get(
        image_url,
        headers={
            'User-Agent': USER_AGENT,
        },
        stream=True
    )
    if not r.status_code == 200:
        message = "HTTP status code {0} when downloading {1}"
        raise Exception(message.format(r.status_code, image_url))
    storage = FileSystemStorage()
    suggested_filename = \
        'queued_image/{d.year}/{d.month:02x}/{d.day:02x}/ci-upload'.format(
            d=date.today()
        )
    storage_filename = storage.save(suggested_filename, r.raw)
    QueuedImage.objects.create(
        why_allowed=QueuedImage.OTHER,
        justification_for_use="Downloaded from {0}".format(image_url),
        decision=QueuedImage.UNDECIDED,
        image=storage_filename,
        person_id=person.id,
        user=user
    ) 
Example #3
Source File: views.py    From django-docker-s3 with MIT License 6 votes vote down vote up
def image_upload(request):
    if request.method == 'POST':
        image_file = request.FILES['image_file']
        image_type = request.POST['image_type']
        if settings.USE_S3:
            if image_type == 'private':
                upload = UploadPrivate(file=image_file)
            else:
                upload = Upload(file=image_file)
            upload.save()
            image_url = upload.file.url
        else:
            fs = FileSystemStorage()
            filename = fs.save(image_file.name, image_file)
            image_url = fs.url(filename)
        return render(request, 'upload.html', {
            'image_url': image_url
        })
    return render(request, 'upload.html') 
Example #4
Source File: finders.py    From django-plotly-dash with MIT License 6 votes vote down vote up
def __init__(self):
        # get all registered apps

        self.locations = []
        self.storages = OrderedDict()

        self.ignore_patterns = ["*.py", "*.pyc",]

        for app_config in apps.get_app_configs():

            path_directory = os.path.join(app_config.path, 'assets')

            if os.path.isdir(path_directory):

                storage = FileSystemStorage(location=path_directory)

                storage.prefix = full_asset_path(app_config.name, "")

                self.locations.append(app_config.name)
                self.storages[app_config.name] = storage

        super(DashAppDirectoryFinder, self).__init__()

    #pylint: disable=redefined-builtin 
Example #5
Source File: views.py    From Pytition with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def image_upload(request):
    pytitionuser = get_session_user(request)

    if request.method != "POST":
        return HttpResponseForbidden()

    file = request.FILES.get('file', '')
    if file == '':
        return HttpResponseForbidden()

    storage = FileSystemStorage()
    path = os.path.join(storage.location, pytitionuser.username, file.name)
    name = storage._save(path, file)
    newrelpath = os.path.relpath(name, storage.location)

    return JsonResponse({'location': storage.base_url + newrelpath})

# /transfer_petition/<int:petition_id>
# Transfer a petition to another org or user 
Example #6
Source File: ar_elections_2015_import_candidates_csv.py    From yournextrepresentative with GNU Affero General Public License v3.0 6 votes vote down vote up
def enqueue_image(person, user, image_url):
    r = requests.get(
        image_url,
        headers={
            'User-Agent': USER_AGENT,
        },
        stream=True
    )
    if not r.status_code == 200:
        message = "HTTP status code {0} when downloading {1}"
        raise Exception(message.format(r.status_code, image_url))
    storage = FileSystemStorage()
    suggested_filename = \
        'queued_image/{d.year}/{d.month:02x}/{d.day:02x}/ci-upload'.format(
            d=date.today()
        )
    storage_filename = storage.save(suggested_filename, r.raw)
    QueuedImage.objects.create(
        why_allowed=QueuedImage.OTHER,
        justification_for_use="Downloaded from {0}".format(image_url),
        decision=QueuedImage.UNDECIDED,
        image=storage_filename,
        person_id=person.id,
        user=user
    ) 
Example #7
Source File: test_process_view.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_store_upload_with_storage_outside_BASE_DIR_with_enable(self):
        old_storage = views.storage
        old_UPLOAD_TMP = drf_filepond_settings.UPLOAD_TMP

        drf_filepond_settings.ALLOW_EXTERNAL_UPLOAD_DIR = True

        views.storage = FileSystemStorage(location='/tmp/uploads')
        drf_filepond_settings.UPLOAD_TMP = '/tmp/uploads'

        (encoded_form, content_type) = self._get_encoded_form('testfile.dat')

        rf = RequestFactory()
        req = rf.post(reverse('process'),
                      data=encoded_form, content_type=content_type)
        pv = views.ProcessView.as_view()
        response = pv(req)
        views.storage = old_storage
        drf_filepond_settings.UPLOAD_TMP = old_UPLOAD_TMP
        drf_filepond_settings.ALLOW_EXTERNAL_UPLOAD_DIR = False
        self.assertEqual(response.status_code, 200, 'Expecting upload to be '
                         'successful.') 
Example #8
Source File: test_process_view.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_store_upload_with_storage_outside_BASE_DIR_without_enable(self):
        old_storage = views.storage
        views.storage = FileSystemStorage(location='/tmp/uploads')
        (encoded_form, content_type) = self._get_encoded_form('testfile.dat')

        rf = RequestFactory()
        req = rf.post(reverse('process'),
                      data=encoded_form, content_type=content_type)
        pv = views.ProcessView.as_view()
        response = pv(req)
        views.storage = old_storage
        self.assertEqual(response.status_code, 500, 'Expecting 500 error due'
                         ' to invalid storage location.')
        self.assertEqual(
            response.data,
            'The file upload path settings are not configured correctly.',
            ('Expecting error showing path settings are configured '
             'incorrectly.')) 
Example #9
Source File: test_process_view.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_process_invalid_storage_location(self):
        old_storage = views.storage
        views.storage = FileSystemStorage(location='/django_test')
        (encoded_form, content_type) = self._get_encoded_form('testfile.dat')

        rf = RequestFactory()
        req = rf.post(reverse('process'),
                      data=encoded_form, content_type=content_type)
        pv = views.ProcessView.as_view()
        response = pv(req)
        views.storage = old_storage
        self.assertEqual(response.status_code, 500, 'Expecting 500 error due'
                         ' to invalid storage location.')
        self.assertEqual(
            response.data,
            'The file upload path settings are not configured correctly.',
            ('Expecting error showing path settings are configured '
             'incorrectly.')) 
Example #10
Source File: test_signals.py    From django-drf-filepond with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_del_tmp_upload_file(self):
        # Create a temporary file
        tmp_dir = mkdtemp(prefix='django_test_')
        self.assertTrue(os.path.exists(tmp_dir),
                        'Test temp file was not created.')
        tmp_dir_split = tmp_dir.rsplit(os.sep, 1)
        _, path = mkstemp(suffix='.txt', prefix='django_test_', text=True)
        self.assertTrue(os.path.exists(path),
                        'Test temp file was not created.')

        # Mock a TemporaryUpload instance object
        tu = Mock(spec=TemporaryUpload)
        upload_file = Mock(spec=SimpleUploadedFile)
        models.storage = Mock(spec=FileSystemStorage)
        models.storage.location = tmp_dir_split[0]

        upload_file.path = path
        tu.upload_id = tmp_dir_split[1]
        tu.file = upload_file
        delete_temp_upload_file(None, tu)
        self.assertFalse(os.path.exists(path), 'Test temp file was not '
                         'removed by the signal handler.') 
Example #11
Source File: storage.py    From adminset with GNU General Public License v2.0 5 votes vote down vote up
def _rmdir_callable(self, path, storage):
        """
        Remove directory when using a 
        `FileSystemStorage <https://docs.djangoproject.com/en/dev/ref/files/storage/#the-filesystemstorage-class>`_
        storage backend. See also the :ref:`setting-rmDir` setting.
        """
        return os.rmdir(self._join_path(storage.location, path)) 
Example #12
Source File: finders.py    From django-plotly-dash with MIT License 5 votes vote down vote up
def __init__(self):

        self.locations = []
        self.storages = OrderedDict()
        self.components = {}

        self.ignore_patterns = ["*.py", "*.pyc",]

        try:
            components = settings.PLOTLY_COMPONENTS
        except:
            components = []

        for component_name in components:

            module = importlib.import_module(component_name)
            path_directory = os.path.dirname(module.__file__)

            root = path_directory
            storage = FileSystemStorage(location=root)
            path = "dash/component/%s" % component_name

            # Path_directory is where from
            # path is desired url mount point of the content of path_directory
            # component_name is the name of the component

            storage.prefix = path

            self.locations.append(component_name)

            self.storages[component_name] = storage
            self.components[path] = component_name

        super(DashComponentFinder, self).__init__() 
Example #13
Source File: storage.py    From python2017 with MIT License 5 votes vote down vote up
def __init__(self, location=None, base_url=None, *args, **kwargs):
        if location is None:
            location = settings.STATIC_ROOT
        if base_url is None:
            base_url = settings.STATIC_URL
        check_settings(base_url)
        super(StaticFilesStorage, self).__init__(location, base_url,
                                                 *args, **kwargs)
        # FileSystemStorage fallbacks to MEDIA_ROOT when location
        # is empty, so we restore the empty value.
        if not location:
            self.base_location = None
            self.location = None 
Example #14
Source File: staticfiles.py    From cjworkbench with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.root = Path(__file__).parent
        self.storage = FileSystemStorage(location=str(self.root.resolve()))

    # override 
Example #15
Source File: views.py    From django-upload-example with MIT License 5 votes vote down vote up
def upload(request):
    context = {}
    if request.method == 'POST':
        uploaded_file = request.FILES['document']
        fs = FileSystemStorage()
        name = fs.save(uploaded_file.name, uploaded_file)
        context['url'] = fs.url(name)
    return render(request, 'upload.html', context) 
Example #16
Source File: collectstatic.py    From python2017 with MIT License 5 votes vote down vote up
def is_local_storage(self):
        return isinstance(self.storage, FileSystemStorage) 
Example #17
Source File: storage.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def __init__(self, location=None, base_url=None, *args, **kwargs):
        if location is None:
            location = settings.STATIC_ROOT
        if base_url is None:
            base_url = settings.STATIC_URL
        check_settings(base_url)
        super(StaticFilesStorage, self).__init__(location, base_url,
                                                 *args, **kwargs)
        # FileSystemStorage fallbacks to MEDIA_ROOT when location
        # is empty, so we restore the empty value.
        if not location:
            self.base_location = None
            self.location = None 
Example #18
Source File: finders.py    From django-plotly-dash with MIT License 5 votes vote down vote up
def __init__(self):

        # Ensure urls are loaded
        root_urls = settings.ROOT_URLCONF
        importlib.import_module(root_urls)

        # Get all registered django dash apps

        self.apps = all_apps()

        self.locations = []
        self.storages = OrderedDict()

        self.ignore_patterns = ["*.py", "*.pyc",]

        added_locations = {}

        for app_slug, obj in self.apps.items():

            caller_module = obj.caller_module
            location = obj.caller_module_location
            subdir = obj.assets_folder

            path_directory = os.path.join(os.path.dirname(location), subdir)

            if os.path.isdir(path_directory):

                component_name = app_slug
                storage = FileSystemStorage(location=path_directory)
                path = full_asset_path(obj.caller_module.__name__,"")
                storage.prefix = path

                self.locations.append(component_name)
                self.storages[component_name] = storage

        super(DashAssetFinder, self).__init__()

    #pylint: disable=redefined-builtin 
Example #19
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 #20
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 #21
Source File: views.py    From simple-file-upload with MIT License 5 votes vote down vote up
def simple_upload(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        return render(request, 'core/simple_upload.html', {
            'uploaded_file_url': uploaded_file_url
        })
    return render(request, 'core/simple_upload.html') 
Example #22
Source File: storage.py    From webterminal with GNU General Public License v3.0 5 votes vote down vote up
def _rmdir_callable(self, path, storage):
        """
        Remove directory when using a 
        `FileSystemStorage <https://docs.djangoproject.com/en/dev/ref/files/storage/#the-filesystemstorage-class>`_
        storage backend. See also the :ref:`setting-rmDir` setting.
        """
        return os.rmdir(self._join_path(storage.location, path)) 
Example #23
Source File: finders.py    From django-npm with MIT License 5 votes vote down vote up
def __init__(self, apps=None, *args, **kwargs):
        self.node_modules_path = get_npm_root_path()
        self.destination = getattr(settings, 'NPM_STATIC_FILES_PREFIX', '')
        self.cache_enabled = getattr(settings, 'NPM_FINDER_USE_CACHE', True)
        self.cached_list = None

        self.match_patterns = flatten_patterns(getattr(settings, 'NPM_FILE_PATTERNS', None)) or ['*']
        self.locations = [(self.destination, os.path.join(self.node_modules_path, 'node_modules'))]
        self.storages = OrderedDict()

        filesystem_storage = FileSystemStorage(location=self.locations[0][1])
        filesystem_storage.prefix = self.locations[0][0]
        self.storages[self.locations[0][1]] = filesystem_storage 
Example #24
Source File: test_finder.py    From django-npm with MIT License 5 votes vote down vote up
def test_get_files(npm_dir):
    storage = FileSystemStorage(location=str(npm_dir))
    files = get_files(storage, match_patterns='*')
    assert any([True for _ in files]) 
Example #25
Source File: static_finders.py    From tethys with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, apps=None, *args, **kwargs):
        # List of locations with static files
        self.locations = get_directories_in_tethys(('static', 'public'), with_app_name=True)

        # Maps dir paths to an appropriate storage instance
        self.storages = SortedDict()

        for prefix, root in self.locations:
            filesystem_storage = FileSystemStorage(location=root)
            filesystem_storage.prefix = prefix
            self.storages[root] = filesystem_storage

        super().__init__(*args, **kwargs) 
Example #26
Source File: dummy_external_storage.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        self.wrapped = FileSystemStorage(*args, **kwargs) 
Example #27
Source File: storage.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, location=None, base_url=None, *args, **kwargs):
        if location is None:
            location = settings.STATIC_ROOT
        if base_url is None:
            base_url = settings.STATIC_URL
        check_settings(base_url)
        super(StaticFilesStorage, self).__init__(location, base_url,
                                                 *args, **kwargs)
        # FileSystemStorage fallbacks to MEDIA_ROOT when location
        # is empty, so we restore the empty value.
        if not location:
            self.base_location = None
            self.location = None 
Example #28
Source File: collectstatic.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def is_local_storage(self):
        return isinstance(self.storage, FileSystemStorage) 
Example #29
Source File: views.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def export_download(request, username, id_string, export_type, filename):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)
    helper_auth_helper(request)
    if not has_forms_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    # find the export entry in the db
    export = get_object_or_404(Export, xform=xform, filename=filename)

    if (export_type == Export.GDOC_EXPORT or export_type == Export.EXTERNAL_EXPORT) \
            and export.export_url is not None:
        return HttpResponseRedirect(export.export_url)

    ext, mime_type = export_def_from_filename(export.filename)

    audit = {
        "xform": xform.id_string,
        "export_type": export.export_type
    }
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Downloaded %(export_type)s export '%(filename)s' "
          "on '%(id_string)s'.") %
        {
            'export_type': export.export_type.upper(),
            'filename': export.filename,
            'id_string': xform.id_string,
        }, audit, request)
    if request.GET.get('raw'):
        id_string = None

    default_storage = get_storage_class()()
    if not isinstance(default_storage, FileSystemStorage):
        return HttpResponseRedirect(default_storage.url(export.filepath))
    basename = os.path.splitext(export.filename)[0]
    response = response_with_mimetype_and_name(
        mime_type, name=basename, extension=ext,
        file_path=export.filepath, show_date=False)
    return response 
Example #30
Source File: storage.py    From bioforum with MIT License 5 votes vote down vote up
def __init__(self, location=None, base_url=None, *args, **kwargs):
        if location is None:
            location = settings.STATIC_ROOT
        if base_url is None:
            base_url = settings.STATIC_URL
        check_settings(base_url)
        super().__init__(location, base_url, *args, **kwargs)
        # FileSystemStorage fallbacks to MEDIA_ROOT when location
        # is empty, so we restore the empty value.
        if not location:
            self.base_location = None
            self.location = None