Ejemplo n.º 1
0
def save_form():
    ID = request.form['id']
    if not ID:
        raise Exception("no id")

    if not allowed_id(ID):
        return redirect(url_for(".index"))

    NEWID = request.form.get('krename', '').strip()
    FINISH_RENAME = request.form.get('finish_rename', '')
    k = Knowl(ID, saving=True, renaming=bool(NEWID))
    new_title = request.form['title']
    new_content = request.form['content']
    who = current_user.get_id()
    if new_title != k.title or new_content != k.content:
        if not k.content and not k.title and k.exists(allow_deleted=True):
            # Creating a new knowl with the same id as one that had previously been deleted
            k.resurrect()
            flash("Knowl successfully created.  Note that a knowl with this id existed previously but was deleted; its history has been restored.")
        k.title = new_title
        k.content = new_content
        k.timestamp = datetime.now()
        k.status = 0
        k.save(who=who)
    if NEWID:
        if not current_user.is_admin():
            flash("You do not have permissions to rename knowl", "error")
        elif not allowed_id(NEWID):
            pass
        else:
            try:
                k.start_rename(NEWID, who)
            except ValueError as err:
                flash(str(err))
            else:
                if k.sed_safety == 1:
                    flash("Knowl rename process started. You can change code references using".format(NEWID))
                    flash("git grep -l '{0}' | xargs sed -i '' -e 's/{0}/{1}/g' (Mac)".format(ID, NEWID))
                    flash("git grep -l '{0}' | xargs sed -i 's/{0}/{1}/g' (Linux)".format(ID, NEWID))
                elif k.sed_safety == -1:
                    flash("Knowl rename process started.  This knowl appears in the code (see references below), but cannot trivially be replaced with grep/sed".format(NEWID))
                else:
                    time.sleep(0.01)
                    k.actually_rename()
                    flash("Knowl renamed to {0} successfully.".format(NEWID))
                ID = NEWID
    elif FINISH_RENAME:
        # We need to sleep briefly so that we don't have two identical timestamps
        time.sleep(0.01)
        if FINISH_RENAME == 'finish':
            k.actually_rename()
        elif FINISH_RENAME == 'undo':
            k.undo_rename()
    if k.type == -2:
        return redirect(url_for(".show", ID=k.source))
    else:
        return redirect(url_for(".show", ID=ID))
Ejemplo n.º 2
0
def save_form():
    ID = request.form['id']
    if not ID:
        raise Exception("no id")

    if not allowed_id(ID):
        return redirect(url_for(".index"))

    FINISH_RENAME = request.form.get('finish_rename', '')
    UNDO_RENAME = request.form.get('undo_rename', '')
    if FINISH_RENAME:
        k = Knowl(ID)
        k.actually_rename()
        flash(
            Markup(
                "Renaming complete; the history of %s has been merged into %s"
                % (ID, k.source_name)))
        return redirect(url_for(".show", ID=k.source_name))
    elif UNDO_RENAME:
        k = Knowl(ID)
        k.undo_rename()
        flash(
            Markup(
                "Renaming undone; the history of %s has been merged back into %s"
                % (k.source_name, ID)))
        return redirect(url_for(".show", ID=ID))
    NEWID = request.form.get('krename', '').strip()
    k = Knowl(ID, saving=True, renaming=bool(NEWID))
    new_title = request.form['title']
    new_content = request.form['content']
    who = current_user.get_id()
    if new_title != k.title or new_content != k.content:
        if not k.content and not k.title and k.exists(allow_deleted=True):
            # Creating a new knowl with the same id as one that had previously been deleted
            k.resurrect()
            flash(
                Markup(
                    "Knowl successfully created.  Note that a knowl with this id existed previously but was deleted; its history has been restored."
                ))
        k.title = new_title
        k.content = new_content
        k.timestamp = datetime.now()
        k.status = 0
        k.save(who=who)
    if NEWID:
        if not current_user.is_admin():
            flash_error("You do not have permissions to rename knowl")
        elif not allowed_id(NEWID):
            pass
        else:
            try:
                if k.sed_safety == 0:
                    time.sleep(0.01)
                    k.actually_rename(NEWID)
                    flash(
                        Markup("Knowl renamed to {0} successfully.".format(
                            NEWID)))
                else:
                    k.start_rename(NEWID, who)
            except ValueError as err:
                flash_error(str(err), "error")
            else:
                if k.sed_safety == 1:
                    flash(
                        Markup(
                            "Knowl rename process started. You can change code references using"
                            .format(NEWID)))
                    flash(
                        Markup(
                            "git grep -l '{0}' | xargs sed -i '' -e 's/{0}/{1}/g' (Mac)"
                            .format(ID, NEWID)))
                    flash(
                        Markup(
                            "git grep -l '{0}' | xargs sed -i 's/{0}/{1}/g' (Linux)"
                            .format(ID, NEWID)))
                elif k.sed_safety == -1:
                    flash(
                        Markup(
                            "Knowl rename process started.  This knowl appears in the code (see references below), but cannot trivially be replaced with grep/sed"
                            .format(NEWID)))
                ID = NEWID
    if k.type == -2:
        return redirect(url_for(".show", ID=k.source))
    else:
        return redirect(url_for(".show", ID=ID))
Ejemplo n.º 3
0
def resurrect(ID):
    k = Knowl(ID)
    k.resurrect()
    flash("Knowl %s has been resurrected." % ID)
    return redirect(url_for(".show", ID=ID))
Ejemplo n.º 4
0
def resurrect(ID):
    k = Knowl(ID)
    k.resurrect()
    flash(Markup("Knowl %s has been resurrected." % ID))
    return redirect(url_for(".show", ID=ID))