本文整理汇总了Python中filebrowser.base.FileObject.version_generate方法的典型用法代码示例。如果您正苦于以下问题:Python FileObject.version_generate方法的具体用法?Python FileObject.version_generate怎么用?Python FileObject.version_generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类filebrowser.base.FileObject
的用法示例。
在下文中一共展示了FileObject.version_generate方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def get(self, request, *args, **kwargs):
''' Receives the request '''
ctx = {'show_profile': "0"}
if 'loggedin_user_credentials' in request.session:
ctx = request.session['loggedin_user_credentials']
try:
PhotographerObj = Photographer.objects.get(user_ref=request.user)
if len(PhotographerObj.image.all()):
formargs = {'username': request.user.username, 'home_page_desc': PhotographerObj.home_page_desc}
count = 1
for k in PhotographerObj.image.all().order_by('created_date'):
if k.profile_image:
rel_path = os.path.join(os.path.join(FILEBROWSER_DIRECTORY,request.user.username) + '/', k.image.filename)
a = FileObject(rel_path)
version = a.version_generate('thumbnail').url
formargs.update({'profile_image_name': k.image.filename,
'profile_image': version})
name = 'image_' + str(count) + '_desc'
rel_path = os.path.join(os.path.join(FILEBROWSER_DIRECTORY,request.user.username) + '/', k.image.filename)
a = FileObject(rel_path)
version = a.version_generate('thumbnail').url
formargs.update({'image_' + str(count) + '_name': k.image.filename,
'image_' + str(count): version
})
formargs.update({'image_' + str(count) + '_desc': k.image_desc})
count += 1
# formargs.update({'image_1_desc': PhotographerObj.image_1_desc,
# 'image_2_desc': PhotographerObj.image_2_desc,
# 'image_3_desc': PhotographerObj.image_3_desc,
# 'image_4_desc': PhotographerObj.image_4_desc,
# })
formsubmit = self.form_class(formargs)
ctx.update({'upload_form': formsubmit})
ctx.update({'show_profile': "1"})
except Exception as e:
ctx.update({'show_profile': "0"})
pass
ctx.update({'username': request.user.username})
return Response(ctx, template_name=self.template_name)
示例2: render
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def render(self, context):
try:
version_suffix = self.suffix.resolve(context)
source = self.src.resolve(context)
except VariableDoesNotExist:
return None
if version_suffix not in VERSIONS:
return "" # FIXME: should this throw an error?
if isinstance(source, FileObject):
source = source.path
elif isinstance(source, File):
source = source.name
else: # string
source = source
site = context.get('filebrowser_site', get_default_site())
if FORCE_PLACEHOLDER or (SHOW_PLACEHOLDER and not site.storage.isfile(source)):
source = PLACEHOLDER
fileobject = FileObject(source, site=site)
try:
version = fileobject.version_generate(version_suffix)
context[self.var_name] = version
except Exception as e:
if settings.TEMPLATE_DEBUG:
raise e
context[self.var_name] = ""
return ""
示例3: get_teaser_image
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def get_teaser_image(path, version):
if path:
image = FileObject(path)
if image.filetype == "Image":
return image.version_generate(version).url.split('adimas.info')[1]
else:
return ""
示例4: image_thumbnail
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def image_thumbnail(self):
if self.image:
image = FileObject(self.image.path)
if image.filetype == "Image":
return '<img src="%s" />' % image.version_generate('admin_thumbnail').url
else:
return ""
示例5: image_thumbnail
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def image_thumbnail(self, obj):
if obj.image:
image = FileObject(obj.image.path)
if image.filetype == "Image":
return '<img src="%s" />' % image.version_generate(ADMIN_THUMBNAIL).url
else:
return ""
示例6: test_unicode_options_namer_version
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def test_unicode_options_namer_version(self):
path_unicode = os.path.join(self.FOLDER_PATH, "測試文件.jpg")
expected = u"測試文件_large--680x0.jpg"
shutil.copy(self.STATIC_IMG_PATH, path_unicode)
f = FileObject(path_unicode, site=site)
version = f.version_generate("large")
self.assertEqual(version.filename, expected)
示例7: get_image_thumbnail_html
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def get_image_thumbnail_html(image):
if image:
image_fileobject = FileObject(image.path)
if image_fileobject and image_fileobject.filetype == "Image" \
and os.path.isfile(settings.MEDIA_ROOT + image.path):
str = '<img src="%s" />' % image_fileobject.version_generate(settings.FILEBROWSER_ADMIN_THUMBNAIL).url
return str
return False
else:
return False
示例8: save_file
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def save_file(self, file):
"""
Little helper to save a file default save to media/temp/
"""
# original_name = file.name
# extension = os.path.splitext(original_name)[1][1:]
# filename = os.path.splitext(original_name)[0].strip('.')
# filename = filename + '__' + str(time.time()) + '.' + extension
# rel_path = os.path.join(TEMP_UPLOAD_DIR, filename)
#Create user directory in temp folder
try:
os.makedirs(os.path.join(MEDIA_ROOT, TEMP_UPLOAD_DIR))
except OSError:
# Do nothing Assume that dir is already created.
pass
try:
userdirpath = os.path.join(os.path.join(MEDIA_ROOT, TEMP_UPLOAD_DIR), self.request.user.username)
os.makedirs(userdirpath)
except OSError:
# Do nothing Assume that dir is already created.
pass
source_dir = os.path.join(MEDIA_ROOT)
destination_dir = os.path.join(source_dir,os.path.join(TEMP_UPLOAD_DIR,self.request.user.username))
ext = '.' + os.path.splitext(file.name)[1][1:]
img_name = file.name.split(ext)[0]
img_name = generate_unique_file_name(destination_dir, img_name, ext)
rel_path = os.path.join(os.path.join(TEMP_UPLOAD_DIR,self.request.user.username) + '/', img_name)
fd = open(os.path.join(MEDIA_ROOT, rel_path), 'wb')
for chunk in file.chunks():
fd.write(chunk)
fd.close()
try:
a = FileObject(rel_path)
version = a.version_generate('thumbnail').url
except Exception as e:
pass
return {'path': MEDIA_URL + rel_path, 'file': img_name, 'name': img_name, 'version': version}
示例9: set_version_img_url
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def set_version_img_url(image, context, version):
if isinstance(image, FileObject):
source = image.path
elif isinstance(image, File):
source = image.name
else: # string
source = image
site = context.get('filebrowser_site', get_default_site())
if getattr(settings, 'FILEBROWSER_FORCE_PLACEHOLDER', None) \
or \
(getattr(settings, 'FILEBROWSER_SHOW_PLACEHOLDER', None) and not site.storage.isfile(source)):
source = settings.FILEBROWSER_PLACEHOLDER
fileobject = FileObject(source, site=site)
try:
version = fileobject.version_generate(version)
image = version.path
except Exception as e:
raise e
return settings.MEDIA_URL + image
示例10: generate_summary
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def generate_summary(self, nchars=200):
'''提出摘要, 最终返回以HTML片段. 代表插图 + 前N个文字'''
orig_html = pq(self.html_content)
# 优先提取带有 cover 类的图片作为封面
cover = orig_html('img.cover:first') or orig_html('img:first')
if cover:
try:
# 清楚来自图片上传插件(ckeditor)自动添加的属性
cover.removeAttr('style').removeAttr('width').removeAttr('height')
except KeyError:
pass
cover.addClass('cover')
orig_src = cover.attr('src')
# 如果是本地图片, 则封面img标签使用django-filebrowser生成的缩略图
if orig_src.startswith(settings.MEDIA_URL):
print "更新封面"
relative_path = orig_src.replace(settings.MEDIA_URL, '')
if relative_path.startswith(settings.FILEBROWSER_VERSIONS_BASEDIR):
# 如果已经引用的是FileBrowser生成的小尺寸图片,
# 则试图推导出原图路径, 并根据原图生成缩略图.
relative_path = re.sub(r'^%s' % settings.FILEBROWSER_VERSIONS_BASEDIR,
settings.FILEBROWSER_DIRECTORY, relative_path)
# FileBrowser生成图片的后缀模式:
postfix_pat = '|'.join(['_'+i for i in settings.FILEBROWSER_ADMIN_VERSIONS])
relative_path = re.sub(r'(%s)\.' % postfix_pat, '.', relative_path)
fileobject = FileObject(relative_path)
if fileobject.exists():
fileobject = fileobject.original
thumbnail = fileobject.version_generate('thumbnail')
cover.attr('src', thumbnail.url).attr('data-orig-src', orig_src)
cover.css(height='100px', width='100px')
else:
print u'引用的图片不存在: %s' % fileobject.path
summary_text = cgi.escape(orig_html.text()[:int(nchars)])
return (cover.outerHtml() or "") + \
(u'<span class="summary-text">%s...</span>' % summary_text) + \
(u'<a class="more" href="/%d/">阅读全文→</a>' % self.id) + \
u'<div class="clear"></div>'
示例11: render
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def render(self, context):
try:
version_suffix = self.suffix.resolve(context)
source = self.src.resolve(context)
except VariableDoesNotExist:
return ""
if version_suffix not in VERSIONS:
return "" # FIXME: should this throw an error?
if isinstance(source, FileObject):
source = source.path
elif isinstance(source, File):
source = source.name
else: # string
source = source
site = context.get('filebrowser_site', get_default_site())
fileobject = FileObject(source, site=site)
try:
version = fileobject.version_generate(version_suffix)
return version.url
except Exception, e:
if settings.TEMPLATE_DEBUG:
raise e
示例12: FileObjectAttributeTests
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
#.........这里部分代码省略.........
# is_version
# original
# versions_basedir
# versions
# admin_versions
# version_name(suffix)
# version_path(suffix)
# version_generate(suffix)
"""
# new settings
filebrowser.base.VERSIONS_BASEDIR = ""
filebrowser.base.VERSIONS = {
'admin_thumbnail': {'verbose_name': 'Admin Thumbnail', 'width': 60, 'height': 60, 'opts': 'crop'},
'large': {'verbose_name': 'Large', 'width': 600, 'height': '', 'opts': ''},
}
filebrowser.base.ADMIN_VERSIONS = ['large']
# expected test results
version_list = ['fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg', 'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg']
admin_version_list = ['fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg']
self.assertEqual(self.f_image.is_version, False)
self.assertEqual(self.f_image.original.path, self.f_image.path)
self.assertEqual(self.f_image.versions_basedir, "fb_test_directory/")
self.assertEqual(self.f_image.versions(), version_list)
self.assertEqual(self.f_image.admin_versions(), admin_version_list)
self.assertEqual(self.f_image.version_name("large"), "testimage_large.jpg")
self.assertEqual(self.f_image.version_path("large"), "fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg")
# version does not exist yet
f_version = FileObject(os.path.join(self.directory, self.tmpdir_name, "testimage_large.jpg"), site=site)
self.assertEqual(f_version.exists, False)
# generate version
f_version = self.f_image.version_generate("large")
self.assertEqual(f_version.path, "fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg")
self.assertEqual(f_version.exists, True)
self.assertEqual(f_version.is_version, True)
self.assertEqual(f_version.original_filename, "testimage.jpg")
self.assertEqual(f_version.original.path, self.f_image.path)
# FIXME: versions should not have versions or admin_versions
def test_version_attributes_2(self):
"""
FileObject version attributes/methods
with versions_basedir
# is_version
# original
# versions_basedir
# versions
# admin_versions
# version_name(suffix)
# version_generate(suffix)
"""
# new settings
filebrowser.base.VERSIONS_BASEDIR = "fb_test_directory/_versions"
filebrowser.base.VERSIONS = {
'admin_thumbnail': {'verbose_name': 'Admin Thumbnail', 'width': 60, 'height': 60, 'opts': 'crop'},
'large': {'verbose_name': 'Large', 'width': 600, 'height': '', 'opts': ''},
}
filebrowser.base.ADMIN_VERSIONS = ['large']
# expected test results
version_list = ['fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg', 'fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_admin_thumbnail.jpg']
admin_version_list = ['fb_test_directory/_versions/fb_tmp_dir/fb_tmp_dir_sub/testimage_large.jpg']
self.assertEqual(self.f_image.is_version, False)
示例13: _upload_file
# 需要导入模块: from filebrowser.base import FileObject [as 别名]
# 或者: from filebrowser.base.FileObject import version_generate [as 别名]
def _upload_file(self, request):
"""
Upload file to the server.
If temporary is true, we upload to UPLOAD_TEMP_DIR, otherwise
we upload to site.directory
"""
if request.method == "POST":
folder = request.GET.get('folder', '')
temporary = request.GET.get('temporary', '')
temp_filename = None
if len(request.FILES) == 0:
return HttpResponseBadRequest('Invalid request! No files included.')
if len(request.FILES) > 1:
return HttpResponseBadRequest('Invalid request! Multiple files included.')
filedata = list(request.FILES.values())[0]
fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
folder = fb_uploadurl_re.sub('', folder)
# temporary upload folder should be outside self.directory
if folder == UPLOAD_TEMPDIR and temporary == "true":
path = folder
else:
path = os.path.join(self.directory, folder)
# we convert the filename before uploading in order
# to check for existing files/folders
file_name = convert_filename(filedata.name)
filedata.name = file_name
file_path = os.path.join(path, file_name)
file_already_exists = self.storage.exists(file_path)
# construct temporary filename by adding the upload folder, because
# otherwise we don't have any clue if the file has temporary been
# uploaded or not
if folder == UPLOAD_TEMPDIR and temporary == "true":
temp_filename = os.path.join(folder, file_name)
# Check for name collision with a directory
if file_already_exists and self.storage.isdir(file_path):
ret_json = {'success': False, 'filename': file_name}
return HttpResponse(json.dumps(ret_json))
signals.filebrowser_pre_upload.send(sender=request, path=folder, file=filedata, site=self)
uploadedfile = handle_file_upload(path, filedata, site=self)
if file_already_exists and OVERWRITE_EXISTING:
old_file = smart_text(file_path)
new_file = smart_text(uploadedfile)
self.storage.move(new_file, old_file, allow_overwrite=True)
full_path = FileObject(smart_text(old_file), site=self).path_full
else:
file_name = smart_text(uploadedfile)
filedata.name = os.path.relpath(file_name, path)
full_path = FileObject(smart_text(file_name), site=self).path_full
# set permissions
if DEFAULT_PERMISSIONS is not None:
os.chmod(full_path, DEFAULT_PERMISSIONS)
f = FileObject(smart_text(file_name), site=self)
signals.filebrowser_post_upload.send(sender=request, path=folder, file=f, site=self)
# We don't know at this stage if it's an image
# so attempt to generate an admin thumb and catch the exception
try:
admin_thumbnail_url = f.version_generate(ADMIN_THUMBNAIL).url
except IOError:
admin_thumbnail_url = ''
# Let Ajax Upload know whether we saved it or not
ret_json = {
'success': True,
'filename': f.filename,
'temp_filename': temp_filename,
'url': f.url,
'admin_thumbnail_url': admin_thumbnail_url,
}
return HttpResponse(json.dumps(ret_json), content_type="application/json")