本文整理匯總了Python中django.core.files.storage.Storage方法的典型用法代碼示例。如果您正苦於以下問題:Python storage.Storage方法的具體用法?Python storage.Storage怎麽用?Python storage.Storage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.core.files.storage
的用法示例。
在下文中一共展示了storage.Storage方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_logo_path
# 需要導入模塊: from django.core.files import storage [as 別名]
# 或者: from django.core.files.storage import Storage [as 別名]
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')
示例2: test_branding_configuration_saving_successfully
# 需要導入模塊: from django.core.files import storage [as 別名]
# 或者: from django.core.files.storage import Storage [as 別名]
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)
示例3: path
# 需要導入模塊: from django.core.files import storage [as 別名]
# 或者: from django.core.files.storage import Storage [as 別名]
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
示例4: path
# 需要導入模塊: from django.core.files import storage [as 別名]
# 或者: from django.core.files.storage import Storage [as 別名]
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.")
示例5: _save
# 需要導入模塊: from django.core.files import storage [as 別名]
# 或者: from django.core.files.storage import Storage [as 別名]
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