-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_helper.py
125 lines (106 loc) · 3.82 KB
/
database_helper.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
import sqlite3
from flask import g
DATABASE_URI = 'database.db'
def get_db():
db = getattr(g, 'db', None)
if db is None:
db = g.db = sqlite3.connect(DATABASE_URI)
return db
def disconnect_db():
db = getattr(g, 'db', None)
if db is not None:
g.db.close()
g.db = None
def sign_in(email, token):
try:
get_db().execute("insert into loggedinusers values(?,?);",[email,token])
get_db().commit()
return True
except:
return False
def delete_loggedinuser(email):
try:
email=email.replace('"', '')
result = get_db().execute("delete from loggedinusers where email like ?", [email])
get_db().commit()
return True
except:
return False
def get_contact(email):
cursor = get_db().execute('select * from contact where email like ?', [email])
rows = cursor.fetchall()
cursor.close()
result = []
for index in range(len(rows)):
result.append({'firstname': rows[index][0], 'familyname': rows[index][1],
'gender': rows[index][2], 'city': rows[index][3], 'country': rows[index][4],
'email': rows[index][5], 'password': rows[index][6]})
return result
def sign_up(firstname, familyname, gender, city, country, email, password):
try:
get_db().execute("insert into contact values(?,?,?,?,?,?,?);",
[firstname, familyname, gender, city, country, email, password])
get_db().commit()
return True
except:
return False
def sign_out(token):
try:
get_db().execute("delete from loggedinusers where token like ?", [token])
get_db().commit()
return True
except:
return False
def get_old_password(email):
cursor = get_db().execute('select * from contact where email like ?', [email])
rows = cursor.fetchall()
cursor.close()
return rows[0][6]
def get_email_from_token(token):
cursor = get_db().execute('select * from loggedinusers where token like ?', [token])
rows = cursor.fetchall()
cursor.close()
return rows[0][0]
def check_token(token):
a = get_db().execute('select * from loggedinusers where token like ?', [token]).fetchall()
get_db().commit()
if a:
return True
else:
return False
def Change_password(token, newPassword):
try:
cursor = get_db().execute('select * from loggedinusers where token like ?', [token])
rows = cursor.fetchall()
cursor.close()
get_db().execute("UPDATE contact SET password = ? WHERE email like ?", [newPassword, rows[0][0]])
get_db().commit()
return True
except:
return False
def get_user_data_by_email(email):
cursor = get_db().execute('select * from contact where email like ?', [email])
rows = cursor.fetchmany(size=6)
cursor.close()
result = []
for index in range(len(rows)):
result.append({'firstname': rows[index][0], 'familyname': rows[index][1],
'gender': rows[index][2], 'city': rows[index][3], 'country': rows[index][4],
'email': rows[index][5]})
return result
def get_user_messages_by_email(email):
cursor = get_db().execute('select * from wall_data where email like ?', [email])
rows = cursor.fetchall()
cursor.close()
result = []
for index in range(len(rows)):
result.append({'email': rows[index][0], 'writer': rows[index][1], 'content': rows[index][2]})
return result
def post_message(email, writer, content):
print(email, writer, content)
try:
get_db().execute("insert into wall_data values(?,?,?);", [email, writer, content])
get_db().commit()
return True
except:
return False