Esempio 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))
Esempio n. 2
0
def show(ID):
    timestamp = request.args.get('timestamp')
    if timestamp is not None:
        timestamp = timestamp_in_ms_to_datetime(timestamp)
    k = Knowl(ID, timestamp=timestamp, showing=True)
    if k.exists():
        r = render_knowl(ID, footer="0", raw=True)
        title = k.title or "'%s'" % k.id
        if not is_beta():
            if k.status == 0:
                title += " (awaiting review)"
            else:
                title += " (reviewed)"
    else:
        if current_user.is_admin() and k.exists(allow_deleted=True):
            k = Knowl(ID, showing=True, allow_deleted=True)
            r = render_knowl(ID, footer="0", raw=True, allow_deleted=True)
            title = (k.title or "'%s'" % k.id) + " (DELETED)"
        else:
            return abort(404, "No knowl found with the given id")
    for elt in k.edit_history:
        # We will be printing these within a javascript ` ` string
        # so need to escape backticks
        elt['content'] = json.dumps(elt['content'])
    # Modify the comments list to add information on whether this user can delete
    if k.type != -2:
        for i, (cid, author, timestamp) in enumerate(k.comments):
            can_delete = (current_user.is_admin()
                          or current_user.get_id() == author)
            author_name = userdb.lookup(author)["full_name"]
            k.comments[i] = (cid, author_name, timestamp, can_delete)
    b = get_bread([(k.category, url_for('.index', category=k.category)),
                   ('%s' % title, url_for('.show', ID=ID))])

    return render_template(u"knowl-show.html",
                           title=title,
                           k=k,
                           cur_username=current_user.get_id(),
                           render=r,
                           bread=b)
Esempio n. 3
0
File: main.py Progetto: LMFDB/lmfdb
def show(ID):
    timestamp = request.args.get('timestamp')
    if timestamp is not None:
        timestamp = timestamp_in_ms_to_datetime(timestamp)
    k = Knowl(ID, timestamp=timestamp, showing=True)
    if k.exists():
        r = render_knowl(ID, footer="0", raw=True)
        title = k.title or "'%s'" % k.id
        if not is_beta():
            if k.status == 0:
                title += " (awaiting review)"
            else:
                title += " (reviewed)"
    else:
        if current_user.is_admin() and k.exists(allow_deleted=True):
            k = Knowl(ID, showing=True, allow_deleted=True)
            r = render_knowl(ID, footer="0", raw=True, allow_deleted=True)
            title = (k.title or "'%s'" % k.id) + " (DELETED)"
        else:
            return abort(404, "No knowl found with the given id")
    for elt in k.edit_history:
        # We will be printing these within a javascript ` ` string
        # so need to escape backticks
        elt['content'] = json.dumps(elt['content'])
    # Modify the comments list to add information on whether this user can delete
    if k.type != -2:
        for i, (cid, author, timestamp) in enumerate(k.comments):
            can_delete = (current_user.is_admin() or current_user.get_id() == author)
            author_name = userdb.lookup(author)["full_name"]
            k.comments[i] = (cid, author_name, timestamp, can_delete)
    b = get_bread([(k.category, url_for('.index', category=k.category)), ('%s' % title, url_for('.show', ID=ID))])

    return render_template(u"knowl-show.html",
                           title=title,
                           k=k,
                           cur_username=current_user.get_id(),
                           render=r,
                           bread=b)
Esempio n. 4
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))