From e5e9b7a5f444cc65f3a3456e05af65bc5e7ddb13 Mon Sep 17 00:00:00 2001 From: xemeds Date: Tue, 25 Aug 2020 12:11:00 +0300 Subject: [PATCH] Added a original url lookup --- tiny0/forms.py | 2 +- tiny0/routes.py | 25 ++++++++++++++++++++++--- tiny0/templates/layout.html | 2 +- tiny0/templates/lookup.html | 17 +++++++++++++++++ tiny0/templates/original-url.html | 18 ++++++++++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 tiny0/templates/lookup.html create mode 100644 tiny0/templates/original-url.html diff --git a/tiny0/forms.py b/tiny0/forms.py index 2714c7b..797c675 100644 --- a/tiny0/forms.py +++ b/tiny0/forms.py @@ -102,7 +102,7 @@ class URLForm(FlaskForm): submit = SubmitField("Shorten") -class TrackerForm(FlaskForm): +class ShortURLForm(FlaskForm): url = StringField(validators=[DataRequired(), Length(min=len(WEBSITE_DOMAIN) + 7, max=len(WEBSITE_DOMAIN) + 25, message="Invalid short URL"), validate_short_URL]) submit = SubmitField("Track") diff --git a/tiny0/routes.py b/tiny0/routes.py index 445108e..eb56775 100644 --- a/tiny0/routes.py +++ b/tiny0/routes.py @@ -1,6 +1,6 @@ from flask import render_template, redirect, url_for from tiny0 import app, db -from tiny0.forms import URLForm, TrackerForm +from tiny0.forms import URLForm, ShortURLForm from tiny0.models import URL from tiny0.token import gen_valid_token from tiny0.config import WEBSITE_DOMAIN @@ -60,11 +60,11 @@ def short_url(token): # Redirect to the url of the token return redirect(query.url) -# Click Tracker route +# Click tracker route @app.route("/tracker", methods=['GET', 'POST']) def tracker(): # Create a instance of the form - form = TrackerForm() + form = ShortURLForm() # If the form was valid if form.validate_on_submit(): @@ -79,6 +79,25 @@ def tracker(): # Return the tracker page with the form return render_template("tracker.html", form=form) +# 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: + # Return the tracker page with the form + return render_template("lookup.html", form=form) + # Donate route @app.route("/donate") def donate(): diff --git a/tiny0/templates/layout.html b/tiny0/templates/layout.html index 8400c83..a96c82f 100644 --- a/tiny0/templates/layout.html +++ b/tiny0/templates/layout.html @@ -19,7 +19,7 @@
  • Shortener
  • Tracker
  • -
  • Lookup
  • +
  • Lookup
  • Report
  • Donate
  • diff --git a/tiny0/templates/lookup.html b/tiny0/templates/lookup.html new file mode 100644 index 0000000..fb08c64 --- /dev/null +++ b/tiny0/templates/lookup.html @@ -0,0 +1,17 @@ +{% extends "layout.html" %} + +{% block body %} +
    +

    < URL Lookup >

    + {{ form.hidden_tag() }} + {{ form.url(placeholder="Enter the short URL here", autofocus=true, class="feedback-input") }} + {{ form.submit(class="button") }} + {% if form.errors %} +
    + {% for error in form.url.errors %} +

    {{ error }}

    + {% endfor %} +
    + {% endif %} +
    +{% endblock %} diff --git a/tiny0/templates/original-url.html b/tiny0/templates/original-url.html new file mode 100644 index 0000000..e0b39d1 --- /dev/null +++ b/tiny0/templates/original-url.html @@ -0,0 +1,18 @@ +{% extends "layout.html" %} + +{% block body %} +
    +

    < Original URL >

    + + +
    +{% endblock %} + +{% block script %} +function copyURL() { + var url = document.getElementById("url"); + url.select(); + url.setSelectionRange(0, 99999) + document.execCommand("copy"); +} +{% endblock %}