ifrequest.method=='POST':#Post will be executed if the client inserts a new entry
returnrender_template('home.html',builddate=builddate,domain=domain_prepared)#return the default site to create a new shorten link
if (request.form.get('url').replace("","")==""):
returnrender_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("","")==""):
defhome_post():
returnrender_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
returnrender_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')
returnrender_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
withsqlite3.connect('db/urls.db')asconn:#Check if another user already used the short link
res=cursor.execute('SELECT LONG_URL FROM WEB_URL WHERE SHORT_URL=?',[shorturl])
url=request.form.get('url')
try:
withsqlite3.connect('db/urls.db')asconn:#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])
ifshortisnotNone:
try:
already_used=True
short=res.fetchone()
except:
already_used=False
pass
ifshortisnotNone:
already_used=True
ifnotalready_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]
)
returnrender_template('home.html',short_url=shorturl,builddate=builddate,domain=domain_prepared,qrcode=makeQR("http://"+shorturl))#return the shorten link to the user
else:
returnrender_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
returnrender_template('home.html',builddate=builddate,domain=domain_prepared)#If request method is get, return the default site to create a new shorten link
ifnotalready_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]
)
returnrender_template('home.html',short_url=shorturl,builddate=builddate,domain=domain_prepared,qrcode=makeQR("http://"+shorturl))#return the shorten link to the user
else:
returnrender_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