Added the token generator
This commit is contained in:
Binary file not shown.
+10
-2
@@ -2,7 +2,7 @@ from flask import render_template, redirect, request, url_for
|
|||||||
from tiny0 import app, db
|
from tiny0 import app, db
|
||||||
from tiny0.forms import URLForm
|
from tiny0.forms import URLForm
|
||||||
from tiny0.models import URL
|
from tiny0.models import URL
|
||||||
#from token import gen_valid_token
|
from tiny0.token import gen_valid_token
|
||||||
|
|
||||||
# Index Page
|
# Index Page
|
||||||
@app.route("/", methods=['GET', 'POST'])
|
@app.route("/", methods=['GET', 'POST'])
|
||||||
@@ -12,7 +12,15 @@ def index():
|
|||||||
|
|
||||||
# If the form was valid
|
# If the form was valid
|
||||||
if form.validate_on_submit():
|
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
|
# If the form was invalid or not submitted
|
||||||
else:
|
else:
|
||||||
|
|||||||
+15
-4
@@ -1,8 +1,19 @@
|
|||||||
#from tiny0.models import URL
|
from tiny0.models import URL
|
||||||
from secrets import choice
|
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
|
||||||
|
|
||||||
'''
|
|
||||||
|
|||||||
Reference in New Issue
Block a user