From 33e29853dd52df2a5a5f65fb37f0f824a4f5cfea Mon Sep 17 00:00:00 2001 From: xemeds Date: Mon, 20 Jul 2020 16:00:36 +0000 Subject: [PATCH] Added the token generator --- tiny0/database.db | Bin 12288 -> 12288 bytes tiny0/routes.py | 12 ++++++++++-- tiny0/token.py | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/tiny0/database.db b/tiny0/database.db index d19529601034b1b3d61c20de1be7a869f484156c..eb18bb559f400bfb82f2ac20a632711878e02e07 100644 GIT binary patch delta 33 gcmZojXh@hKEhxsoz`zW|Fd#Tl$Cy!UW5NP`09`!=jsO4v delta 33 gcmZojXh@hKEy%>cz`zW|Fu*iX$C#06W5NP`09rc*R{#J2 diff --git a/tiny0/routes.py b/tiny0/routes.py index 0f7bb1b..e21cbfd 100644 --- a/tiny0/routes.py +++ b/tiny0/routes.py @@ -2,7 +2,7 @@ from flask import render_template, redirect, request, url_for from tiny0 import app, db from tiny0.forms import URLForm from tiny0.models import URL -#from token import gen_valid_token +from tiny0.token import gen_valid_token # Index Page @app.route("/", methods=['GET', 'POST']) @@ -12,7 +12,15 @@ def index(): # If the form was valid if form.validate_on_submit(): - return render_template("url.html", url=form.url.data) + # 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="127.0.0.1:5000/" + token) # If the form was invalid or not submitted else: diff --git a/tiny0/token.py b/tiny0/token.py index 29420ab..a56921c 100644 --- a/tiny0/token.py +++ b/tiny0/token.py @@ -1,8 +1,19 @@ -#from tiny0.models import URL +from tiny0.models import URL from secrets import choice -''' +# The characters used to generate the token +token_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz_-" -Generate valid token +def gen_valid_token(): + while True: + # Generate a token + token = "".join(choice(token_characters) for i in range(8)) + + # If the token does not exists in the database + if not URL.query.filter_by(token=token).first(): + # Break the loop + break + + # Return the token + return token -'''