Пример #1
0
def add():
    form = AddForm(request.form)
    if request.method != "POST" or not form.validate():
        return render_template("admin_add.html", add_form=form)

    shortcode = form.shortcode.data
    target_url = form.target_url.data
    random = form.is_random.data

    if (not shortcode or len(shortcode) == 0) and not random:
        return abort("No shortcode specified.", 400)

    if random:
        # Make sure the target doesn't already have a random shortcode
        target = database_helper.find_by_target(target_url)
        if target and target.is_random:
            flash("Shortcode '%s' for this URL already exists. %s" % (target.shortcode, url_for_code(target.shortcode, target.target_url)), category="info")
            return render_template("admin_add.html", add_form=form)

        # Find an unused random shortcode
        count = 0
        while True:
            shortcode = get_random_shortcode(current_app.config["SHORTCODE_LENGTH"])
            if database_helper.get_shortcode_target(shortcode) is None:
                break
                
            # Make sure we don't loop endlessly
            count = count + 1
            if count > 4:
                flash("Could not find usable shortcode after 5 tries.", category="danger")
                return render_template("admin_add.html", add_form=form)
    else:
        # Make sure this shortcode doesn't already exist
        target = database_helper.get_shortcode_target(shortcode)
        if target:
            flash("Shortcode '%s' already exists to %s." % (shortcode, target.target_url), category="warning")
            return render_template("admin_add.html", add_form=form)

    if database_helper.insert_shortcode(shortcode, target_url, random):
        msg = "Shortcode '%s' added successfully. %s" % (shortcode, url_for_code(shortcode, target_url))
        category = "success"
    else:
        msg = "Failed to create shortcode."
        category = "danger"

    flash(msg, category)
    return redirect(url_for(".add"))
Пример #2
0
def delete(shortcode):
    result = database_helper.get_shortcode_target(shortcode)
    if result is None:
        return abort(404)

    if request.method != "POST":
        return render_template("admin_delete.html", data=result)

    if database_helper.delete_shortcode(shortcode):
        msg = "Shortcode '%s' was deleted." % (shortcode)
        category = "success"
    else:
        msg = "Failed to remove shortcode."
        category = "warn"

    flash(msg, category=category)
    return redirect(url_for(".list"))
Пример #3
0
def target(shortcode):
    is_view = False
    if shortcode[-1:] == "+":
        shortcode = shortcode[:-1]
        is_view = True

    result = database_helper.get_shortcode_target(shortcode)
    if result == None:
        return abort(404)

    if is_view:
        return render_template("index_view.html", shortcode=shortcode, data=result, sitename=current_app.config["SITENAME"])

    if result.is_random:
        code = 301
    else:
        code = 307
    
    return redirect(result.target_url, code)
Пример #4
0
def edit(shortcode):
    result = database_helper.get_shortcode_target(shortcode)
    if result is None:
        return abort(404)

    form = AddForm(request.form, obj=result)
    if request.method != "POST" or not form.validate():
        return render_template("admin_edit.html", add_form=form, data=result)

    target_url = form.target_url.data

    if database_helper.update_shortcode(shortcode, target_url):
        msg = "Shortcode '%s' updated successfully. %s" % (shortcode, url_for_code(shortcode, target_url))
        category = "success"
    else:
        msg = "Failed to update shortcode."
        category = "danger"

    flash(msg, category)
    return redirect(url_for(".list"))