def versions(request): """ Show all Versions for an Image according to ADMIN_VERSIONS. """ # 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) return render_to_response('filebrowser/versions.html', { 'original': path_to_url(os.path.join(DIRECTORY, path, filename)), 'query': query, 'title': _(u'Versions for "%s"') % filename, 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Versions for "%s"') % filename }, context_instance=Context(request))
def edit(request): """ Edit existing File. """ from filebrowser.forms import EditForm # 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.") messages.error(request, 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 = EditForm(abs_path, filename, file_extension, request.POST) if form.is_valid(): try: form.save() # MESSAGE & REDIRECT msg = _("Edit action was successful.") messages.success(request, 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 versions(request): """ Show all Versions for an Image according to ADMIN_VERSIONS. """ # 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.") messages.warning(request, message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(fb_settings.MEDIA_ROOT, fb_settings.DIRECTORY, path) return render_to_response( "filebrowser/versions.html", { "original": path_to_url(os.path.join(fb_settings.DIRECTORY, path, filename)), "query": query, "title": _(u'Versions for "%s"') % filename, "settings_var": get_settings_var(), "breadcrumbs": get_breadcrumbs(query, path), "breadcrumbs_title": _(u'Versions for "%s"') % filename, }, context_instance=Context(request), )
def edit(request): """ Edit existing File. """ from filebrowser.forms import EditForm # 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 = EditForm(abs_path, filename, file_extension, request.POST) if form.is_valid(): try: form.save() # MESSAGE & REDIRECT msg = _('Edit action 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 versions(request): """ Show all Versions for an Image according to ADMIN_VERSIONS. """ # 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) return render(request, _template() + 'versions.html', { 'original': path_to_url( os.path.join(fb_settings.DIRECTORY, path, filename)), 'query': query, 'title': _(u'Versions for "{0}"').format(filename), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Versions for "{0}"').format(filename), 'is_popup': is_popup })
def decorator(request, *args, **kwargs): if get_file(request.GET.get('dir', ''), request.GET.get('filename', '')) == None: msg = _('The requested File does not exist.') messages.add_message(request, messages.ERROR, msg) redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "dir") return HttpResponseRedirect(redirect_url) return function(request, *args, **kwargs)
def versions(request): """ Show all Versions for an Image according to ADMIN_VERSIONS. """ # 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.') messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) return render_to_response( 'filebrowser/versions.html', { 'original': path_to_url(os.path.join(DIRECTORY, path, filename)), 'query': query, 'title': _(u'Versions for "%s"') % filename, 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Versions for "%s"') % filename }, context_instance=Context(request))
def versions(request): """ Show all Versions for an Image according to ADMIN_VERSIONS. """ # 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) return render_to_response(_template() + 'versions.html', { 'original': path_to_url(os.path.join(fb_settings.DIRECTORY, path, filename)), 'query': query, 'title': _(u'Versions for "%s"') % filename, 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Versions for "%s"') % filename, 'is_popup': is_popup }, context_instance=Context(request))
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 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.")])
def decorator(request, *args, **kwargs): file_path = get_file(request.GET.get('dir', ''), request.GET.get('filename', '')) if file_path == None: msg = _('The requested File does not exist.') messages.add_message(request, messages.ERROR, msg) redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "dir") return HttpResponseRedirect(redirect_url) elif file_path.startswith('/') or file_path.startswith('..'): # prevent path traversal msg = _('You do not have permission to access this file!') messages.add_message(request, messages.ERROR, msg) redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "dir") return HttpResponseRedirect(redirect_url) return function(request, *args, **kwargs)
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 _get_path_and_filename(request, root_directory): 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) return path, filename, abs_path
def _get_path_and_filename(request, root_directory): 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.') request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, root_directory, path) return path, filename, abs_path
def versions(request): """ Show all Versions for an Image according to ADMIN_VERSIONS. """ # 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 = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, path) path = os.path.join(DIRECTORY, path, filename) settings_var = get_settings_var() if request.POST: formset = ImageCropDataFormSet(request.POST, queryset=ImageCropData.objects.filter(path=path)) if formset.is_valid(): formset.save() for version in settings_var.get("ADMIN_VERSIONS", {}): # TODO: or at least, only do if needed version_generator(path, version) else: raise Exception("Somehow formset became invalid: %s" % formset.errors) return HttpResponseRedirect("") # We have to create all the versions first, so the crop data form has something to fill with #TODO: this could work better somehow for version in VERSIONS: version_for_image(path, version) formset = ImageCropDataFormSet(queryset=ImageCropData.objects.filter(path=path)) return render_to_response( "filebrowser/versions.html", { "original": path_to_url(path), "query": query, "title": _(u'Versions for "%s"') % filename, "settings_var": settings_var, "breadcrumbs": get_breadcrumbs(query, path), "breadcrumbs_title": _(u'Versions for "%s"') % filename, "formset": formset, }, context_instance=Context(request), )
def decorator(request, *args, **kwargs): file_path = get_file(request.GET.get('dir', ''), request.GET.get('filename', ''), site=site) if file_path == None: msg = _('The requested File does not exist.') messages.add_message(request, messages.ERROR, msg) redirect_url = reverse("filebrowser:fb_browse", current_app=site.name) + query_helper( request.GET, "", "dir") return HttpResponseRedirect(redirect_url) elif file_path.startswith('/') or file_path.startswith('..'): # prevent path traversal msg = _('You do not have permission to access this file!') messages.add_message(request, messages.ERROR, msg) redirect_url = reverse("filebrowser:fb_browse", current_app=site.name) + query_helper( request.GET, "", "dir") return HttpResponseRedirect(redirect_url) return function(request, *args, **kwargs)
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 })
def delete(request): """ Delete existing File/Directory. When trying to delete a Directory, the Directory has to be empty. """ # 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.') messages.warning(request,message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) msg = "" if request.GET: if request.GET.get('filetype') != "Folder": relative_server_path = os.path.join(fb_settings.DIRECTORY, path, filename) try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE FILE os.unlink(smart_str(_check_access(request, path, filename))) # DELETE IMAGE VERSIONS/THUMBNAILS for version in VERSIONS: try: os.unlink(os.path.join(fb_settings.MEDIA_ROOT, get_version_path(relative_server_path, version))) except: pass # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The file %s was successfully deleted.') % (filename.lower()) messages.success(request,message=msg) redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError, e: # todo: define error message msg = unicode(e) else: try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE FOLDER os.rmdir(_check_access(request, path, filename)) # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The folder %s was successfully deleted.') % (filename.lower()) messages.success(request,message=msg) redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError, e: # todo: define error message msg = unicode(e)
def delete(request): """ Delete existing File/Directory. When trying to delete a Directory, the Directory has to be empty. """ # 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) msg = "" if request.GET: if request.GET.get('filetype') != "Folder": relative_server_path = os.path.join(DIRECTORY, path, filename) try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE IMAGE VERSIONS/THUMBNAILS for version in VERSIONS: try: os.unlink(os.path.join(MEDIA_ROOT, get_version_path(relative_server_path, version))) except: pass # DELETE FILE os.unlink(smart_str(os.path.join(abs_path, filename))) # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The file %s was successfully deleted.') % (filename.lower()) request.user.message_set.create(message=msg) redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError: # todo: define error message msg = OSError else: try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE FOLDER os.rmdir(os.path.join(abs_path, filename)) # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The folder %s was successfully deleted.') % (filename.lower()) request.user.message_set.create(message=msg) redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError: # todo: define error message msg = OSError if msg: request.user.message_set.create(message=msg) return render_to_response('filebrowser/index.html', { 'dir': dir_name, 'file': request.GET.get('filename', ''), 'query': query, 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, dir_name), 'breadcrumbs_title': "" }, context_instance=Context(request))
def delete(request): """ Delete existing File/Directory. When trying to delete a Directory, the Directory has to be empty. """ # 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.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) msg = "" if request.GET: if request.GET.get('filetype') != "Folder": relative_server_path = os.path.join(fb_settings.DIRECTORY, path, filename) try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE FILE os.unlink(smart_str(_check_access(request, path, filename))) # DELETE IMAGE VERSIONS/THUMBNAILS for version in VERSIONS: try: os.unlink( os.path.join( fb_settings.MEDIA_ROOT, get_version_path(relative_server_path, version))) except: pass # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The file %s was successfully deleted.') % ( filename.lower()) messages.success(request, message=msg) redirect_url = reverse("fb_browse") + query_helper( query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError, e: # todo: define error message msg = unicode(e) else: try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE FOLDER os.rmdir(_check_access(request, path, filename)) # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The folder %s was successfully deleted.') % ( filename.lower()) messages.success(request, message=msg) redirect_url = reverse("fb_browse") + query_helper( query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError, e: # todo: define error message msg = unicode(e)
def delete(request): """ Delete existing File/Directory. When trying to delete a Directory, the Directory has to be empty. """ # 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.') messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) msg = "" if request.GET: if request.GET.get('filetype') != "Folder": relative_server_path = os.path.join(DIRECTORY, path, filename) try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE IMAGE VERSIONS/THUMBNAILS for version in VERSIONS: try: os.unlink( os.path.join( MEDIA_ROOT, get_version_path(relative_server_path, version))) except: pass # DELETE FILE os.unlink(smart_str(os.path.join(abs_path, filename))) # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The file %s was successfully deleted.') % ( filename.lower()) messages.add_message(request, messages.SUCCESS, msg) redirect_url = reverse("fb_browse") + query_helper( query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError: # todo: define error message msg = OSError else: try: # PRE DELETE SIGNAL filebrowser_pre_delete.send(sender=request, path=path, filename=filename) # DELETE FOLDER os.rmdir(os.path.join(abs_path, filename)) # POST DELETE SIGNAL filebrowser_post_delete.send(sender=request, path=path, filename=filename) # MESSAGE & REDIRECT msg = _('The folder %s was successfully deleted.') % ( filename.lower()) messages.add_message(request, messages.SUCCESS, msg) redirect_url = reverse("fb_browse") + query_helper( query, "", "filename,filetype") return HttpResponseRedirect(redirect_url) except OSError: # todo: define error message msg = OSError if msg: messages.add_message(request, messages.SUCCESS, msg) return render_to_response('filebrowser/index.html', { 'dir': dir_name, 'file': request.GET.get('filename', ''), 'query': query, 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, dir_name), 'breadcrumbs_title': "" }, context_instance=Context(request))