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 handle_action(self, return_view=None): """ Handle action request. :param return_view: Name of the view to return to after the request. If not provided, will return user to the return url in the form or the list view. """ form = self.action_form() if self.validate_form(form): # using getlist instead of FieldList for backward compatibility ids = request.form.getlist('rowid') action = form.action.data handler = self._actions_data.get(action) if handler and self.is_action_allowed(action): response = handler[0](ids) if response is not None: return response else: flash_errors(form, message='Failed to perform action. %(error)s') if return_view: url = self.get_url('.' + return_view) else: url = get_redirect_target() or self.get_url('.index_view') return redirect(url)
def _action_view_base(self, action, error_msg): return_url = get_redirect_target() or self.get_url(".index_view") form = self.action_form() if self.validate_form(form): action((request.form.get('id'), )) return redirect(return_url) else: flash_errors(form, message=error_msg) return redirect(return_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_view', op.dirname(path)) else: return redirect(self.get_url('.index_view')) 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_view')) if not self.storage.path_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) self.storage.rename_path( full_path, self._separator.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), 'success') 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') if self.rename_modal and request.args.get('modal'): template = self.rename_modal_template else: template = self.rename_template return self.render(template, form=form, path=op.dirname(path), name=op.basename(path), dir_url=return_url, header_text=gettext('Rename %(name)s', name=op.basename(path)))
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 self.storage.is_dir(full_path): if not self.can_delete_dirs: flash(gettext('Directory deletion is disabled.'), 'error') return redirect(return_url) try: self.before_directory_delete(full_path, path) self.storage.delete_tree(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: self.before_file_delete(full_path, path) self.delete_file(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 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_view", op.dirname(path)) else: return redirect(self.get_url(".index_view")) 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_view")) if not self.storage.path_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) self.storage.rename_path(full_path, self._separator.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") if self.rename_modal and request.args.get("modal"): template = self.rename_modal_template else: template = self.rename_template return self.render( template, form=form, path=op.dirname(path), name=op.basename(path), dir_url=return_url, header_text=gettext("Rename %(name)s", name=op.basename(path)), )
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_view", 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_view")) form = self.name_form() if self.validate_form(form): try: self.storage.make_dir(directory, form.name.data) self.on_mkdir(directory, form.name.data) flash( gettext( "Successfully created directory: %(directory)s", directory=form.name.data, ), "success", ) 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") if self.mkdir_modal and request.args.get("modal"): template = self.mkdir_modal_template else: template = self.mkdir_template return self.render( template, form=form, dir_url=dir_url, header_text=gettext("Create Directory"), )
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') if self.rename_modal and request.args.get('modal'): template = self.rename_modal_template else: template = self.rename_template return self.render(template, form=form, path=op.dirname(path), name=op.basename(path), dir_url=return_url, header_text=gettext('Rename %(name)s', name=op.basename(path)))
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_view', 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_view')) form = self.name_form() if self.validate_form(form): try: if self.is_newname_existed(path, form.name.data): raise Exception('新文件夹"%s"已经存在' % form.name.data) self.storage.make_dir(directory, form.name.data) self.on_mkdir(directory, form.name.data) flash( gettext('Successfully created directory: %(directory)s', directory=form.name.data), 'success') 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') if self.mkdir_modal and request.args.get('modal'): template = self.mkdir_modal_template else: template = self.mkdir_template return self.render(template, form=form, dir_url=dir_url, header_text=gettext('Create Directory'))
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: self.before_directory_delete(full_path, path) 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: self.before_file_delete(full_path, path) 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 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 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: self.storage.make_dir(directory, form.name.data) self.on_mkdir(directory, form.name.data) flash(gettext('Successfully created directory: %(directory)s', 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') if self.mkdir_modal and request.args.get('modal'): template = self.mkdir_modal_template else: template = self.mkdir_template return self.render(template, form=form, dir_url=dir_url, header_text=gettext('Create Directory'))
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) flash(gettext("Successfully created directory: %(directory)s", 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, header_text=gettext("Create Directory"), modal=request.args.get("modal"), )
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist('path') if not path: return redirect(self.get_url('.index_view')) 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_view')) dir_url = self._get_dir_url('.index_view', op.dirname(path)) next_url = next_url or dir_url form = self.edit_form() error = False if self.validate_form(form): form.process(request.form, content='') if form.validate(): try: self.storage.write_file(full_path, request.form['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), 'success') return redirect(next_url) else: helpers.flash_errors(form, message='Failed to edit file. %(error)s') try: content = self.storage.read_file(full_path) 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 if error: return redirect(next_url) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, dir_url=dir_url, path=path, form=form, error=error, header_text=gettext('Editing %(path)s', path=path))
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist('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.form, content='') if form.validate(): try: with open(full_path, 'w') as f: f.write(request.form['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 if error: return redirect(next_url) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, dir_url=dir_url, path=path, form=form, error=error, header_text=gettext('Editing %(path)s', path=path))
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist("path") if not path: return redirect(self.get_url(".index_view")) 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_view")) dir_url = self._get_dir_url(".index_view", op.dirname(path)) next_url = next_url or dir_url form = self.edit_form() error = False if self.validate_form(form): form.process(request.form, content="") if form.validate(): try: with open(full_path, "w") as f: f.write(request.form["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), "success", ) 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 if error: return redirect(next_url) if self.edit_modal and request.args.get("modal"): template = self.edit_modal_template else: template = self.edit_template return self.render( template, dir_url=dir_url, path=path, form=form, error=error, header_text=gettext("Editing %(path)s", path=path), )
def rename(self, path=None): """ Rename view method """ path = unicode(path) if self.storage.is_dir(path): form = self.name_form() else: form = self.edit_document_form(directory=path) if path: base_path, full_path, path = self._normalize_path(path) return_url = self._get_dir_url('.index_view', op.dirname(path)) else: return redirect(self.get_url('.index_view')) 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_view')) if not self.storage.path_exists(full_path): flash(gettext('Path does not exist.'), 'error') return redirect(return_url) if self.validate_form(form) and not self.storage.is_dir(path): try: dir_base = op.dirname(full_path) filename = form.name.data office = form.office.data chapter = form.chapter.data date = form.date.data if self.is_newname_existed(dir_base, filename): raise Exception(u'新名称"%s"已经存在' % filename) self.storage.rename_path( full_path, self._separator.join([dir_base, filename]) if dir_base else filename) self.on_edit_document(full_path, dir_base, filename, office, chapter, date) flash( gettext('Successfully renamed "%(src)s" to "%(dst)s"', src=op.basename(path), dst=filename), 'success') 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') if self.validate_form(form) and self.storage.is_dir(path): try: dir_base = op.dirname(full_path) filename = form.name.data self.storage.rename_path( full_path, self._separator.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), 'success') 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') if self.rename_modal and request.args.get('modal'): template = self.rename_modal_template else: template = self.rename_template return self.render(template, form=form, path=op.dirname(path), name=op.basename(path), dir_url=return_url, header_text=gettext('Rename %(name)s', name=op.basename(path)))
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist("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.form, content="") if form.validate(): try: with open(full_path, "w") as f: f.write(request.form["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, modal=request.args.get("modal") )