def edit(self): """ Edit view method """ path = request.args.getlist('path') next_url = None if not path: return redirect(url_for('.index')) if len(path) > 1: next_url = url_for('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = EditForm(helpers.get_form_data()) error = False if helpers.validate_form_on_submit(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: try: with open(full_path, 'r') 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.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)
def edit(self): """ Edit view method """ path = request.args.getlist('path') next_url = None if not path: return redirect(url_for('.index')) if len(path) > 1: next_url = url_for('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = EditForm() error = False if request.method == 'POST': 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: flash( gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: try: with open(full_path, 'r') 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.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)
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.")) 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 = EditForm(helpers.get_form_data()) error = False if helpers.validate_form_on_submit(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: try: with open(full_path, "r") 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)
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.')) 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 = EditForm(helpers.get_form_data()) error = False if helpers.validate_form_on_submit(form): form.process(request.form, content='') if form.validate(): try: with codecs.open(full_path, 'w', encoding='utf-8') 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: try: with codecs.open(full_path, 'r', encoding='utf-8') 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: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error, fileExtension=fileExtension)
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)