Went back to each url generating a new token even if they already exist in the database
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@ from tiny0 import db
|
|||||||
class URL(db.Model):
|
class URL(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
token = db.Column(db.String(6), index=True, unique=True, nullable=False)
|
token = db.Column(db.String(6), index=True, unique=True, nullable=False)
|
||||||
url = db.Column(db.String(2000), index=True, unique=True, nullable=False)
|
url = db.Column(db.String(2000), nullable=False)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"'{self.id}' '{self.token}' '{self.url}'"
|
return f"'{self.id}' '{self.token}' '{self.url}'"
|
||||||
|
|||||||
+1
-24
@@ -13,9 +13,6 @@ def index():
|
|||||||
|
|
||||||
# If the form was valid
|
# If the form was valid
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
|
|
||||||
# If the given url is a rick roll
|
|
||||||
if ("youtube.com/watch?v=dQw4w9WgXcQ" in form.url.data) or ("youtu.be/dQw4w9WgXcQ" in form.url.data):
|
|
||||||
# Generate a valid token
|
# Generate a valid token
|
||||||
token = gen_valid_token()
|
token = gen_valid_token()
|
||||||
|
|
||||||
@@ -26,27 +23,7 @@ def index():
|
|||||||
# Return the url page with the shortened url
|
# Return the url page with the shortened url
|
||||||
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + token)
|
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + token)
|
||||||
|
|
||||||
# Query the urls that are not a rick roll in the database
|
# If the form was invalid or not submitted
|
||||||
query = URL.query.filter_by(url=form.url.data).first()
|
|
||||||
|
|
||||||
# If the url exists in the database
|
|
||||||
if query:
|
|
||||||
# Return the url page with the previously shortened url
|
|
||||||
return render_template("url.html", url=WEBSITE_DOMAIN + "/" + query.token)
|
|
||||||
|
|
||||||
# Else if the url does not exist in the database
|
|
||||||
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)
|
|
||||||
|
|
||||||
# Else if the form was invalid or not submitted
|
|
||||||
else:
|
else:
|
||||||
# Return the index page with the form
|
# Return the index page with the form
|
||||||
return render_template("index.html", form=form)
|
return render_template("index.html", form=form)
|
||||||
|
|||||||
Reference in New Issue
Block a user