本文整理汇总了Python中django.template.defaultfilters.filesizeformat方法的典型用法代码示例。如果您正苦于以下问题:Python defaultfilters.filesizeformat方法的具体用法?Python defaultfilters.filesizeformat怎么用?Python defaultfilters.filesizeformat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.defaultfilters
的用法示例。
在下文中一共展示了defaultfilters.filesizeformat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def clean(self, *args, **kwargs):
data = super(PrivateFileField, self).clean(*args, **kwargs)
file = data.file
if isinstance(file, UploadedFile):
# content_type is only available for uploaded files,
# and not for files which are already stored in the model.
content_type = file.content_type
if self.content_types and content_type not in self.content_types:
logger.debug('Rejected uploaded file type: %s', content_type)
raise ValidationError(self.error_messages['invalid_file_type'])
if self.max_file_size and file.size > self.max_file_size:
raise ValidationError(self.error_messages['file_too_large'].format(
max_size=filesizeformat(self.max_file_size),
size=filesizeformat(file.size)
))
return data
示例2: test_add_too_large_file
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def test_add_too_large_file(self):
file_content = get_test_image_file().file.getvalue()
response = self.post({
'title': "Test image",
'file': SimpleUploadedFile('test.png', file_content),
})
# Shouldn't redirect anywhere
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailimages/images/add.html')
# The form should have an error
self.assertFormError(
response, 'form', 'file',
"This file is too big ({file_size}). Maximum filesize {max_file_size}.".format(
file_size=filesizeformat(len(file_content)),
max_file_size=filesizeformat(1),
)
)
示例3: test_localized_formats
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def test_localized_formats(self):
with self.settings(USE_L10N=True), translation.override('de'):
self.assertEqual(filesizeformat(1023), '1023\xa0Bytes')
self.assertEqual(filesizeformat(1024), '1,0\xa0KB')
self.assertEqual(filesizeformat(10 * 1024), '10,0\xa0KB')
self.assertEqual(filesizeformat(1024 * 1024 - 1), '1024,0\xa0KB')
self.assertEqual(filesizeformat(1024 * 1024), '1,0\xa0MB')
self.assertEqual(filesizeformat(1024 * 1024 * 50), '50,0\xa0MB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 - 1), '1024,0\xa0MB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024), '1,0\xa0GB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024), '1,0\xa0TB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024), '1,0\xa0PB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024 * 2000), '2000,0\xa0PB')
self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0Bytes')
self.assertEqual(filesizeformat(""), '0\xa0Bytes')
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0Bytes')
示例4: check
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def check(request):
return {
'hostname': socket.gethostname(),
'ips': ips,
'cpus': psutil.cpu_count(),
'uptime': timesince(datetime.fromtimestamp(psutil.boot_time())),
'memory': {
'total': filesizeformat(psutil.virtual_memory().total),
'available': filesizeformat(psutil.virtual_memory().available),
'used': filesizeformat(psutil.virtual_memory().used),
'free': filesizeformat(psutil.virtual_memory().free),
'percent': psutil.virtual_memory().percent
},
'swap': {
'total': filesizeformat(psutil.swap_memory().total),
'used': filesizeformat(psutil.swap_memory().used),
'free': filesizeformat(psutil.swap_memory().free),
'percent': psutil.swap_memory().percent
}
}
示例5: test_add_too_large_file
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def test_add_too_large_file(self):
video_file = create_test_video_file()
response = self.post({
'title': "Test video",
'file': SimpleUploadedFile('small.mp4', video_file.read(), "video/mp4"),
})
# Shouldn't redirect anywhere
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailvideos/videos/add.html')
# The form should have an error
self.assertFormError(
response, 'form', 'file',
"This file is too big ({file_size}). Maximum filesize {max_file_size}.".format(
file_size=filesizeformat(video_file.size),
max_file_size=filesizeformat(1),
)
)
示例6: upload_size_error
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def upload_size_error(request, max_size, image=None):
subscriptions_url = reverse('subscription_list')
open_link = "<a href=\"%s\">" % subscriptions_url
close_link = "</a>"
msg = "Sorry, but this image is too large. Under your current subscription plan, the maximum allowed image size is %(max_size)s. %(open_link)sWould you like to upgrade?%(close_link)s"
messages.error(request, _(msg) % {
"max_size": filesizeformat(max_size),
"open_link": open_link,
"close_link": close_link
})
if image is not None:
return HttpResponseRedirect(image.get_absolute_url())
return HttpResponseRedirect('/upload/')
示例7: clean
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def clean(self, *args, **kwargs):
uploaded_file = super(FormBuilderFileField, self).clean(*args, **kwargs)
if not uploaded_file:
if self.required:
raise forms.ValidationError(_('This field is required.'))
return uploaded_file
if not os.path.splitext(uploaded_file.name)[1].lstrip('.').lower() in \
self.allowed_file_types:
raise forms.ValidationError(
_('Sorry, this filetype is not allowed. '
'Allowed filetype: %s') % ', '.join(self.allowed_file_types))
if uploaded_file._size > self.max_upload_size:
params = {
'max_size': filesizeformat(self.max_upload_size),
'size': filesizeformat(uploaded_file._size)
}
msg = _(
'Please keep file size under %(max_size)s. Current size is %(size)s.') % params
raise forms.ValidationError(msg)
return uploaded_file
示例8: clean_attachment
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def clean_attachment(self):
max_filesize = 10 * 1024 * 1024 # 10MB
from django.template.defaultfilters import filesizeformat
f = self.cleaned_data.get('attachment')
if f and f.size > max_filesize:
size = filesizeformat(max_filesize)
error = _('Attachment should be no larger than %s') % size
raise forms.ValidationError(error)
return f
示例9: clean_photo
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def clean_photo(self):
MAX_FILESIZE = 1*1024*1024 # 1 MB
from django.template.defaultfilters import filesizeformat
photo = self.cleaned_data.get('photo')
if photo and photo.size > MAX_FILESIZE:
size = filesizeformat(MAX_FILESIZE)
error = _('Profile picture should be no larger than %s') % size
raise forms.ValidationError(error)
return photo
示例10: filesize
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def filesize(self):
try:
return filesizeformat(int(self.size))
except ValueError:
return self.size
示例11: clean
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def clean(self, data, initial=None):
file = super(RestrictedFileField, self).clean(data, initial)
try:
content_type = file.content_type
if content_type in self.content_types:
if file._size > self.max_upload_size:
raise ValidationError(_('Please keep filesize under %s. Current filesize %s') % (
filesizeformat(self.max_upload_size), filesizeformat(file._size)))
else:
raise ValidationError(_('Filetype not supported.'))
except AttributeError:
pass
return data
示例12: clean_upl
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def clean_upl(self):
content = self.cleaned_data['upl']
logger.info(content.content_type)
if content.size > DEPOSIT_MAX_FILE_SIZE:
raise forms.ValidationError(_('File too large (%(size)s). Maximum size is %(maxsize)s.') %
{'size': filesizeformat(content._size),
'maxsize': filesizeformat(DEPOSIT_MAX_FILE_SIZE)},
code='too_large')
return content
示例13: validate_size
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def validate_size(content):
"""Validates if the size of the content in not too big."""
if content.file.size > validate_size.MAX_UPLOAD_SIZE:
message = format_lazy(
_("Please keep filesize under {limit}. Current filesize {current}"),
limit=filesizeformat(validate_size.MAX_UPLOAD_SIZE),
current=filesizeformat(content.file.size))
raise ValidationError(message, code='file-size')
示例14: file_size
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def file_size(file, article):
try:
return filesizeformat(file.get_file_size(article))
except BaseException:
return 0
示例15: check_image_file_size
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import filesizeformat [as 别名]
def check_image_file_size(self, f):
# Upload size checking can be disabled by setting max upload size to None
if self.max_upload_size is None:
return
# Check the filesize
if f.size > self.max_upload_size:
raise ValidationError(self.error_messages['file_too_large'] % (
filesizeformat(f.size),
), code='file_too_large')