def upload(request): """ Multiple File Upload. """ from django.forms.formsets import formset_factory # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') messages.add_message(request, messages.ERROR, msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) if STRICT_PIL: from PIL import ImageFile else: try: from PIL import ImageFile except ImportError: import ImageFile ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k from filebrowser.forms import UploadForm, BaseUploadFormSet UploadFormSet = formset_factory(UploadForm, formset=BaseUploadFormSet, extra=5) if request.method == 'POST': formset = UploadFormSet(data=request.POST, files=request.FILES, path=abs_path) if formset.is_valid(): for cleaned_data in formset.cleaned_data: if cleaned_data: f = cleaned_data['file'] f.name = convert_filename(f.name) # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=abs_path, file=f) # HANDLE UPLOAD uploadedfile = handle_file_upload(abs_path, f) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=abs_path, file=uploadedfile) # MESSAGE & REDIRECT msg = _('Upload successful.') messages.add_message(request, messages.INFO, msg) # on redirect, sort by date desc to see the uploaded files on top of the list redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o") return HttpResponseRedirect(redirect_url) else: formset = UploadFormSet(path=abs_path) return render_to_response('filebrowser/upload.html', { 'formset': formset, 'dir': path, 'query': query, 'settings_var': get_settings_var(), 'breadcrumbs_title': _(u'Upload'), 'title': _(u'Select files to upload'), }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if 'pop' in query: is_popup = True else: is_popup = False if path is None: msg = _('The requested Folder does not exist.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) # SESSION (used for flash-uploading) session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) return render_to_response(_template() + 'upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload'), 'is_popup': is_popup }, context_instance=Context(request))
def delete_confirm(self, request): """ Delete existing File/Directory. """ query = request.GET path = u'%s' % os.path.join(self.directory, query.get('dir', '')) fileobject = FileObject(os.path.join(path, query.get('filename', '')), site=self) if fileobject.filetype == "Folder": filelisting = FileListing(os.path.join(path, fileobject.filename), sorting_by=query.get('o', 'filename'), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER), site=self) filelisting = filelisting.files_walk_total() if len(filelisting) > 100: additional_files = len(filelisting) - 100 filelisting = filelisting[:100] else: additional_files = None else: filelisting = None additional_files = None return render_to_response('filebrowser/delete_confirm.html', { 'fileobject': fileobject, 'filelisting': filelisting, 'additional_files': additional_files, 'query': query, 'title': _(u'Confirm delete'), 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Confirm delete'), 'filebrowser_site': self }, context_instance=Context(request, current_app=self.name))
def delete_confirm(self, request): """ Delete existing File/Directory. """ query = request.GET path = u'%s' % os.path.join(self.directory, query.get('dir', '')) fileobject = FileObject(os.path.join(path, query.get('filename', '')), site=self) if fileobject.filetype == "Folder": filelisting = FileListing(os.path.join(path, fileobject.filename), sorting_by=query.get('o', 'filename'), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER), site=self) filelisting = filelisting.files_walk_total() if len(filelisting) > 100: additional_files = len(filelisting) - 100 filelisting = filelisting[:100] else: additional_files = None else: filelisting = None additional_files = None return render_to_response('filebrowser/delete_confirm.html', { 'fileobject': fileobject, 'filelisting': filelisting, 'additional_files': additional_files, 'query': query, 'title': _(u'Confirm delete'), 'is_popup': "pop" in request.REQUEST, 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Confirm delete'), 'filebrowser_site': self }, context_instance=Context(request, current_app=self.name))
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 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 upload(request): """ Multipe File Upload. """ from django.http import parse_cookie # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', '')) engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response('filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload') }, 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(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 delete_confirm(request): """ Delete existing File/Directory. """ query = request.GET abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, query.get('dir', '')) fileobject = FileObject(os.path.join(abs_path, query.get('filename', ''))) if fileobject.filetype == "Folder": filelisting = FileListing(os.path.join(abs_path, fileobject.filename), filter_func=filter_browse, sorting_by=query.get('o', 'filename'), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER)) filelisting = filelisting.files_walk_total() if len(filelisting) > 100: additional_files = len(filelisting) - 100 filelisting = filelisting[:100] else: additional_files = None else: filelisting = None additional_files = None return render_to_response('filebrowser/delete_confirm.html', { 'fileobject': fileobject, 'filelisting': filelisting, 'additional_files': additional_files, 'query': query, 'title': _(u'Confirm delete'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Confirm delete') }, 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', '')) 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 upload(request): """ Multipe File Upload. """ from django.http import parse_cookie # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', '')) engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response('filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload') }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if 'pop' in query: is_popup = True else: is_popup = False if path is None: msg = _('The requested Folder does not exist.') messages.warning(request,message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) # SESSION (used for flash-uploading) session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) return render_to_response(_template() + 'upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload'), 'is_popup': is_popup }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie # QUERY / PATH CHECK query = request.GET path = get_path(query.get("dir", "")) if path is None: msg = _("The requested Folder 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) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get("HTTP_COOKIE", "")) engine = __import__(settings.SESSION_ENGINE, {}, {}, [""]) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response( "filebrowser/upload.html", { "query": query, "title": _(u"Select files to upload"), "settings_var": get_settings_var(), "session_key": session_key, "breadcrumbs": get_breadcrumbs(query, path), "breadcrumbs_title": _(u"Upload"), }, 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 browse(request): """ Browse Files/Directories. """ query = request.GET.copy() abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, query.get('dir', '')) filelisting = FileListing(abs_path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER)) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() # If we do a search, precompile the search pattern now do_search = query.get("q") if do_search: re_q = re.compile(query.get("q").lower(), re.M) filter_type = query.get('filter_type') filter_date = query.get('filter_date') for fileobject in listing: # date/type filter append = False if (not filter_type or fileobject.filetype == filter_type) and (not filter_date or get_filterdate(filter_date, fileobject.date or 0)): append = True # search if do_search and not re_q.search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = len(listing) filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render_to_response('filebrowser/index.html', { 'p': p, 'page': page, 'filelisting': filelisting, 'query': query, 'title': _(u'FileBrowser'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "" }, context_instance=Context(request))
def edit(request): """ simple edit a file content. TODO: - Ask the user for file encoding? - catch read/write errors - merge coded parts and template parts """ # 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 relative_server_path = os.path.join(DIRECTORY, path, filename) abs_filepath = os.path.join(MEDIA_ROOT, relative_server_path) if request.method == 'POST': encoding_form = SelectEncodingForm(request.POST) edit_form = EditForm(request.POST) if "reopen" in request.POST: # Reopen the file with other encoding if encoding_form.is_valid(): encoding = encoding_form.cleaned_data["encoding"] edit_form = _get_editfile_forms(request, abs_filepath, encoding) msg = _("File was reopend with '%s' encoding." % encoding) request.user.message_set.create(message=msg) else: if edit_form.is_valid() and encoding_form.is_valid(): new_content = edit_form.cleaned_data["content"] encoding = encoding_form.cleaned_data["encoding"] f = codecs.open(abs_filepath, "w", encoding) f.write(new_content) f.close() msg = _( "Editing was successful." " (Content was written with '%s' encoding.)" ) % (encoding) request.user.message_set.create(message=msg) redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "filename") return HttpResponseRedirect(redirect_url) else: default_encoding = CodecChoiceField().choices[0][0] #FIXME encoding_form = SelectEncodingForm() edit_form = _get_editfile_forms(request, abs_filepath, default_encoding) return render_to_response('filebrowser/edit.html', { 'encoding_form': encoding_form, 'edit_form': edit_form, 'query': request.GET, 'title': _(u'edit "%s"') % filename, 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(request.GET, path), 'breadcrumbs_title': _(u'edit') }, context_instance=Context(request))
def upload(request): """ Multiple File Upload. """ from django.forms.formsets import formset_factory # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) if STRICT_PIL: from PIL import ImageFile else: try: from PIL import ImageFile except ImportError: import ImageFile ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k from filebrowser.forms import UploadForm, BaseUploadFormSet UploadFormSet = formset_factory(UploadForm, formset=BaseUploadFormSet, extra=5) if request.method == 'POST': formset = UploadFormSet(data=request.POST, files=request.FILES, path=abs_path) if formset.is_valid(): for cleaned_data in formset.cleaned_data: if cleaned_data: f = cleaned_data['file'] f.name = convert_filename(f.name) # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=abs_path, file=f) # HANDLE UPLOAD uploadedfile = handle_file_upload(abs_path, f) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=abs_path, file=uploadedfile) # MESSAGE & REDIRECT msg = _('Upload successful.') request.user.message_set.create(message=msg) # on redirect, sort by date desc to see the uploaded files on top of the list redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o") return HttpResponseRedirect(redirect_url) else: formset = UploadFormSet(path=abs_path) return render_to_response('filebrowser/upload.html', { 'formset': formset, 'dir': path, 'query': query, 'settings_var': get_settings_var(), 'breadcrumbs_title': _(u'Upload'), 'title': _(u'Select files to upload'), }, context_instance=Context(request))
def browse(request): """ Browse Files/Directories. """ query = request.GET.copy() abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, query.get( 'dir', '')) filelisting = FileListing(abs_path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER)) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() for fileobject in listing: # date/type filter append = False if fileobject.filetype == query.get( 'filter_type', fileobject.filetype) and get_filterdate( query.get('filter_date', ''), fileobject.date or 0): append = True # search if query.get("q") and not re.compile(query.get("q").lower(), re.M).search( fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = filelisting.results_listing_filtered filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render_to_response( 'filebrowser/index.html', { 'p': p, 'page': page, 'filelisting': filelisting, 'query': query, 'title': _(u'FileBrowser'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "" }, context_instance=Context(request))
def mkdir(request): """ Make Directory. """ from filebrowser.forms import MakeDirForm # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') messages.warning(request,message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) if request.method == 'POST': form = MakeDirForm(abs_path, request.POST) if form.is_valid(): _new_dir_name = form.cleaned_data['dir_name'] server_path = _check_access(request, path, _new_dir_name) try: # PRE CREATE SIGNAL filebrowser_pre_createdir.send(sender=request, path=path, dirname=_new_dir_name) # CREATE FOLDER os.mkdir(server_path) os.chmod(server_path, 0o775) # POST CREATE SIGNAL filebrowser_post_createdir.send(sender=request, path=path, dirname=_new_dir_name) # MESSAGE & REDIRECT msg = _('The Folder %s was successfully created.') % (_new_dir_name) messages.success(request,message=msg) # on redirect, sort by date desc to see the new directory on top of the list # remove filter in order to actually _see_ the new folder # remove pagination redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o,filter_type,filter_date,q,p") return HttpResponseRedirect(redirect_url) except OSError(errno, strerror): if errno == 13: form.errors['dir_name'] = forms.util.ErrorList([_('Permission denied.')]) else: form.errors['dir_name'] = forms.util.ErrorList([_('Error creating folder.')]) else: form = MakeDirForm(abs_path) return render_to_response('filebrowser/makedir.html', { 'form': form, 'query': query, 'title': _(u'New Folder'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'New Folder') }, context_instance=Context(request))
def browse(request): """ Browse Files/Directories. """ query = request.GET.copy() user = request.user.username abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, user, query.get('dir', '')) if not abs_path.startswith(request.user.get_profile().media_path) and \ not request.user.is_superuser: raise Http403("You may not access this directory") filelisting = FileListing(abs_path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER)) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() for fileobject in listing: # date/type filter append = False if fileobject.filetype == query.get('filter_type', fileobject.filetype) and get_filterdate(query.get('filter_date', ''), fileobject.date or 0): append = True # search if query.get("q") and not re.compile(query.get("q").lower(), re.M).search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = filelisting.results_listing_filtered filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render_to_response('filebrowser/index.html', { 'p': p, 'page': page, 'filelisting': filelisting, 'query': query, 'title': _(u'FileBrowser'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "" }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ query = request.GET return render_to_response('filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Upload') }, context_instance=Context(request))
def version(request): """ Version detail. """ query = request.GET abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, query.get('dir', '')) fileobject = FileObject(os.path.join(abs_path, query.get('filename', ''))) return render_to_response('filebrowser/version.html', { 'fileobject': fileobject, 'query': query, 'settings_var': get_settings_var(), }, context_instance=Context(request))
def _get_list_item(self, request): query = request.GET.copy() path = u'%s' % os.path.join(self.directory, query.get('dir', '')) fileobject = FileObject(os.path.join(path, query.get('filename', '')), site=self) rendered = render_to_string('filebrowser/include/filelist_item.html', { 'fileobject': fileobject, 'settings_var': get_settings_var(directory=self.directory), 'query': query, }) json_response = {"html": rendered} return HttpResponse(json.dumps(json_response))
def version(self, request): """ Version detail. """ query = request.GET path = u'%s' % os.path.join(self.directory, query.get('dir', '')) fileobject = FileObject(os.path.join(path, query.get('filename', '')), site=self) return render_to_response('filebrowser/version.html', { 'fileobject': fileobject, 'query': query, 'settings_var': get_settings_var(directory=self.directory), 'filebrowser_site': self }, context_instance=Context(request, current_app=self.name))
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 upload(self, request): """ Multipe File Upload. """ query = request.GET path = u'%s' % os.path.join(self.directory, query.get('dir', '')) return render_to_response('filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Upload'), 'filebrowser_site': self }, context_instance=Context(request, current_app=self.name))
def version(self, request): """ Version detail. """ query = request.GET abs_path = u'%s' % os.path.join(self.media_root, query.get('dir', '')) fileobject = FileObject(os.path.join(abs_path, query.get('filename', '')), media_root=self.media_root, media_url=self.media_url) return render_to_response('filebrowser/version.html', { 'fileobject': fileobject, 'query': query, 'settings_var': get_settings_var(media_root=self.media_root, media_url=self.media_url), 'media_root': self.media_root, 'media_url': self.media_url, }, context_instance=Context(request, current_app=self.name))
def upload(request): """ Multipe File Upload. """ # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if 'pop' in query: is_popup = True else: is_popup = False if path is None: msg = _('The requested Folder does not exist.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) # SESSION (used for flash-uploading) session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) # we need max. upload size in MB for dropzone library max_upload_size = fb_settings.MAX_UPLOAD_SIZE / 1048576.0 # we need accepted files mime-type for dropzone library accepted_files = set() for ext_list in fb_settings.EXTENSIONS.values(): for ext in ext_list: try: accepted_files.add(mimetypes.types_map[ext]) except KeyError: pass return render( request, _template() + 'upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload'), 'is_popup': is_popup, 'max_upload_size': max_upload_size, 'accepted_files': ','.join(accepted_files) })
def edit(request, root_directory=DIRECTORY): """ simple edit a file content. TODO: - Ask the user for file encoding? - catch read/write errors - merge coded parts and template parts """ # QUERY / PATH CHECK result = _get_path_and_filename(request, root_directory) if isinstance(result, HttpResponse): # path or filename doesn't exist return result path, filename, abs_path = result relative_server_path = os.path.join(root_directory, path, filename) abs_filepath = os.path.join(MEDIA_ROOT, relative_server_path) if request.method == "POST": form = EditForm(request.POST) if form.is_valid(): new_content = form.cleaned_data["content"] f = codecs.open(abs_filepath, "w", encoding="UTF-8") f.write(new_content) f.close() msg = _("Editing was successful.") _message(request, msg) redirect_url = reverse("fb_browse") + query_helper(request.GET, "", "filename") return HttpResponseRedirect(redirect_url) else: f = file(abs_filepath, "r") file_content = f.read() f.close() form = EditForm(initial={"content": file_content}) return render_to_response( "filebrowser/edit.html", { "form": form, "query": request.GET, "title": _(u'edit "%s"') % filename, "settings_var": get_settings_var(), "breadcrumbs": get_breadcrumbs(request.GET, path), "breadcrumbs_title": _(u"edit"), }, context_instance=Context(request), )
def upload(request): """ Multipe File Upload. """ # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if 'pop' in query: is_popup = True else: is_popup = False if path is None: msg = _('The requested Folder does not exist.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) # SESSION (used for flash-uploading) session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) # we need max. upload size in MB for dropzone library max_upload_size = fb_settings.MAX_UPLOAD_SIZE / 1048576.0 # we need accepted files mime-type for dropzone library accepted_files = set() for ext_list in fb_settings.EXTENSIONS.values(): for ext in ext_list: try: accepted_files.add(mimetypes.types_map[ext]) except KeyError: pass return render(request, _template() + 'upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload'), 'is_popup': is_popup, 'max_upload_size': max_upload_size, 'accepted_files': ','.join(accepted_files) })
def version(self, request): """ Version detail. """ query = request.GET path = u"%s" % os.path.join(self.directory, query.get("dir", "")) fileobject = FileObject(os.path.join(path, query.get("filename", "")), site=self) return render_to_response( "filebrowser/version.html", { "fileobject": fileobject, "query": query, "settings_var": get_settings_var(directory=self.directory), "site": self, }, context_instance=Context(request, current_app=self.name), )
def upload(self, request): """ Multipe File Upload. """ query = request.GET path = u"%s" % os.path.join(self.directory, query.get("dir", "")) return render_to_response( "filebrowser/upload.html", { "query": query, "title": _(u"Select files to upload"), "settings_var": get_settings_var(directory=self.directory), "breadcrumbs": get_breadcrumbs(query, query.get("dir", "")), "breadcrumbs_title": _(u"Upload"), "site": self, }, context_instance=Context(request, current_app=self.name), )
def delete_confirm(self, request): """ Delete existing File/Directory. """ query = request.GET abs_path = u"%s" % os.path.join(self.media_root, query.get("dir", "")) fileobject = FileObject( os.path.join(abs_path, query.get("filename", "")), media_root=self.media_root, media_url=self.media_url ) if fileobject.filetype == "Folder": filelisting = FileListing( os.path.join(abs_path, fileobject.filename), sorting_by=query.get("o", "filename"), sorting_order=query.get("ot", DEFAULT_SORTING_ORDER), media_root=self.media_root, media_url=self.media_url, ) filelisting = filelisting.files_walk_total() if len(filelisting) > 100: additional_files = len(filelisting) - 100 filelisting = filelisting[:100] else: additional_files = None else: filelisting = None additional_files = None return render_to_response( "filebrowser/delete_confirm.html", { "fileobject": fileobject, "filelisting": filelisting, "additional_files": additional_files, "query": query, "title": _(u"Confirm delete"), "settings_var": get_settings_var(media_root=self.media_root, media_url=self.media_url), "breadcrumbs": get_breadcrumbs(query, query.get("dir", "")), "breadcrumbs_title": _(u"Confirm delete"), "media_root": self.media_root, "media_url": self.media_url, }, context_instance=Context(request, current_app=self.name), )
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie query = request.GET abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, query.get('dir', '')) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', '')) engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response('filebrowser/upload.html', { 'session_key': session_key, 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Upload') }, context_instance=Context(request))
def version(self, request): """ Version detail. """ query = request.GET abs_path = u"%s" % os.path.join(self.media_root, query.get("dir", "")) fileobject = FileObject( os.path.join(abs_path, query.get("filename", "")), media_root=self.media_root, media_url=self.media_url ) return render_to_response( "filebrowser/version.html", { "fileobject": fileobject, "query": query, "settings_var": get_settings_var(media_root=self.media_root, media_url=self.media_url), "media_root": self.media_root, "media_url": self.media_url, }, context_instance=Context(request, current_app=self.name), )
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie if USE_NATIVE_UPLOAD and request.method == 'POST': if 'file' in request.FILES: dir = request.GET.get('dir', '') _process_fileupload(request, request.FILES['file'], dir) return HttpResponseRedirect( reverse('admin:filebrowser:browse') + '?dir=%s' % dir) else: # Flash-upload submits to '_upload_file' view instead. pass # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("admin:filebrowser:browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', '')) engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response( 'filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path, _(u'Upload')), 'session_key': session_key, }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie query = request.GET user = request.user.username abs_path = u'%s' % os.path.join(MEDIA_ROOT, DIRECTORY, user, query.get('dir', '')) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', '')) engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response('filebrowser/upload.html', { 'session_key': session_key, 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'Upload') }, context_instance=Context(request))
def upload(request): """ Multiple File Upload. """ # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("fb_browse")) redirect_url = reverse("fb_browse") + query_helper(query, "ot=desc,o=date", "ot,o") return render_to_response('filebrowser/upload_uploadify.html', { 'redirect_url': redirect_url, 'query': query, 'settings_var': get_settings_var(), 'breadcrumbs_title': _(u'Upload'), 'title': _(u'Select files to upload'), }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie if USE_NATIVE_UPLOAD and request.method == 'POST': if 'file' in request.FILES: dir = request.GET.get('dir', '') _process_fileupload(request, request.FILES['file'], dir) return HttpResponseRedirect(reverse('admin:filebrowser:browse') + '?dir=%s' % dir) else: # Flash-upload submits to '_upload_file' view instead. pass # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("admin:filebrowser:browse")) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) # SESSION (used for flash-uploading) cookie_dict = parse_cookie(request.META.get('HTTP_COOKIE', '')) engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) session_key = cookie_dict.get(settings.SESSION_COOKIE_NAME, None) return render_to_response('filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path, _(u'Upload')), 'session_key': session_key, }, context_instance=Context(request))
def upload(request): """ Multipe File Upload. """ # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if path is None: msg = _('The requested Folder does not exist.') messages.warning(request,message=msg) return HttpResponseRedirect(reverse("fb_browse")) # SESSION (used for flash-uploading) session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) return render_to_response('filebrowser/upload.html', { 'query': query, 'title': _(u'Select files to upload'), 'settings_var': get_settings_var(), 'session_key': session_key, 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'Upload') }, 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.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))
def browse(self, request): """ Browse Files/Directories. """ filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.iteritems(): exp = (r'_%s(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) def filter_browse(item): filtered = item.filename.startswith('.') for re_prefix in filter_re: if re_prefix.search(item.filename): filtered = True if filtered: return False return True query = request.GET.copy() path = u'%s' % os.path.join(self.directory, query.get('dir', '')) filelisting = FileListing(path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get( 'ot', DEFAULT_SORTING_ORDER), site=self) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() # If we do a search, precompile the search pattern now do_search = query.get("q") if do_search: re_q = re.compile(query.get("q").lower(), re.M) filter_type = query.get('filter_type') filter_date = query.get('filter_date') for fileobject in listing: # date/type filter append = False if (not filter_type or fileobject.filetype == filter_type) and ( not filter_date or get_filterdate(filter_date, fileobject.date or 0)): append = True # search if do_search and not re_q.search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = len(listing) filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render_to_response( 'filebrowser/index.html', { 'p': p, 'page': page, 'filelisting': filelisting, 'query': query, 'title': _(u'FileBrowser'), 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "", 'filebrowser_site': self }, context_instance=Context(request, current_app=self.name))
except OSError, (errno, strerror): if errno == 13: form.errors['name'] = forms.util.ErrorList( [_('Permission denied.')]) else: form.errors['name'] = forms.util.ErrorList( [_('Error creating folder.')]) else: form = CreateDirForm(path, filebrowser_site=self) return render_to_response( 'filebrowser/createdir.html', { 'form': form, 'query': query, 'title': _(u'New Folder'), 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': _(u'New Folder'), 'filebrowser_site': self }, context_instance=Context(request, current_app=self.name)) def upload(self, request): """ Multipe File Upload. """ query = request.GET path = u'%s' % os.path.join(self.directory, query.get('dir', '')) return render_to_response( 'filebrowser/upload.html', {
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 browse(request): """ Browse Files/Directories. """ # QUERY / PATH CHECK query = request.GET.copy() path = get_path(query.get('dir', '')) directory = get_path('') if path is None: msg = _('The requested Folder does not exist.') messages.add_message(request, messages.SUCCESS, msg) if directory is None: # The DIRECTORY does not exist, raise an error to prevent eternal redirecting. raise ImproperlyConfigured, _( "Error finding Upload-Folder. Maybe it does not exist?") redirect_url = reverse("fb_browse") + query_helper(query, "", "dir") return HttpResponseRedirect(redirect_url) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path) # INITIAL VARIABLES results_var = { 'results_total': 0, 'results_current': 0, 'delete_total': 0, 'images_total': 0, 'select_total': 0 } counter = {} for k, v in EXTENSIONS.iteritems(): counter[k] = 0 dir_list = os.listdir(abs_path) files = [] for file in dir_list: # EXCLUDE FILES MATCHING VERSIONS_PREFIX OR ANY OF THE EXCLUDE PATTERNS filtered = file.startswith('.') for re_prefix in filter_re: if re_prefix.search(file): filtered = True if filtered: continue results_var['results_total'] += 1 # CREATE FILEOBJECT fileobject = FileObject(os.path.join(DIRECTORY, path, file)) # FILTER / SEARCH append = False if fileobject.filetype == request.GET.get( 'filter_type', fileobject.filetype) and get_filterdate( request.GET.get('filter_date', ''), fileobject.date): append = True if request.GET.get('q') and not re.compile( request.GET.get('q').lower(), re.M).search(file.lower()): append = False # APPEND FILE_LIST if append: try: # COUNTER/RESULTS if fileobject.filetype == 'Image': results_var['images_total'] += 1 if fileobject.filetype != 'Folder': results_var['delete_total'] += 1 elif fileobject.filetype == 'Folder' and fileobject.is_empty: results_var['delete_total'] += 1 if query.get('type') and query.get( 'type' ) in SELECT_FORMATS and fileobject.filetype in SELECT_FORMATS[ query.get('type')]: results_var['select_total'] += 1 elif not query.get('type'): results_var['select_total'] += 1 except OSError: # Ignore items that have problems continue else: files.append(fileobject) results_var['results_current'] += 1 # COUNTER/RESULTS if fileobject.filetype: counter[fileobject.filetype] += 1 # SORTING query['o'] = request.GET.get('o', DEFAULT_SORTING_BY) query['ot'] = request.GET.get('ot', DEFAULT_SORTING_ORDER) files = sort_by_attr(files, request.GET.get('o', DEFAULT_SORTING_BY)) if not request.GET.get( 'ot') and DEFAULT_SORTING_ORDER == "desc" or request.GET.get( 'ot') == "desc": files.reverse() p = Paginator(files, LIST_PER_PAGE) try: page_nr = request.GET.get('p', '1') except: page_nr = 1 try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render_to_response('filebrowser/index.html', { 'dir': path, 'p': p, 'page': page, 'results_var': results_var, 'counter': counter, 'query': query, 'title': _(u'FileBrowser'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': "" }, context_instance=Context(request))
def mkdir(request): """ Make Directory. """ # QUERY / PATH CHECK query = request.GET path = get_path(query.get('dir', '')) if 'pop' in query: is_popup = True else: is_popup = False if path is None: msg = _('The requested Folder does not exist.') messages.warning(request, message=msg) return HttpResponseRedirect(reverse("fb_browse")) abs_path = _check_access(request, path) if request.method == 'POST': form = MakeDirForm(abs_path, request.POST) if form.is_valid(): _new_dir_name = form.cleaned_data['dir_name'] server_path = _check_access(request, path, _new_dir_name) try: # PRE CREATE SIGNAL filebrowser_pre_createdir.send(sender=request, path=path, dirname=_new_dir_name) # CREATE FOLDER os.mkdir(server_path) os.chmod(server_path, 0o775) # POST CREATE SIGNAL filebrowser_post_createdir.send(sender=request, path=path, dirname=_new_dir_name) # MESSAGE & REDIRECT msg = _('The Folder {0} was successfully created.').format( _new_dir_name) messages.success(request, message=msg) # on redirect, sort by date desc to see # the new directory on top of the list # remove filter in order to actually _see_ the new folder # remove pagination redirect_url = reverse("fb_browse") + query_helper( query, "ot=desc,o=date", "ot,o,filter_type,filter_date,q,p") return HttpResponseRedirect(redirect_url) except OSError as e: if e.errno == 13: form.errors['dir_name'] = forms.utils.ErrorList( [_('Permission denied.')]) else: form.errors['dir_name'] = forms.utils.ErrorList( [_('Error creating folder.')]) else: form = MakeDirForm(abs_path) return render( request, _template() + 'makedir.html', { 'form': form, 'query': query, 'title': _(u'New Folder'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'New Folder'), 'is_popup': is_popup })
return HttpResponseRedirect(redirect_url) except OSError, (errno, strerror): if errno == 13: form.errors['dir_name'] = forms.util.ErrorList( [_('Permission denied.')]) else: form.errors['dir_name'] = forms.util.ErrorList( [_('Error creating folder.')]) else: form = MakeDirForm(abs_path) return render_to_response('filebrowser/makedir.html', { 'form': form, 'query': query, 'title': _(u'New Folder'), 'settings_var': get_settings_var(), 'breadcrumbs': get_breadcrumbs(query, path), 'breadcrumbs_title': _(u'New Folder') }, context_instance=Context(request)) mkdir = staff_member_required(never_cache(mkdir)) def upload(request): """ Multipe File Upload. """ from django.http import parse_cookie