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 after_request(res): if not g.is_api_req: # Flash all our flashes map(lambda f: _flash(*f), g.flashes) if not hasattr(res, "_ret_dict") or res._ret_dict is None: # Too specific for us, let's not screw around. return res res = res._ret_dict errors = filter(lambda f: len(f) >= 2 and f[1] == "error", g.flashes) if g.is_api_req: res["error"] = bool(errors) # Backwards compat.. Sort of? res.setdefault("message", "Check `messages` instead, buddy.") res["messages"] = map(lambda f: f[0], g.flashes) # Not interesting to API consumers. res.pop("_template", None) return jsonify(res) return make_response(render_template(res["_template"], res=res))
def __call__(self, message: str, level: str = 'info') -> None: _flash(message, level)
def __call__(self, errors, category): if not isinstance(errors, list): errors = [errors] for msg in errors: _flash(msg, category)
def flash(message, category='info'): _flash(message, category)