Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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))
Exemplo n.º 3
0
    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))
Exemplo n.º 4
0
    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")
        )
Exemplo n.º 5
0
    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),
        )