Skip to content
Snippets Groups Projects
Commit 0c26495a authored by Jonas Leder's avatar Jonas Leder
Browse files

splitted index function into two parts (GET and POST)

parent 9360cd17
No related branches found
No related tags found
No related merge requests found
...@@ -71,37 +71,39 @@ def makeQR(text): #This function is used to create a QR code and encode it base6 ...@@ -71,37 +71,39 @@ def makeQR(text): #This function is used to create a QR code and encode it base6
img.save(buffer, 'jpeg') img.save(buffer, 'jpeg')
return base64.b64encode(buffer.getvalue()).decode() return base64.b64encode(buffer.getvalue()).decode()
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET'])
def home(): def home_get():
if request.method == 'POST': #Post will be executed if the client inserts a new entry return render_template('home.html', builddate=builddate, domain=domain_prepared) #return the default site to create a new shorten link
if (request.form.get('url').replace(" ", "") == ""):
return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="Please enter a url to short, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')]) #return the user the prefilled form with an error message, because no url to short was provided @app.route('/', methods=['POST']) #This function is used to create a new url
if (request.form.get('short').replace(" ", "") == ""): def home_post():
return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="Please enter a short name, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')]) #return the user the prefilled form with an error message, because no short link was provided if (request.form.get('url').replace(" ", "") == ""):
shorturl = (request.form.get('domain') + "/" + request.form.get('short')).lower() return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="Please enter a url to short, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')]) #return the user the prefilled form with an error message, because no url to short was provided
if (request.form.get('short').replace(" ", "") == ""):
url = request.form.get('url') return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="Please enter a short name, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')]) #return the user the prefilled form with an error message, because no short link was provided
with sqlite3.connect('db/urls.db') as conn: #Check if another user already used the short link shorturl = (request.form.get('domain') + "/" + request.form.get('short')).lower()
cursor = conn.cursor()
res = cursor.execute('SELECT LONG_URL FROM WEB_URL WHERE SHORT_URL=?', [shorturl]) url = request.form.get('url')
try: with sqlite3.connect('db/urls.db') as conn: #Check if another user already used the short link
short = res.fetchone() cursor = conn.cursor()
already_used = False res = cursor.execute('SELECT LONG_URL FROM WEB_URL WHERE SHORT_URL=?', [shorturl])
if short is not None: try:
already_used = True short = res.fetchone()
except: already_used = False
pass if short is not None:
already_used = True
if not already_used: #If short link wasn't used before, insert the link in the Database. except:
res = cursor.execute( pass
'INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)',
[url, shorturl]
)
return render_template('home.html', short_url=shorturl, builddate=builddate, domain=domain_prepared, qrcode=makeQR("http://" + shorturl)) #return the shorten link to the user
else:
return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="URL already used, please try another one", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')]) #return the user the prefilled form with an error message, because the url was already used
return render_template('home.html', builddate=builddate, domain=domain_prepared) #If request method is get, return the default site to create a new shorten link
if not already_used: #If short link wasn't used before, insert the link in the Database.
res = cursor.execute(
'INSERT INTO WEB_URL (LONG_URL, SHORT_URL) VALUES (?, ?)',
[url, shorturl]
)
return render_template('home.html', short_url=shorturl, builddate=builddate, domain=domain_prepared, qrcode=makeQR("http://" + shorturl)) #return the shorten link to the user
else:
return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="URL already used, please try another one", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')]) #return the user the prefilled form with an error message, because the url was already used
@app.route('/favicon.ico') #Redirect to the static url of the favicon @app.route('/favicon.ico') #Redirect to the static url of the favicon
def favicon(): def favicon():
return redirect("/static/favicon.ico") return redirect("/static/favicon.ico")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment