本文整理匯總了Python中filebrowser.forms.UploadFileForm.is_valid方法的典型用法代碼示例。如果您正苦於以下問題:Python UploadFileForm.is_valid方法的具體用法?Python UploadFileForm.is_valid怎麽用?Python UploadFileForm.is_valid使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類filebrowser.forms.UploadFileForm
的用法示例。
在下文中一共展示了UploadFileForm.is_valid方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _upload_file
# 需要導入模塊: from filebrowser.forms import UploadFileForm [as 別名]
# 或者: from filebrowser.forms.UploadFileForm import is_valid [as 別名]
def _upload_file(request):
"""
Handles file uploaded by HDFSfileUploadHandler.
The uploaded file is stored in HDFS at its destination with a .tmp suffix.
We just need to rename it to the destination path.
"""
form = UploadFileForm(request.POST, request.FILES)
response = {'status': -1, 'data': ''}
if request.META.get('upload_failed'):
raise PopupException(request.META.get('upload_failed'))
if form.is_valid():
uploaded_file = request.FILES['hdfs_file']
dest = form.cleaned_data['dest']
if request.fs.isdir(dest) and posixpath.sep in uploaded_file.name:
raise PopupException(_('Sorry, no "%(sep)s" in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name}))
dest = request.fs.join(dest, uploaded_file.name)
tmp_file = uploaded_file.get_temp_path()
username = request.user.username
try:
# Remove tmp suffix of the file
request.fs.do_as_user(username, request.fs.rename, tmp_file, dest)
response['status'] = 0
except IOError, ex:
already_exists = False
try:
already_exists = request.fs.exists(dest)
except Exception:
pass
if already_exists:
msg = _('Destination %(name)s already exists.') % {'name': dest}
else:
msg = _('Copy to %(name)s failed: %(error)s') % {'name': dest, 'error': ex}
raise PopupException(msg)
response.update({
'path': dest,
'result': _massage_stats(request, request.fs.stats(dest)),
'next': request.GET.get("next")
})
return response
示例2: _upload_file
# 需要導入模塊: from filebrowser.forms import UploadFileForm [as 別名]
# 或者: from filebrowser.forms.UploadFileForm import is_valid [as 別名]
def _upload_file(request):
"""
Handles file uploaded by HDFSfileUploadHandler.
The uploaded file is stored in HDFS. We just need to rename it to the destination path.
"""
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
uploaded_file = request.FILES['hdfs_file']
dest = form.cleaned_data['dest']
if request.fs.isdir(dest) and posixpath.sep in uploaded_file.name:
raise PopupException(_('Sorry, no "%(sep)s" in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name}))
dest = request.fs.join(dest, uploaded_file.name)
tmp_file = uploaded_file.get_temp_path()
username = request.user.username
try:
# Temp file is created by superuser. Chown the file.
request.fs.do_as_superuser(request.fs.chmod, tmp_file, 0644)
request.fs.do_as_superuser(request.fs.chown, tmp_file, username, username)
# Move the file to where it belongs
request.fs.rename(tmp_file, dest)
except IOError, ex:
already_exists = False
try:
already_exists = request.fs.exists(dest)
except Exception:
pass
if already_exists:
msg = _('Destination %(name)s already exists.') % {'name': dest}
else:
msg = _('Copy to %(name)s failed: %(error)s') % {'name': dest, 'error': ex}
raise PopupException(msg)
return {
'status': 0,
'path': dest,
'result': _massage_stats(request, request.fs.stats(dest)),
'next': request.GET.get("next")
}