Files
flask_url_shortener/tiny0/routes.py
T

134 lines
3.8 KiB
Python
Raw Normal View History

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-08-25 13:02:48 +03:00
from tiny0.forms import URLForm, ShortURLForm, ReportForm
from tiny0.models import URL, Reports
2020-07-20 16:00:36 +00:00
from tiny0.token import gen_valid_token
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():
# Create a instance of the form
form = URLForm()
2020-07-18 16:40:51 +00:00
# If the form was valid
if form.validate_on_submit():
2020-07-20 16:00:36 +00:00
2020-08-21 12:17:17 +03:00
# If a token was given
if form.token.data:
# Add the token and the given url to the database
db.session.add(URL(token=form.token.data, url=form.url.data))
db.session.commit()
2020-07-20 16:00:36 +00:00
2020-08-21 12:17:17 +03:00
# Return the url page with the shortened url
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + form.token.data)
# Else if a token was not given
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)
2020-07-18 16:40:51 +00:00
2020-08-22 15:36:09 +03:00
# Else if the form was invalid or not submitted
2020-07-18 16:40:51 +00:00
else:
# 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:
# Return the error page with a 404 not found error
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:
2020-08-22 15:36:09 +03:00
# Addd one to the clicks of the shortened url
query.clicks += 1
db.session.commit()
2020-07-20 16:40:15 +00:00
# Redirect to the url of the token
2020-07-20 17:51:52 +00:00
return redirect(query.url)
2020-08-25 12:11:00 +03:00
# Click tracker route
2020-08-22 15:36:09 +03:00
@app.route("/tracker", methods=['GET', 'POST'])
def tracker():
# Create a instance of the form
2020-08-25 12:11:00 +03:00
form = ShortURLForm()
2020-08-22 15:36:09 +03:00
# If the form was valid
if form.validate_on_submit():
# Get the clicks of the given token
clicks = URL.query.filter_by(token=form.url.data).first().clicks
# Return the clicks page with the clicks of that token
return render_template("clicks.html", clicks=clicks)
# Else if the form was invalid or not submitted
else:
# Return the tracker page with the form
return render_template("tracker.html", form=form)
2020-08-25 12:11:00 +03:00
# url lookup route
@app.route("/lookup", methods=['GET', 'POST'])
def lookup():
# Create a instance of the form
form = ShortURLForm()
# If the form was valid
if form.validate_on_submit():
# Get the original url of the given token
url = URL.query.filter_by(token=form.url.data).first().url
# Return the original url page with the url
return render_template("original-url.html", url=url)
# Else if the form was invalid or not submitted
else:
2020-08-25 13:02:48 +03:00
# Return the lookup page with the form
2020-08-25 12:11:00 +03:00
return render_template("lookup.html", form=form)
2020-08-25 13:02:48 +03:00
# url report route
@app.route("/report", methods=['GET', 'POST'])
def report():
# Create a instance of the form
form = ReportForm()
# If the form was valid
if form.validate_on_submit():
# Add the report to the database
db.session.add(Reports(token=form.url.data, message=form.message.data))
db.session.commit()
# Return the thanks page
return render_template("thanks.html")
# Else if the form was invalid or not submitted
else:
# Return the report page with the form
return render_template("report.html", form=form)
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):
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):
return render_template("error.html", error_code=500, error_message="Internal Server Error"), 500