Exemple #1
0
def index():
    urls = Url.query.all()
    form = URLForm()
    if form.validate_on_submit():
        full_url = form.full_url.data
        short_url = form.short_url.data
        if Url.short_url_exists(short_url):
            flash("Short url is already exist. Try another one.")
            return redirect(url_for("main.index"))
        new_url = Url(full_url=full_url, clicks=0, created=datetime.today())
        element_text = new_url.check_url()
        if not element_text:
            flash("Check url!!! Failed request %s" % new_url.full_url)
            return redirect(url_for("main.index"))
        new_url.element_text = element_text
        db.session.add(new_url)
        db.session.commit()
        new_url.store_short_url(short_url)
        flash("Your url has been created successfully.")
        return redirect(url_for("main.index"))

    return render_template("index.html", urls=urls, form=form)