def mkdir(self, path=None): """ Directory creation view method :param path: Optional directory path. If not provided, will use the base directory """ # Get path and verify if it is valid base_path, directory, path = self._normalize_path(path) dir_url = self._get_dir_url('.index', path) if not self.can_mkdir: flash(gettext('Directory creation is disabled.'), 'error') return redirect(dir_url) if not self.is_accessible_path(path): flash(gettext('Permission denied.'), 'error') return redirect(self._get_dir_url('.index')) form = self.name_form() if self.validate_form(form): try: os.mkdir(op.join(directory, form.name.data)) self.on_mkdir(directory, form.name.data) return redirect(dir_url) except Exception as ex: flash(gettext('Failed to create directory: %(error)s', error=ex), 'error') else: helpers.flash_errors(form, message='Failed to create directory: %(error)s') return self.render(self.mkdir_template, form=form, dir_url=dir_url)
def rename(self): """ Rename view method """ form = self.name_form() path = form.path.data if path: base_path, full_path, path = self._normalize_path(path) return_url = self._get_dir_url('.index', op.dirname(path)) else: return redirect(self.get_url('.index')) if not self.can_rename: flash(gettext('Renaming is disabled.'), 'error') return redirect(return_url) if not self.is_accessible_path(path): flash(gettext('Permission denied.'), 'error') return redirect(self._get_dir_url('.index')) if not op.exists(full_path): flash(gettext('Path does not exist.'), 'error') return redirect(return_url) if self.validate_form(form): try: dir_base = op.dirname(full_path) filename = secure_filename(form.name.data) os.rename(full_path, op.join(dir_base, filename)) self.on_rename(full_path, dir_base, filename) flash(gettext('Successfully renamed "%(src)s" to "%(dst)s"', src=op.basename(path), dst=filename)) except Exception as ex: flash(gettext('Failed to rename: %(error)s', error=ex), 'error') return redirect(return_url) else: helpers.flash_errors(form, message='Failed to rename: %(error)s') return self.render(self.rename_template, form=form, path=op.dirname(path), name=op.basename(path), dir_url=return_url)
def delete(self): """ Delete view method """ form = self.delete_form() path = form.path.data if path: return_url = self._get_dir_url('.index', op.dirname(path)) else: return_url = self.get_url('.index') if self.validate_form(form): # Get path and verify if it is valid base_path, full_path, path = self._normalize_path(path) if not self.can_delete: flash(gettext('Deletion is disabled.'), 'error') return redirect(return_url) if not self.is_accessible_path(path): flash(gettext('Permission denied.'), 'error') return redirect(self._get_dir_url('.index')) if op.isdir(full_path): if not self.can_delete_dirs: flash(gettext('Directory deletion is disabled.'), 'error') return redirect(return_url) try: shutil.rmtree(full_path) self.on_directory_delete(full_path, path) flash(gettext('Directory "%(path)s" was successfully deleted.', path=path)) except Exception as ex: flash(gettext('Failed to delete directory: %(error)s', error=ex), 'error') else: try: os.remove(full_path) self.on_file_delete(full_path, path) flash(gettext('File "%(name)s" was successfully deleted.', name=path)) except Exception as ex: flash(gettext('Failed to delete file: %(name)s', name=ex), 'error') else: helpers.flash_errors(form, message='Failed to delete file. %(error)s') return redirect(return_url)
def edit(self): """ Edit view method """ request = get_current_request() next_url = None path = request.GET.getall('path') if not path: return redirect(self.get_url('.index')) if len(path) > 1: next_url = self.get_url('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) if not self.is_accessible_path(path) or not self.is_file_editable(path): flash(gettext('Permission denied.'), 'error') return redirect(self._get_dir_url('.index')) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = self.edit_form() error = False if self.validate_form(form): form.process(request.POST, content='') if form.validate(): try: with open(full_path, 'w') as f: f.write(request.POST['content']) except IOError: flash(gettext("Error saving changes to %(name)s.", name=path), 'error') error = True else: self.on_edit_file(full_path, path) flash(gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: helpers.flash_errors(form, message='Failed to edit file. %(error)s') try: with open(full_path, 'rb') as f: content = f.read() except IOError: flash(gettext("Error reading %(name)s.", name=path), 'error') error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: try: content = content.decode('utf8') except UnicodeDecodeError: flash(gettext("Cannot edit %(name)s.", name=path), 'error') error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error)