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'])
|
||||
def short(shortLink=""):
|
||||
if request.method == "GET":
|
||||
if shortLink:
|
||||
conn = sqlite3.connect("data/links.sqlite")
|
||||
c = conn.cursor()
|
||||
result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone()
|
||||
conn.close()
|
||||
if result:
|
||||
url = result[1]
|
||||
parsedUrl = urlparse(url)
|
||||
|
@ -19,18 +21,17 @@ def short(shortLink=""):
|
|||
url = "http://" + url
|
||||
return redirect(url, code=301) # Redirect to long URL saved in the database
|
||||
else:
|
||||
message = ""
|
||||
if len(shortLink) > 0:
|
||||
message = "Enter long URL for "+ request.url_root + shortLink+":"
|
||||
return render_template("index.html", name=shortLink, message=message, message_type="info") # Does the user wish to create a personel short link?
|
||||
return render_template("index.html", name=shortLink, message="Enter long URL for "+ request.url_root + shortLink+":", message_type="info") # Custom link page
|
||||
else:
|
||||
return render_template("index.html", name=shortLink) # Landing page
|
||||
elif request.method == "POST": # Someone submitted a new link to short
|
||||
wishId = request.form["wishId"]
|
||||
longUrl = request.form["url"]
|
||||
if not wishId:
|
||||
databaseId = insertIdUnique("", longUrl)
|
||||
else:
|
||||
if wishId:
|
||||
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):
|
||||
hashUrl = hashlib.sha256(longUrl.encode()).digest()
|
||||
|
|
Loading…
Reference in a new issue