-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (54 loc) · 1.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask ,redirect,render_template,request,url_for
from shorten import createid
from flask_sqlalchemy import SQLAlchemy
import re
app=Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']= False
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///db.sqlite3'
db = SQLAlchemy(app)
#### handeling errors
# @app.errorhandler(404)
# def page_not_found(e):
# # note that we set the 404 status explicitly
# return render_template('404.html'), 404
# @app.errorhandler(500)
# def internal_server_error(e):
# # note that we set the 500 status explicitly
# return render_template('500.html'), 500
# ###############################
#Creating the database
class LINKS(db.Model):
id = db.Column(db.Integer,primary_key=True)
link= db.Column(db.String(1000))
linkid = db.Column(db.String(6),unique=True)
######################################
def addnewlink(url):
id =createid()
if str(LINKS.query.filter_by(linkid=id).first()) != 'None':
while str(LINKS.query.filter_by(linkid=id).first()) != 'None' :
id =createid()
newlink = LINKS(link=url,linkid=id)
db.session.add(newlink)
db.session.commit()
return id
@app.route('/',defaults={'url': 'URL SHORTNER'})
def home(url):
return render_template('home.html',url=url)
@app.route('/<id>')
def redirectto(id):
link= LINKS.query.filter_by(linkid=id).first()
return redirect(link.link)
@app.route('/shorten',methods=['POST','GET'])
def shorten():
x=request.form.get('link')
if re.search('https?://\w+.+',x) != None:
print(x+' is a link')
x=addnewlink(x)
message='SUCCESS Your Link is ' + "http://127.0.0.1:5000" + url_for("home")+x
return render_template('home.html',url=message)
else:
message= 'SORRY Not a Link Try Again'
return render_template('home.html',url=message)
if __name__ == "__main__":
app.run(debug=True)
db.create_all()