2020-07-23 17:07:21 +00:00
|
|
|
from flask import render_template, redirect, url_for
|
2020-07-20 14:55:19 +00:00
|
|
|
from tiny0 import app, db
|
2020-07-18 16:40:51 +00:00
|
|
|
from tiny0.forms import URLForm
|
2020-07-20 14:55:19 +00:00
|
|
|
from tiny0.models import URL
|
2020-07-20 16:00:36 +00:00
|
|
|
from tiny0.token import gen_valid_token
|
2020-07-20 16:53:18 +00:00
|
|
|
from tiny0.config import WEBSITE_DOMAIN
|
2020-07-18 13:10:03 +00:00
|
|
|
|
2020-07-20 16:40:15 +00:00
|
|
|
# Index route
|
2020-07-18 16:40:51 +00:00
|
|
|
@app.route("/", methods=['GET', 'POST'])
|
2020-07-18 13:10:03 +00:00
|
|
|
def index():
|
2020-07-19 20:01:03 +00:00
|
|
|
# Create a instance of the form
|
|
|
|
|
form = URLForm()
|
2020-07-18 16:40:51 +00:00
|
|
|
|
2020-07-19 20:01:03 +00:00
|
|
|
# If the form was valid
|
|
|
|
|
if form.validate_on_submit():
|
2020-07-20 16:00:36 +00:00
|
|
|
|
2020-08-17 12:37:00 +03:00
|
|
|
# If the given url is a rick roll
|
|
|
|
|
if ("youtube.com/watch?v=dQw4w9WgXcQ" in form.url.data) or ("youtu.be/dQw4w9WgXcQ" in form.url.data):
|
|
|
|
|
# Generate a valid token
|
|
|
|
|
token = gen_valid_token()
|
2020-07-20 16:00:36 +00:00
|
|
|
|
2020-08-17 12:37:00 +03:00
|
|
|
# Add the token and the given url to the database
|
|
|
|
|
db.session.add(URL(token=token, url=form.url.data))
|
|
|
|
|
db.session.commit()
|
2020-07-18 16:40:51 +00:00
|
|
|
|
2020-08-17 12:37:00 +03:00
|
|
|
# Return the url page with the shortened url
|
|
|
|
|
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + token)
|
|
|
|
|
|
|
|
|
|
# Query the urls that are not a rick roll in the database
|
|
|
|
|
query = URL.query.filter_by(url=form.url.data).first()
|
|
|
|
|
|
|
|
|
|
# If the url exists in the database
|
|
|
|
|
if query:
|
|
|
|
|
# Return the url page with the previously shortened url
|
|
|
|
|
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + query.token)
|
|
|
|
|
|
|
|
|
|
# Else if the url does not exist in the database
|
|
|
|
|
else:
|
|
|
|
|
# Generate a valid token
|
|
|
|
|
token = gen_valid_token()
|
|
|
|
|
|
|
|
|
|
# Add the token and the given url to the database
|
|
|
|
|
db.session.add(URL(token=token, url=form.url.data))
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
# Return the url page with the shortened url
|
|
|
|
|
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + token)
|
|
|
|
|
|
|
|
|
|
# Else if the form was invalid or not submitted
|
2020-07-18 16:40:51 +00:00
|
|
|
else:
|
2020-07-19 20:01:03 +00:00
|
|
|
# Return the index page with the form
|
2020-07-18 16:40:51 +00:00
|
|
|
return render_template("index.html", form=form)
|
2020-07-20 16:40:15 +00:00
|
|
|
|
|
|
|
|
# Shortened url route
|
|
|
|
|
@app.route("/<token>")
|
|
|
|
|
def short_url(token):
|
|
|
|
|
# Query the token in the database
|
|
|
|
|
query = URL.query.filter_by(token=token).first()
|
|
|
|
|
|
|
|
|
|
# If the query response was empty
|
|
|
|
|
if not query:
|
2020-07-20 18:14:55 +00:00
|
|
|
# Return the error page with a 404 not found error
|
2020-08-07 17:28:35 +03:00
|
|
|
return render_template("error.html", error_code=404, error_message="Not Found"), 404
|
2020-07-20 16:40:15 +00:00
|
|
|
|
|
|
|
|
# Else if the query response contained data
|
|
|
|
|
else:
|
|
|
|
|
# Redirect to the url of the token
|
2020-07-20 17:51:52 +00:00
|
|
|
return redirect(query.url)
|
|
|
|
|
|
2020-07-23 18:26:18 +00:00
|
|
|
# Donate route
|
|
|
|
|
@app.route("/donate")
|
|
|
|
|
def donate():
|
|
|
|
|
return render_template("donate.html")
|
2020-07-20 17:51:52 +00:00
|
|
|
|
|
|
|
|
# Error handling routes
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
|
def error_404(error):
|
2020-08-07 17:28:35 +03:00
|
|
|
return render_template("error.html", error_code=404, error_message="Not Found"), 404
|
2020-07-20 17:51:52 +00:00
|
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
|
def error_500(error):
|
2020-08-07 17:28:35 +03:00
|
|
|
return render_template("error.html", error_code=500, error_message="Internal Server Error"), 500
|