Added a original url lookup

This commit is contained in:
xemeds
2020-08-25 12:11:00 +03:00
parent a138e24d79
commit e5e9b7a5f4
5 changed files with 59 additions and 5 deletions
+1 -1
View File
@@ -102,7 +102,7 @@ class URLForm(FlaskForm):
submit = SubmitField("Shorten") 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]) 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") submit = SubmitField("Track")
+22 -3
View File
@@ -1,6 +1,6 @@
from flask import render_template, redirect, url_for from flask import render_template, redirect, url_for
from tiny0 import app, db from tiny0 import app, db
from tiny0.forms import URLForm, TrackerForm from tiny0.forms import URLForm, ShortURLForm
from tiny0.models import URL from tiny0.models import URL
from tiny0.token import gen_valid_token from tiny0.token import gen_valid_token
from tiny0.config import WEBSITE_DOMAIN from tiny0.config import WEBSITE_DOMAIN
@@ -60,11 +60,11 @@ def short_url(token):
# Redirect to the url of the token # Redirect to the url of the token
return redirect(query.url) return redirect(query.url)
# Click Tracker route # Click tracker route
@app.route("/tracker", methods=['GET', 'POST']) @app.route("/tracker", methods=['GET', 'POST'])
def tracker(): def tracker():
# Create a instance of the form # Create a instance of the form
form = TrackerForm() form = ShortURLForm()
# If the form was valid # If the form was valid
if form.validate_on_submit(): if form.validate_on_submit():
@@ -79,6 +79,25 @@ def tracker():
# Return the tracker page with the form # Return the tracker page with the form
return render_template("tracker.html", form=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 # Donate route
@app.route("/donate") @app.route("/donate")
def donate(): def donate():
+1 -1
View File
@@ -19,7 +19,7 @@
<li class="logo">tiny0 - URL Shortener</li> <li class="logo">tiny0 - URL Shortener</li>
<li class="items"><a href="{{ url_for('index') }}">Shortener</a></li> <li class="items"><a href="{{ url_for('index') }}">Shortener</a></li>
<li class="items"><a href="{{ url_for('tracker') }}">Tracker</a></li> <li class="items"><a href="{{ url_for('tracker') }}">Tracker</a></li>
<li class="items"><a href="#">Lookup</a></li> <li class="items"><a href="{{ url_for('lookup') }}">Lookup</a></li>
<li class="items"><a href="#">Report</a></li> <li class="items"><a href="#">Report</a></li>
<li class="items"><a href="{{ url_for('donate') }}">Donate</a></li> <li class="items"><a href="{{ url_for('donate') }}">Donate</a></li>
<li class="btn"><a href="#"><i class="fas fa-bars"></i></a></li> <li class="btn"><a href="#"><i class="fas fa-bars"></i></a></li>
+17
View File
@@ -0,0 +1,17 @@
{% extends "layout.html" %}
{% block body %}
<form method="POST" action="" class="url-form">
<h1>&#60 URL Lookup &#62</h1>
{{ form.hidden_tag() }}
{{ form.url(placeholder="Enter the short URL here", autofocus=true, class="feedback-input") }}
{{ form.submit(class="button") }}
{% if form.errors %}
<div class="form-errors">
{% for error in form.url.errors %}
<h4>{{ error }}</h4>
{% endfor %}
</div>
{% endif %}
</form>
{% endblock %}
+18
View File
@@ -0,0 +1,18 @@
{% extends "layout.html" %}
{% block body %}
<div class="url-form">
<h1>&#60 Original URL &#62</h1>
<input type="text" value="{{ url }}" id="url" class="feedback-input" readonly>
<button onclick="copyURL()" class="button">Copy</button>
</div>
{% endblock %}
{% block script %}
function copyURL() {
var url = document.getElementById("url");
url.select();
url.setSelectionRange(0, 99999)
document.execCommand("copy");
}
{% endblock %}