def index(): form = get_shrink_form() surl = None render = lambda: render_template("index.html", form=form, url=surl) if form.validate_on_submit(): if form.custom.data and url.exists(form.custom.data): _flash("Sorry, that custom URL already exists.", "error") return render() url_ = url.add( form.url.data, statistics=form.statistics.data, burn=form.burn.data, short=form.custom.data if form.custom.data else None, ua=request.user_agent.string, ip=request.remote_addr ) if url_: _flash("Successfully shrunk your URL!", "success") surl = { "long": form.url.data, "short": url_for("reroute", short=url_[0], _external=True), "stats": url_for("stats", short=url_[0], _external=True), "delete": url_for("delete", short=url_[0], key=url_[1], _external=True), "burn": form.burn.data, } form.url.data = form.custom.data = "" form.statistics.data = True form.burn.data = False return render()
def delete(short, key): empty_or_redir = lambda: {} if g.is_api_req else redirect(url_for("index")) if not url.exists(short) or key != url.delete_key(short): flash("URL doesn't exist or deletion key is incorrect.", "error") return empty_or_redir() if not url.remove(short): flash("Something dun goofed! Sorry.", "error") return empty_or_redir() flash("Successfully deleted {0}!".format(url_for("reroute", short=short, _external=True)), "success") return empty_or_redir()
def reroute(short): # redirect() would clash with flask.redirect if not url.exists(short): return redirect(url_for("index")) long_ = url.long(short) url.hit(short, ua=request.user_agent.string, ip=request.remote_addr) return redirect(long_)