当前位置: 首页>>代码示例>>Python>>正文


Python forms.RenameForm类代码示例

本文整理汇总了Python中filebrowser.forms.RenameForm的典型用法代码示例。如果您正苦于以下问题:Python RenameForm类的具体用法?Python RenameForm怎么用?Python RenameForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了RenameForm类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: rename

def rename(request):
	"""
	Rename existing File/Directory.

	Includes renaming existing Image Versions/Thumbnails.
	"""

	from filebrowser.forms import RenameForm

	# QUERY / PATH CHECK
	query = request.GET
	path = get_path(query.get('dir', ''))

	if 'pop' in query:
		is_popup = True
	else:
		is_popup = False

	filename = get_file(query.get('dir', ''), query.get('filename', ''))
	if path is None or filename is None:
		if path is None:
			msg = _('The requested Folder does not exist.')
		else:
			msg = _('The requested File does not exist.')
		messages.warning(request, message=msg)
		return HttpResponseRedirect(reverse("fb_browse"))
	abs_path = _check_access(request, path)
	file_extension = os.path.splitext(filename)[1].lower()

	if request.method == 'POST':
		form = RenameForm(abs_path, file_extension, request.POST)
		if form.is_valid():
			relative_server_path = os.path.join(fb_settings.DIRECTORY, path, filename)
			new_filename = form.cleaned_data['name'] + file_extension
			new_relative_server_path = os.path.join(fb_settings.DIRECTORY, path, new_filename)
			try:
				# PRE RENAME SIGNAL
				filebrowser_pre_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
				# DELETE IMAGE VERSIONS/THUMBNAILS
				# regenerating versions/thumbs will be done automatically
				for version in VERSIONS:
					try:
						os.unlink(os.path.join(fb_settings.MEDIA_ROOT, get_version_path(relative_server_path, version)))
					except:
						pass
				# RENAME ORIGINAL
				os.rename(os.path.join(fb_settings.MEDIA_ROOT, relative_server_path),
				          os.path.join(fb_settings.MEDIA_ROOT, new_relative_server_path))
				# POST RENAME SIGNAL
				filebrowser_post_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
				# MESSAGE & REDIRECT
				msg = _('Renaming was successful.')
				messages.success(request, message=msg)
				redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
				return HttpResponseRedirect(redirect_url)
			except OSError, (errno, strerror):
				form.errors['name'] = forms.util.ErrorList([_('Error.')])
开发者ID:davidraywilson,项目名称:sluyterphoto_2016,代码行数:57,代码来源:views.py

示例2: rename

def rename(request):
    """
    Rename existing File/Directory.
    
    Includes renaming existing Image Versions/Thumbnails.
    """

    from filebrowser.forms import RenameForm

    # QUERY / PATH CHECK
    query = request.GET
    path = _get_path(query.get('dir', ''))
    filename = _get_file(query.get('dir', ''), query.get('filename', ''))
    if path is None or filename is None:
        msg = _('Directory/File does not exist.')
        request.user.message_set.create(message=msg)
        return HttpResponseRedirect(reverse("fb_browse"))
    abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)
    file_extension = os.path.splitext(filename)[1].lower()

    if request.method == 'POST':
        form = RenameForm(abs_path, file_extension, request.POST)
        if form.is_valid():
            relative_server_path = os.path.join(DIRECTORY, path, filename)
            new_relative_server_path = os.path.join(DIRECTORY, path, form.cleaned_data['name'] + file_extension)
            try:
                # PRE RENAME SIGNAL
                filebrowser_pre_delete.send(sender=request, path=path, filename=filename)
                # DELETE IMAGE VERSIONS/THUMBNAILS
                # regenerating versions/thumbs will be done automatically
                for version in VERSIONS:
                    try:
                        _delete(MEDIA_ROOT, _get_version_path(relative_server_path, version))
                        #os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
                    except:
                        pass
                # RENAME ORIGINAL
                newfile = os.path.join(MEDIA_ROOT, new_relative_server_path)
                oldfile = os.path.join(MEDIA_ROOT, relative_server_path)
                # opening and saving might not be efficient, but it works with all storages
                default_storage.save(newfile, default_storage.open(oldfile))
                default_storage.delete(oldfile)
                
                # POST RENAME SIGNAL
                filebrowser_post_delete.send(sender=request, path=path, filename=filename)
                # MESSAGE & REDIRECT
                msg = _('Renaming was successful.')
                request.user.message_set.create(message=msg)
                redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
                return HttpResponseRedirect(redirect_url)
            except OSError, (errno, strerror):
                form.errors['name'] = forms.util.ErrorList([_('Error.')])
