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

The following are 5 code examples of django.core.files.storage.Storage(). 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_models.py    From edx-enterprise with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_logo_path(self, file_exists, delete_called):
        """
        Test that the path of image file should beenterprise/branding/<model.id>/<model_id>_logo.<ext>.lower().

        Additionally, test that the correct backend actions are taken in regards to deleting existing data.
        """
        file_mock = self._make_file_mock()
        branding_config = EnterpriseCustomerBrandingConfiguration(
            id=1,
            enterprise_customer=factories.EnterpriseCustomerFactory(),
            logo=file_mock
        )

        storage_mock = mock.MagicMock(spec=Storage, name="StorageMock")
        storage_mock.exists.return_value = file_exists
        with mock.patch("django.core.files.storage.default_storage._wrapped", storage_mock):
            path = logo_path(branding_config, branding_config.logo.name)
            self.assertEqual(path, "enterprise/branding/1/1_logo.png")
            assert storage_mock.delete.call_count == (1 if delete_called else 0)
            if delete_called:
                storage_mock.delete.assert_called_once_with('enterprise/branding/1/1_logo.png') 
Example #2
Source File: test_models.py    From edx-enterprise with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_branding_configuration_saving_successfully(self):
        """
        Test enterprise customer branding configuration saving successfully.
        """
        storage_mock = mock.MagicMock(spec=Storage, name="StorageMock")
        branding_config_1 = EnterpriseCustomerBrandingConfiguration(
            enterprise_customer=factories.EnterpriseCustomerFactory(),
            logo="test1.png"
        )

        storage_mock.exists.return_value = True
        with mock.patch("django.core.files.storage.default_storage._wrapped", storage_mock):
            branding_config_1.save()
            self.assertEqual(EnterpriseCustomerBrandingConfiguration.objects.count(), 1)

        branding_config_2 = EnterpriseCustomerBrandingConfiguration(
            enterprise_customer=factories.EnterpriseCustomerFactory(),
            logo="test2.png"
        )

        storage_mock.exists.return_value = False
        with mock.patch("django.core.files.storage.default_storage._wrapped", storage_mock):
            branding_config_2.save()
            self.assertEqual(EnterpriseCustomerBrandingConfiguration.objects.count(), 2) 
Example #3
Source File: filebrowser_webdav_file_storage.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def path(self, name):
        """
        Return a local filesystem path where the file can be retrieved using
        Python's built-in open() function. Storage systems that can't be
        accessed using open() should *not* implement this method.
        """
        # FIXME: this would be useful with self.location != ''
        # in this case use this notation:
        # 1. define self.location in __init__
        # 2. rewrite path() method to be like
        # return os.oath.join(self.location, name)
        # 3. everywhere in other sel.methods use self.path(name) instead of name attr
        return name 
Example #4
Source File: dummy_external_storage.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def path(self, name):
        # Overridden to give it the behaviour of the base Storage class
        # This is what an external storage backend would have
        raise NotImplementedError("This backend doesn't support absolute paths.") 
Example #5
Source File: storage.py    From ecommerce_website_development with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _save(self, name, content):
        """保存文件时调用"""
        # name: 上传文件的名称 a.txt
        # content: File类的对象,包含了上传文件的内容

        # 上传文件到FDFS文件存储系统
        # client = Fdfs_client('客户配置文件路径')
        # client = Fdfs_client(os.path.join(settings.BASE_DIR, 'utils/fdfs/client.conf'))
        client = Fdfs_client(self.client_conf)


        # 获取上传文件内容
        file_content = content.read()

        # 上传文件
        # {
        #     'Group name': group_name,
        #     'Remote file_id': remote_file_id, # 保存的文件id
        #     'Status': 'Upload successed.', # 上传是否成功
        #     'Local file name': '',
        #     'Uploaded size': upload_size,
        #     'Storage IP': storage_ip
        # } if success else None

        response = client.upload_by_buffer(file_content)

        if response is None or response.get('Status') != 'Upload successed.':
            # 上传失败
            raise Exception('上传文件到fast dfs系统失败')

        # 获取保存文件id
        file_id = response.get('Remote file_id')

        # 返回file_id
        return file_id