本文整理汇总了Python中django.core.files.storage.DefaultStorage.url方法的典型用法代码示例。如果您正苦于以下问题:Python DefaultStorage.url方法的具体用法?Python DefaultStorage.url怎么用?Python DefaultStorage.url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.storage.DefaultStorage
的用法示例。
在下文中一共展示了DefaultStorage.url方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_avatar_urls_uncached
# 需要导入模块: from django.core.files.storage import DefaultStorage [as 别名]
# 或者: from django.core.files.storage.DefaultStorage import url [as 别名]
def get_avatar_urls_uncached(self, user, size):
"""Return the avatar URLs for the requested user.
Args:
user (django.contrib.auth.models.User):
The user whose avatar URLs are to be fetched.
size (int):
The size (in pixels) the avatar is to be rendered at.
Returns
dict:
A dictionary containing the URLs of the user's avatars at normal-
and high-DPI.
"""
storage = DefaultStorage()
settings_manager = self._settings_manager_class(user)
configuration = \
settings_manager.configuration_for(self.avatar_service_id)
if not configuration:
return {}
return {
'1x': storage.url(configuration['file_path'])
}
示例2: save
# 需要导入模块: from django.core.files.storage import DefaultStorage [as 别名]
# 或者: from django.core.files.storage.DefaultStorage import url [as 别名]
def save(self):
"""Save the file and return the configuration.
Returns:
dict:
The avatar service configuration.
"""
storage = DefaultStorage()
file_path = self.cleaned_data["avatar_upload"].name
file_path = storage.get_valid_name(file_path)
with storage.open(file_path, "wb") as f:
f.write(self.cleaned_data["avatar_upload"].read())
return {"absolute_url": storage.url(file_path), "file_path": file_path}
示例3: get
# 需要导入模块: from django.core.files.storage import DefaultStorage [as 别名]
# 或者: from django.core.files.storage.DefaultStorage import url [as 别名]
def get(self, request, **kwargs):
entity_id = kwargs.get('entity_id')
current_object = self.get_object(entity_id)
if current_object is None and self.slugToEntityIdRedirect and getattr(request, 'version', 'v1') == 'v2':
return self.get_slug_to_entity_id_redirect(kwargs.get('entity_id', None))
elif current_object is None:
return Response(status=status.HTTP_404_NOT_FOUND)
image_prop = getattr(current_object, self.prop)
if not bool(image_prop):
return Response(status=status.HTTP_404_NOT_FOUND)
image_type = request.query_params.get('type', 'original')
if image_type not in ['original', 'png']:
raise ValidationError(u"invalid image type: {}".format(image_type))
supported_fmts = {
'square': (1, 1),
'wide': (1.91, 1)
}
image_fmt = request.query_params.get('fmt', 'square').lower()
if image_fmt not in supported_fmts.keys():
raise ValidationError(u"invalid image format: {}".format(image_fmt))
image_url = image_prop.url
filename, ext = os.path.splitext(image_prop.name)
basename = os.path.basename(filename)
dirname = os.path.dirname(filename)
version_suffix = getattr(settings, 'CAIROSVG_VERSION_SUFFIX', '1')
new_name = '{dirname}/converted{version}/{basename}{fmt_suffix}.png'.format(
dirname=dirname,
basename=basename,
version=version_suffix,
fmt_suffix="-{}".format(image_fmt) if image_fmt != 'square' else ""
)
storage = DefaultStorage()
def _fit_to_height(img, ar, height=400):
img.thumbnail((height,height))
new_size = (int(ar[0]*height), int(ar[1]*height))
new_img = Image.new("RGBA", new_size)
new_img.paste(img, ((new_size[0] - height)/2, (new_size[1] - height)/2))
new_img.show()
return new_img
if image_type == 'original' and image_fmt == 'square':
image_url = image_prop.url
elif ext == '.svg':
if not storage.exists(new_name):
with storage.open(image_prop.name, 'rb') as input_svg:
svg_buf = StringIO.StringIO()
out_buf = StringIO.StringIO()
cairosvg.svg2png(file_obj=input_svg, write_to=svg_buf)
img = Image.open(svg_buf)
img = _fit_to_height(img, supported_fmts[image_fmt])
img.save(out_buf, format='png')
storage.save(new_name, out_buf)
image_url = storage.url(new_name)
else:
if not storage.exists(new_name):
with storage.open(image_prop.name, 'rb') as input_svg:
out_buf = StringIO.StringIO()
img = Image.open(input_svg)
img = _fit_to_height(img, supported_fmts[image_fmt])
img.save(out_buf, format='png')
storage.save(new_name, out_buf)
image_url = storage.url(new_name)
return redirect(image_url)