Some code cleanups
This commit is contained in:
parent
99b747d4ba
commit
97b906730b
17
schort.py
17
schort.py
|
@ -9,9 +9,11 @@ app = Flask(__name__)
|
||||||
@app.route('/<shortLink>', methods=['GET', 'POST'])
|
@app.route('/<shortLink>', methods=['GET', 'POST'])
|
||||||
def short(shortLink=""):
|
def short(shortLink=""):
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
|
if shortLink:
|
||||||
conn = sqlite3.connect("data/links.sqlite")
|
conn = sqlite3.connect("data/links.sqlite")
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone()
|
result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone()
|
||||||
|
conn.close()
|
||||||
if result:
|
if result:
|
||||||
url = result[1]
|
url = result[1]
|
||||||
parsedUrl = urlparse(url)
|
parsedUrl = urlparse(url)
|
||||||
|
@ -19,18 +21,17 @@ def short(shortLink=""):
|
||||||
url = "http://" + url
|
url = "http://" + url
|
||||||
return redirect(url, code=301) # Redirect to long URL saved in the database
|
return redirect(url, code=301) # Redirect to long URL saved in the database
|
||||||
else:
|
else:
|
||||||
message = ""
|
return render_template("index.html", name=shortLink, message="Enter long URL for "+ request.url_root + shortLink+":", message_type="info") # Custom link page
|
||||||
if len(shortLink) > 0:
|
else:
|
||||||
message = "Enter long URL for "+ request.url_root + shortLink+":"
|
return render_template("index.html", name=shortLink) # Landing page
|
||||||
return render_template("index.html", name=shortLink, message=message, message_type="info") # Does the user wish to create a personel short link?
|
|
||||||
elif request.method == "POST": # Someone submitted a new link to short
|
elif request.method == "POST": # Someone submitted a new link to short
|
||||||
wishId = request.form["wishId"]
|
wishId = request.form["wishId"]
|
||||||
longUrl = request.form["url"]
|
longUrl = request.form["url"]
|
||||||
if not wishId:
|
if wishId:
|
||||||
databaseId = insertIdUnique("", longUrl)
|
|
||||||
else:
|
|
||||||
databaseId = insertIdUnique(wishId, longUrl)
|
databaseId = insertIdUnique(wishId, longUrl)
|
||||||
return request.url_root + databaseId # TODO: Give the user a nice site where he can see his short URL
|
else:
|
||||||
|
databaseId = insertIdUnique("", longUrl)
|
||||||
|
return request.url_root + databaseId # Short link in plain text
|
||||||
|
|
||||||
def insertIdUnique(idToCheck, longUrl):
|
def insertIdUnique(idToCheck, longUrl):
|
||||||
hashUrl = hashlib.sha256(longUrl.encode()).digest()
|
hashUrl = hashlib.sha256(longUrl.encode()).digest()
|
||||||
|
|
Loading…
Reference in a new issue