开发者ID:oppian,项目名称:oppian,代码行数:52,代码来源:views.py

示例3: rename

def rename(request, root_directory=DIRECTORY):
    """
    Rename existing File/Directory.
    
    Includes renaming existing Image Versions/Thumbnails.
    """

    from filebrowser.forms import RenameForm

    # QUERY / PATH CHECK
    query = request.GET
    path = get_path(query.get("dir", ""), root_directory)
    filename = get_file(query.get("dir", ""), query.get("filename", ""), root_directory)
    if path is None or filename is None:
        if path is None:
            msg = _("The requested Folder does not exist.")
        else:
            msg = _("The requested File does not exist.")
        _message(request, msg)
        return HttpResponseRedirect(reverse("fb_browse"))
    abs_path = os.path.join(MEDIA_ROOT, root_directory, path)
    file_extension = os.path.splitext(filename)[1].lower()

    if request.method == "POST":
        form = RenameForm(abs_path, file_extension, request.POST)
        if form.is_valid():
            relative_server_path = os.path.join(root_directory, path, filename)
            new_filename = form.cleaned_data["name"] + file_extension
            new_relative_server_path = os.path.join(root_directory, path, new_filename)
            try:
                # PRE RENAME SIGNAL
                filebrowser_pre_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
                # DELETE IMAGE VERSIONS/THUMBNAILS
                # regenerating versions/thumbs will be done automatically
                for version in VERSIONS:
                    try:
                        os.unlink(os.path.join(MEDIA_ROOT, get_version_path(relative_server_path, version)))
                    except:
                        pass
                # RENAME ORIGINAL
                os.rename(
                    os.path.join(MEDIA_ROOT, relative_server_path), os.path.join(MEDIA_ROOT, new_relative_server_path)
                )
                # POST RENAME SIGNAL
                filebrowser_post_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
                # MESSAGE & REDIRECT
                msg = _("Renaming was successful.")
                _message(request, msg)
                redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "filename")
                return HttpResponseRedirect(redirect_url)
            except OSError, (errno, strerror):
                form.errors["name"] = forms.util.ErrorList([_("Error.")])
开发者ID:primepix,项目名称:django-filebrowser-no-grappelli-and-uploadify,代码行数:52,代码来源:views.py

示例4: rename

def rename(request):
    """
    Rename existing File/Directory.
    
    Includes renaming existing Image Versions/Thumbnails.
    """

    from filebrowser.forms import RenameForm

    # QUERY / PATH CHECK
    result = _get_path_and_filename(request)
    if isinstance(result, HttpResponse):
        # path or filename doesn't exist
        return result
    path, filename, abs_path = result

    file_extension = os.path.splitext(filename)[1].lower()

    if request.method == 'POST':
        form = RenameForm(abs_path, file_extension, request.POST)
        if form.is_valid():
            relative_server_path = os.path.join(DIRECTORY, path, filename)
            new_filename = form.cleaned_data['name'] + file_extension
            new_relative_server_path = os.path.join(DIRECTORY, path, new_filename)
            try:
                # PRE RENAME SIGNAL
                filebrowser_pre_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
                # DELETE IMAGE VERSIONS/THUMBNAILS
                # regenerating versions/thumbs will be done automatically
                for version in VERSIONS:
                    try:
                        os.unlink(os.path.join(MEDIA_ROOT, get_version_path(relative_server_path, version)))
                    except:
                        pass
                # RENAME ORIGINAL
                os.rename(os.path.join(MEDIA_ROOT, relative_server_path), os.path.join(MEDIA_ROOT, new_relative_server_path))
                # POST RENAME SIGNAL
                filebrowser_post_rename.send(sender=request, path=path, filename=filename, new_filename=new_filename)
                # MESSAGE & REDIRECT
                msg = _('Renaming was successful.')
                request.user.message_set.create(message=msg)
                redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "filename")
                return HttpResponseRedirect(redirect_url)
            except OSError, (errno, strerror):
                form.errors['name'] = forms.util.ErrorList([_('Error.')])
开发者ID:jedie,项目名称:django-filebrowser-no-grappelli,代码行数:45,代码来源:views.py

示例5: rename

