示例#1
0
文件: views.py 项目: joealcorn/yl.io
def shorten():
    url = request.form.get('url')

    if url is None:
        error = 'missing required param'
        signals.link_refused.send(g.sender(), error=error)
        return jsonify(error=error), 400
    elif len(url) >= 2000:
        error = 'url too long'
        signals.link_refused.send(g.sender(), error=error)
        return jsonify(error=error), 400

    elif not re.match(url_re, url):
        error = 'invalid url'
        signals.link_refused.send(g.sender(), error=error)
        return jsonify(error=error), 400

    elif canonical_url(url) in app.config['DOMAIN_BLACKLIST']:
        error = 'blacklisted domain'
        signals.link_refused.send(g.sender(), error=error)
        return jsonify(error=error), 403

    id36 = Links.new(url, request.remote_addr)
    if id36 is None:
        return jsonify(error='server error'), 500

    short_url = url_for('shortened', id=id36, _external=True)
    return jsonify(url=short_url)
示例#2
0
文件: views.py 项目: joealcorn/yl.io
def shortened(id):
    link = Links.get(id)

    if link is None:
        abort(404)
    elif not link['active']:
        signals.disabled_link_visited.send(g.sender(), request=request)
        return render_template('disabled.html'), 410

    signals.link_visited.send(g.sender(), request=request)
    return redirect(link['target']), 301
示例#3
0
文件: views.py 项目: joealcorn/yl.io
def not_found(error):
    signals.not_found.send(g.sender(), request=request)
    return render_template('404.html'), 404
示例#4
0
文件: views.py 项目: joealcorn/yl.io
def server_error(error):
    signals.server_error.send(g.sender(), request=request)
    return render_template('500.html'), 500