forked from CMPN-CODECELL/URL-Shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
168 lines (153 loc) · 5.21 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, render_template,request,session,redirect,url_for,g
from pyshorteners import *
import mysql.connector
shortener = Shortener()
app = Flask(__name__)
app.secret_key="japneet"
@app.route('/')
def index():
return render_template('pages/urlShortener.html')
@app.route('/beforeRegister')
def beforeLogin():
return render_template('pages/login.html')
@app.route('/login',methods=['POST','GET'])
def login():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
if request.method=='POST':
signup=request.form
name = signup['name']
username = signup['username']
email = signup['email']
passw = signup['pass']
rpassw = signup['repass']
# mycursor.execute("select * from users where Username='"+username+"'")
# count=mycursor.rowcount
# if count ==1:
# return "A user already exists with this username."
# else:
mycursor.execute("insert into users values(%s,%s,%s,%s,%s)",(name,username,email,passw,rpassw))
mydb.commit()
return render_template('pages/urlShortener.html')
else:
return render_template('pages/urlShortener.html')
mycursor.close()
@app.route('/urlShortener',methods=['POST','GET'])
def urlShortener():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
if request.method=='POST':
signnup=request.form
username = signnup['Username']
session['username']=username
passw = signnup['Password']
# mycursor.execute("TRUNCATE TABLE link")
mycursor.execute("select * from users where Username='"+username+"' and Password='"+passw+"'")
r=mycursor.fetchall()
count=mycursor.rowcount
if count ==1:
return render_template('pages/url.html')
else:
return "You are not a member please <a href='/beforeRegister'>register</a>"
else:
return render_template('pages/url.html')
mydb.commit()
mycursor.close()
@app.route('/url',methods=['POST','GET'])
def url():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
if request.method=='POST':
result=request.form.to_dict()
urll = result['Url']
x = shortener.tinyurl.short(urll)
result['shortLink']=x
shortLink = x
longLink=urll
username = session['username']
linkCode = result['linkCode']
mycursor.execute("insert into link values(%s,%s,%s,%s)",(longLink,shortLink,username,linkCode))
mydb.commit()
mycursor.close()
return render_template('pages/urlResult.html',result=result)
@app.route('/history')
def history():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
mycursor.execute("select * from link")
r=mycursor.fetchall()
username = session['username']
len1 = len(r)
return render_template('pages/history.html',r=r,len1=len1,username=username)
@app.route('/beforeEditProfile')
def beforeEditProfile():
return render_template('pages/editProfile.html')
@app.route('/profile')
def profile():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
username = session['username']
mycursor.execute("select * from users where Username='"+username+"'")
r=mycursor.fetchone()
mydb.commit()
mycursor.close()
return render_template('pages/profile.html',r=r,username=username)
@app.route('/editProfile',methods=['POST','GET'])
def editProfile():
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="",
database="username"
)
mycursor = mydb.cursor()
usernamee = session['username']
mycursor.execute("select * from users where Username='"+usernamee+"'")
r=mycursor.fetchone()
if request.method=='POST':
signup=request.form
name = signup['name']
username = signup['username']
email = signup['email']
oldPassw = signup['oldPass']
newPassw = signup['newPass']
rpassw = signup['repass']
if oldPassw==newPassw:
if oldPassw == r[3]:
mycursor.execute("UPDATE `users` SET `Name`=%s,`Username`=%s,`Email`=%s,`Password`=%s,`ConfirmPassword`=%s WHERE Username=%s",(name,username,email,newPassw,rpassw,usernamee))
mydb.commit()
return render_template('pages/profile.html',r=r,username=username)
session['username']=usernamee
else:
return "<h1>Old Password entered is incorrect.</h1>"
else:
return "<h1>Passwords don't match.</h1>"
return render_template('pages/profile.html',r=r,username=username)
if __name__ == "__main__":
from waitress import serve
serve(app, host="127.0.0.1", port=5000)