def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
python类FileSystemStorage()的实例源码
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
def __init__(self, location=None, base_url=None, file_permissions_mode=None,
directory_permissions_mode=None):
self._file_path = ''
self._file_name = ''
if os.path.isfile(location):
# Separate the path from the filename
self._file_path, self._file_name = os.path.split(location)
else:
# This class won't work if `location` doesn't point to an actual file
raise IOError('Path should point to an existing file')
# Pass in the folder so this acts like a typical FileSystemStorage that needs a path
super().__init__(
self._file_path,
base_url,
file_permissions_mode,
directory_permissions_mode
)
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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
def handle(self, *args, **options):
locale = options['locale']
location = self.get_location()
file = 'choices-{}.js'.format(locale)
fs = FileSystemStorage(location=location)
if fs.exists(file):
fs.delete(file)
content = generate_js(locale)
fs.save(file, ContentFile(content))
if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_choices']:
self.stdout.write('{0} file written to {1}'.format(file, location))
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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
def test_iiif_image_api_storage(self, _getter):
# Enable file storage engine for this test
from icekit.plugins.iiif import views
views.iiif_storage = FileSystemStorage(location=tempfile.gettempdir())
# Generate image at 10% size, manually specified
canonical_url = reverse(
'iiif_image_api',
args=[self.ik_image.pk,
'full', '20,', '0', 'default', 'jpg'])
image = self.mock_image(return_from=_getter)
response = self.app.get(canonical_url, user=self.superuser)
self.assertEqual(image.mock_calls, [
call.resize((20, 30)),
call.resize().convert('RGB'),
call.resize().convert().save(ANY, format='jpeg')
])
self.assertEqual(200, response.status_code)
self.FileResponse.assert_called_with(
ANY, content_type='image/jpeg')
# Generate image at 10% size using pct:10, confirm we get redirected
# to the expected canonical URL path and that image loaded from storage
# instead of generated
image = self.mock_image(return_from=_getter)
response = self.app.get(
reverse(
'iiif_image_api',
args=[self.ik_image.pk,
'full', 'pct:10', '0', 'default', 'jpg']),
user=self.superuser,
)
# Redirected to canonical URL?
self.assertEqual(302, response.status_code)
self.assertTrue(response['location'].endswith(canonical_url))
response = response.follow()
self.assertEqual(200, response.status_code)
self.assertEqual(image.mock_calls, []) # No calls on image
self.FileResponse.assert_called_with(
ANY, content_type='image/jpeg')
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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
def list(self, ignore_patterns):
result = []
for prefix, path in logo_paths.items():
if not path:
continue
basedir, filename = os.path.split(path)
storage = FileSystemStorage(location=basedir)
storage.prefix = prefix
result.append((filename, storage))
return result
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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
def is_local_storage(self):
return isinstance(self.storage, FileSystemStorage)
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