-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackendpy.py
484 lines (427 loc) · 13.4 KB
/
backendpy.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import json
import mysql.connector
from flask import request, redirect
from flask import Flask, render_template
mydb=mysql.connector.connect(
host="localhost",
user="root",
password="Samaya#9421",
database="swarnesh"
)
class TrieNode:
"""A node in the trie structure"""
def __init__(self, char):
# the character stored in this node
self.char = char
# whether this can be the end of a word
self.is_end = False
# a counter indicating how many times a word is inserted
# (if this node's is_end is True)
self.counter = 0
# a dictionary of child nodes
# keys are characters, values are nodes
self.children = {}
class Trie(object):
"""The trie object"""
def __init__(self):
"""
The trie has at least the root node.
The root node does not store any character
"""
self.root = TrieNode("")
def insert(self, word):
"""Insert a word into the trie"""
node = self.root
# Loop through each character in the word
# Check if there is no child containing the character, create a new child for the current node
for char in word:
if char in node.children:
node = node.children[char]
else:
# If a character is not found,
# create a new node in the trie
new_node = TrieNode(char)
node.children[char] = new_node
node = new_node
# Mark the end of a word
node.is_end = True
# Increment the counter to indicate that we see this word once more
node.counter += 1
def dfs(self, node, prefix):
"""Depth-first traversal of the trie
Args:
- node: the node to start with
- prefix: the current prefix, for tracing a
word while traversing the trie
"""
if node.is_end:
self.output.append((prefix + node.char, node.counter))
for child in node.children.values():
self.dfs(child, prefix + node.char)
def query(self, x):
"""Given an input (a prefix), retrieve all words stored in
the trie with that prefix, sort the words by the number of
times they have been inserted
"""
# Use a variable within the class to keep all possible outputs
# As there can be more than one word with such prefix
self.output = []
node = self.root
# Check if the prefix is in the trie
for char in x:
if char in node.children:
node = node.children[char]
'''else:
# cannot found the prefix, return empty list
return []'''
# Traverse the trie to get all candidates
self.dfs(node, x[:-1])
# Sort the results in reverse order and return
return sorted(self.output, key=lambda x: x[1], reverse=True)
mycursor=mydb.cursor()
app = Flask(__name__)
loggedUser='User'
@app.route('/')
def index():
return render_template('homePage.html')
@app.route('/test', methods=['POST'])
def test():
output=request.get_json()
result=json.loads(output)
print(result)
sql1= "SELECT userid FROM users"
mycursor.execute(sql1)
rel1= mycursor.fetchall()
print(rel1)
check="false"
for i in range(0, len(rel1)):
if (result["user"] == rel1[i][0]):
check="true"
break
else:
check="false"
if(check=="false"):
sql ="INSERT INTO users (name, gender, dob, phone, mail, userid, password) VALUES (%s, %s, %s, %s, %s, %s, %s)"
val = (result["name"], result["gender"], result["dob"], result["telp"], result["mail"], result["user"], result["pass"])
mycursor.execute(sql, val)
mydb.commit()
print("inserted")
return check
else:
print("error")
return check
@app.route('/testAdv', methods=['POST'])
def testAdv():
output=request.get_json()
result=json.loads(output)
print(result)
sql1= "SELECT userid FROM websiteUser"
mycursor.execute(sql1)
rel1= mycursor.fetchall()
print(rel1)
check="false"
for i in range(0, len(rel1)):
if (result["user"] == rel1[i][0]):
check="true"
break
else:
check="false"
if(check=="false"):
sql ="INSERT INTO websiteUser (name, compName, contact, telNum, mail, userid, password, walletBalance) VALUES (%s, %s, %s, %s, %s, %s, %s,0)"
val = (result["name"], result["compname"], result["telp"], result["telph"], result["mail"], result["user"], result["pass"])
mycursor.execute(sql, val)
mydb.commit()
print("inserted")
return check
else:
print("error")
return check
@app.route('/signin')
def signin():
return render_template('index.html')
@app.route('/signinadv')
def signinadv():
return render_template('advSignin.html')
@app.route('/signinweb')
def signinweb():
return render_template('webSignin.html')
@app.route('/signUp')
def signup():
return render_template('index1.html')
@app.route('/signUpAdv')
def signupadv():
return render_template('advSignup.html')
t=Trie()
@app.route('/search')
def home():
sql2="SELECT tag FROM websiteTag"
mycursor.execute(sql2)
rel2= mycursor.fetchall()
print(rel2)
for i in rel2:
t.insert(i[0])
sql="SELECT name FROM users WHERE userid = %s"
val=(loggedUser,)
print(val)
mycursor.execute(sql, val)
rel=mycursor.fetchall()
print(rel[0][0])
sql="SELECT mail FROM users WHERE userid = %s"
val=(loggedUser,)
print(val)
mycursor.execute(sql, val)
rel1=mycursor.fetchall()
print(rel1[0][0])
print(rel[0][0])
return render_template('searchEngine.html', name=rel[0][0], mail=rel1[0][0])
@app.route('/advDash')
def advDash():
sql="SELECT * FROM advUser WHERE userid = %s"
print("start")
print(loggedUser)
val=(loggedUser,)
print(val)
mycursor.execute(sql, val)
rel=mycursor.fetchall()
print(rel)
print(rel[0][0])
return render_template('advDash.html', name=rel[0][0],wbalance=rel[0][7])
@app.route('/check', methods=['POST'])
def check():
output1 =request.get_json()
result1=json.loads(output1)
check=0
print(result1)
sql="SELECT password FROM users WHERE userid = %s"
val=(result1["user"],)
print(val[0])
mycursor.execute(sql, val)
rel= mycursor.fetchall()
print(rel)
print(result1["pass"])
global loggedUser
loggedUser=val[0]
return json.dumps({'user':val[0], 'passent':result1["pass"], 'passcrct':rel[0][0]})
@app.route('/checkAdv',methods=['POST'])
def checkAdv():
output1 =request.get_json()
result1=json.loads(output1)
check=0
print(result1)
sql="SELECT password FROM advUser WHERE userid = %s"
val=(result1["user"],)
print(val[0])
mycursor.execute(sql, val)
rel= mycursor.fetchall()
print(rel)
print(rel[0][0])
print(result1["pass"])
global loggedUser
loggedUser=val[0]
return json.dumps({'user':val[0], 'passent':result1["pass"], 'passcrct':rel[0][0]})
@app.route('/checkWeb',methods=['POST'])
def checkWeb():
output1 =request.get_json()
result1=json.loads(output1)
check=0
print(result1)
sql="SELECT password FROM websiteUser WHERE userid = %s"
val=(result1["user"],)
print(val[0])
mycursor.execute(sql, val)
rel= mycursor.fetchall()
print(rel)
print(rel[0][0])
print(result1["pass"])
global loggedUser
loggedUser=val[0]
return json.dumps({'user':val[0], 'passent':result1["pass"], 'passcrct':rel[0][0]})
@app.route('/trie', methods=['POST'])
def trie():
get=request.get_json()
r=json.loads(get)
print(r)
str1=r['inp']
print(str1)
a=t.query(str1)
print(a)
b=[]
for i in a:
b.append(i[0])
print(b)
return json.dumps(b)
keyword1='sample'
@app.route('/webList', methods=['POST'])
def webList():
get=request.get_json()
r=json.loads(get)
print('error')
print(r)
keyword=r['tag1']
global keyword1
keyword1=keyword
print(keyword)
return keyword
@app.route('/displayWebList')
def displayWebList():
'''sql="SELECT distinct(websiteId) FROM websiteTag WHERE tag = %s"
val=(keyword1,)
mycursor.execute(sql, val)
rel=mycursor.fetchall()
print(rel)
list1=[]
for i in rel:
list1.append()'''
return render_template('webList.html',tag1=keyword1)
@app.route('/getWebsiteList', methods=['POST'])
def getWebsiteList():
sql="SELECT distinct(websiteId) FROM websiteTag WHERE tag = %s"
val=(keyword1,)
mycursor.execute(sql, val)
rel=mycursor.fetchall()
print(rel)
list1=[]
for i in rel:
sql1="SELECT websiteName, websiteLink, userid, websiteId from webDetails where websiteId = %s"
val1=(i[0],)
mycursor.execute(sql1, val1)
rel1=mycursor.fetchall()
print(rel1)
list1.append(rel1[0])
print(list1)
return list1
@app.route('/insertWebDetail', methods=['POST'])
def insertWebDetail():
get=request.get_json()
r=json.loads(get)
print(r)
sql1= "SELECT websiteName FROM webDetails"
mycursor.execute(sql1)
rel1= mycursor.fetchall()
print(rel1)
check="false"
for i in range(0, len(rel1)):
if (r["name"] == rel1[i][0]):
check="true"
break
else:
check="false"
if(check=="false"):
sql="insert into webDetails (userid, websiteLink, websiteName, views) values(%s,%s,%s,0)"
val = (loggedUser,r['link'], r['name'])
mycursor.execute(sql, val)
mydb.commit()
return check
@app.route('/insertWebTag', methods=['POST'])
def insertWebTag():
get=request.get_json()
r=json.loads(get)
print(r)
print(r['tag'])
val1=[]
val1.append(r['tag'])
print(val1)
val=(r['tag'],)
mycursor.callproc('insertTag', val)
mydb.commit()
return 'true'
@app.route('/withdrawpage')
def withdrawpage():
return 'amount withdrawn'
@app.route('/webDash')
def webDash():
sql="SELECT * FROM websiteUser WHERE userid = %s"
print("start")
print(loggedUser)
val=(loggedUser,)
print(val)
mycursor.execute(sql, val)
rel=mycursor.fetchall()
print(rel)
print(rel[0][0])
sql1="SELECT websiteId, websiteLink, websiteName FROM webDetails WHERE userid = %s"
val1=(loggedUser,)
mycursor.execute(sql1, val1)
rel1=mycursor.fetchall()
print(rel1)
print(rel1[0][0])
sql2="SELECT increaseAmt, tranDate from webTransaction where userid=%s order by tranDate desc";
val2=(loggedUser,)
mycursor.execute(sql2, val2)
rel2=mycursor.fetchall()
sql3="SELECT websiteId, websiteName, views from webDetails where userid=%s"
val3=(loggedUser,)
mycursor.execute(sql3, val3)
rel3=mycursor.fetchall()
sql4="SELECT sum(views) from webDetails where userid=%s"
val4=(loggedUser,)
mycursor.execute(sql4, val4)
rel4=mycursor.fetchall()
print('rel4')
print(rel4[0][0])
return render_template('webOwnDash.html', name=rel[0][0],wbalance=rel[0][7], table=rel1, table1=rel2, table2=rel3, total=rel4[0][0])
webOwner='user'
webId=0000
@app.route('/getWebName', methods=['POST'])
def getWebName():
get=request.get_json()
r=json.loads(get)
print(r)
sql1="SELECT userid FROM webDetails WHERE websiteId = %s"
val1=(r['ele1'],)
mycursor.execute(sql1, val1)
rel1=mycursor.fetchall()
print("inside rel")
print(rel1)
webOwn=rel1[0][0]
wid=r['ele1']
global webId
webId=wid
global webOwner
webOwner=webOwn
print(webOwner)
return 'true'
@app.route('/openWebsite')
def openWebsite():
sql ="update webdetails set views=views+1 where websiteId=%s"
val = (webId,)
mycursor.execute(sql, val)
mydb.commit()
return render_template('dummyWeb.html')
@app.route('/withdraw', methods=['POST'])
def withdrawn():
get=request.get_json()
r=json.loads(get)
print(r)
val=(r['amt'], loggedUser)
mycursor.callproc('withdraw', val)
mydb.commit()
return 'true'
@app.route('/updateWebDetail', methods=['POST'])
def updateWeb():
get=request.get_json()
r=json.loads(get)
print(r)
sql ="update webDetails set websiteName =%s, websiteLink=%s where websiteId=%s"
val = (r['name'], r['link'], r['id'])
mycursor.execute(sql, val)
mydb.commit()
return 'true'
@app.route('/upWallet')
def upWallet():
sql ="update websiteUser set walletBalance=walletBalance+200 where userid=%s"
val = (webOwner,)
mycursor.execute(sql, val)
mydb.commit()
return 'Advertisement'
@app.route('/removeWeb', methods=['POST'])
def removeWeb():
get=request.get_json()
r=json.loads(get)
print(r)
print(r['id'])
val=(r['id'],)
mycursor.callproc('deleteWeb', val)
mydb.commit()
return 'true'
app.run(debug=True)