Пример #1
0
def rename(request, dir_name=None, file_name=None):
    """
    Rename existing File/Directory.
    """

    path = _get_path(dir_name)
    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 VERSIONS? TOO MUCH MAGIC?

                # MESSAGE & REDIRECT
                msg = _('Renaming was successful.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect(URL_ADMIN + path +
                                            query_helper(query, "", ""))
            except OSError, (errno, strerror):
                form.errors['name'] = forms.util.ErrorList([_('Error.')])
Пример #2
0
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.')])
    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.'])
Пример #4
0
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.')])
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.")])
Пример #6
0
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:
        if path is None:
            msg = _('The requested Folder does not exist.')
        else:
            msg = _('The requested 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_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(query, "", "filename")
                return HttpResponseRedirect(redirect_url)
            except OSError, (errno, strerror):
                form.errors['name'] = forms.util.ErrorList([_('Error.')])
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.')])
Пример #8
0
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.')])
Пример #9
0
def rename(request, dir_name=None, file_name=None):
    """
    Rename existing File/Directory.
    """
    
    path = _get_path(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.')])
Пример #10
0
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.')])
Пример #11
0
    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.'])
Пример #12
0
                          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.')
                messages.add_message(request, messages.SUCCESS, 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.')])
    else:
        form = RenameForm(abs_path, file_extension)

    return render_to_response('filebrowser/rename.html', {
        'form': form,
        'query': query,
        'file_extension': file_extension,
        'title': _(u'Rename "%s"') % filename,
        'settings_var': get_settings_var(),
        'breadcrumbs': get_breadcrumbs(query, path),
        'breadcrumbs_title': _(u'Rename')
    },
                              context_instance=Context(request))


rename = staff_member_required(never_cache(rename))
Пример #13
0
                        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.')])
    else:
        form = RenameForm(PATH_SERVER, path, file_extension)
    
    return render_to_response('filebrowser/rename.html', {
        'form': form,
        'query': query,
        'file_extension': file_extension,
        'settings_var': _get_settings_var(request.META['HTTP_HOST'], path),
        'breadcrumbs': _get_breadcrumbs(_get_query(request.GET), dir_name, 'Rename'),
        'title': _('Rename "%s"') % file_name,
    }, context_instance=Context(request))
rename = staff_member_required(never_cache(rename))


def generateimages(request, dir_name=None, file_name=None):
    """
    Generate Image Versions for existing singe Image or a whole Directory.
Пример #14
0
def rename(request):
    """
    Rename existing File/Directory.

    Includes renaming existing Image Versions/Thumbnails.
    """
    # 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 as e:
                form.errors['name'] = forms.utils.ErrorList([_('Error.')])
    else:
        form = RenameForm(abs_path, file_extension)

    return render(
        request,
        _template() + 'rename.html', {
            'form': form,
            'query': query,
            'file_extension': file_extension,
            'title': _(u'Rename "{0}"').format(filename),
            'settings_var': get_settings_var(),
            'breadcrumbs': get_breadcrumbs(query, path),
            'breadcrumbs_title': _(u'Rename'),
            'is_popup': is_popup
        })