def rename(request, dir_name=None, file_name=None):
    """
    Rename existing File/Directory.
    """
    
    path = _get_path(request, dir_name)
    query = _get_query(request.GET)
    
    if os.path.isfile(os.path.join(PATH_SERVER, path, file_name)): # file
        file_type = _get_file_type(file_name)
        file_extension = os.path.splitext(file_name)[1].lower()
    else:
        file_extension = ""
        file_type = ""
    
    if request.method == 'POST':
        form = RenameForm(PATH_SERVER, path, file_extension, request.POST)
        if form.is_valid():
            old_path = os.path.join(PATH_SERVER, path, file_name)
            new_path = os.path.join(PATH_SERVER, path, request.POST.get('name').lower() + file_extension)
            try:
                os.rename(old_path, new_path)
                
                # RENAME IMAGE_THUMBNAILS
                if file_type == 'Image':
                    old_thumb_path = os.path.join(PATH_SERVER, path, THUMB_PREFIX + file_name)
                    new_thumb_path = os.path.join(PATH_SERVER, path, THUMB_PREFIX + request.POST.get('name').lower() + file_extension)
                    try:
                        os.rename(old_thumb_path, new_thumb_path)
                    except OSError, (errno, strerror):
                        form.errors['name'] = forms.util.ErrorList([_('Error renaming Thumbnail.')])
                    
                    # RENAME IMAGE VERSIONS? TOO MUCH MAGIC?
                
                # MESSAGE & REDIRECT
                if not form.errors:
                    msg = _('Renaming was successful.')
                    request.user.message_set.create(message=msg)
                    # on redirect, sort by date desc to see the new stuff on top of the list
                    return HttpResponseRedirect(URL_ADMIN + path + "?&ot=desc&o=3&" + query['pop'])
            except OSError, (errno, strerror):
                form.errors['name'] = forms.util.ErrorList([_('Error.')])
开发者ID:callowayproject,项目名称:django-filebrowser,代码行数:42,代码来源:views.py

示例6: rename

def rename(request):
    """
    Rename existing File/Directory.
    
    Includes renaming existing Image Versions/Thumbnails.
    """
    
    from filebrowser.forms import RenameForm
    
    # QUERY / PATH CHECK
    query = request.GET
    path = _get_path(query.get('dir', ''))
    filename = _get_file(query.get('dir', ''), query.get('filename', ''))
    if path is None or filename is None:
        msg = _('Directory/File does not exist.')
        request.user.message_set.create(message=msg)
        return HttpResponseRedirect(reverse("fb_browse"))
    abs_path = os.path.join(settings.MEDIA_ROOT, DIRECTORY, path)
    file_extension = os.path.splitext(filename)[1].lower()
    
    if request.method == 'POST':
        form = RenameForm(abs_path, file_extension, request.POST)
        if form.is_valid():
            relative_server_path = os.path.join(DIRECTORY, path, filename)
            new_relative_server_path = os.path.join(DIRECTORY, path, form.cleaned_data['name'] + file_extension)
            try:
                # DELETE IMAGE VERSIONS/THUMBNAILS
                # regenerating versions/thumbs will be done automatically
                for version in VERSIONS:
                    try:
                        os.unlink(os.path.join(MEDIA_ROOT, _get_version_path(relative_server_path, version)))
                    except:
                        pass
                # RENAME ORIGINAL
                os.rename(os.path.join(MEDIA_ROOT, relative_server_path), os.path.join(MEDIA_ROOT, new_relative_server_path))
                msg = _('Renaming was successful.')
                request.user.message_set.create(message=msg)
                redirect_url = reverse("fb_browse") + query_helper(query, "", "filename")
                return HttpResponseRedirect(redirect_url)
            except OSError, (errno, strerror):
                form.errors['name'] = forms.util.ErrorList([_('Error.')])
开发者ID:nailor,项目名称:asteriski-www,代码行数:41,代码来源:views.py

示例7: test_rename_form

    def test_rename_form(self):
        data = {'name': 'test_dir%$'}
        file_extension = ''
        form = RenameForm(self.working_dir, file_extension, data)
        self.assertFalse(form.is_valid())
        self.assertEquals(
            form.errors['name'],
            ['Only letters, numbers, underscores, '
             'spaces and hyphens are allowed.']
        )

        os.makedirs(os.path.join(self.working_dir, 'test_dir'))
        data = {'name': 'test_dir'}
        file_extension = ''
        form = RenameForm(self.working_dir, file_extension, data)
        self.assertFalse(form.is_valid())
        self.assertEquals(form.errors['name'], ['The Folder already exists.'])

        os.rmdir(os.path.join(self.working_dir, 'test_dir'))

        file_name, file_extension = self.image_name.split('.')
        data = {'name': file_name}
        form = RenameForm(
            self.working_dir, '.{0}'.format(file_extension), data
        )
        self.assertFalse(form.is_valid())
        self.assertEquals(form.errors['name'], ['The File already exists.'])
开发者ID:miguelramos,项目名称:django-media-manager,代码行数:27,代码来源:test_forms.py


注:本文中的filebrowser.forms.RenameForm类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。