-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.py
More file actions
246 lines (191 loc) · 7.27 KB
/
Copy pathqueries.py
File metadata and controls
246 lines (191 loc) · 7.27 KB
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
import data_manager
import bcrypt
# Functions for registration
def get_hashed_password(plain_text_password):
hashed_bytes = bcrypt.hashpw(plain_text_password.encode('utf-8'), bcrypt.gensalt())
return hashed_bytes.decode("utf-8")
@data_manager.connection_handler
def submit_registration(cursor, username, password):
hashword = get_hashed_password(password)
cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s);", (username, hashword))
@data_manager.connection_handler
def username_check(cursor, username):
cursor.execute("SELECT username FROM users WHERE username = (%s);", (username,))
result = cursor.fetchall()
if len(result) == 0:
return True
else:
return False
# Functions for login
def check_password(plain_text_password, hashed_text_password):
hashed_bytes_password = hashed_text_password.encode("utf-8")
return bcrypt.checkpw(plain_text_password.encode('utf-8'), hashed_bytes_password)
@data_manager.connection_handler
def is_valid_login(cursor, username, password):
cursor.execute("SELECT password FROM users WHERE username = (%s);", (username,))
hashword = cursor.fetchall()
if len(hashword) == 0:
return False
else:
return check_password(password, hashword[0]["password"])
@data_manager.connection_handler
def check_blog_ownership(cursor, blogID, userID):
cursor.execute("SELECT owner_id FROM blogs WHERE id = %s;", (blogID,))
ownerID = cursor.fetchall()[0]["owner_id"]
if int(ownerID) == int(userID):
return True
else:
return False
@data_manager.connection_handler
def check_answer_ownership(cursor, answerID, userID):
cursor.execute("SELECT owner_id FROM answers WHERE id = %s;", (answerID,))
ownerID = cursor.fetchall()[0]["owner_id"]
if int(ownerID) == int(userID):
return True
else:
return False
# Functions for grab datas
@data_manager.connection_handler
def get_user(cursor, username):
cursor.execute("SELECT id, username FROM users WHERE username = (%s);", (username,))
data = cursor.fetchall()
return data[0]
@data_manager.connection_handler
def get_users(cursor):
cursor.execute("SELECT * FROM users")
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_blogs(cursor):
cursor.execute("SELECT * FROM blogs ORDER BY id")
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_blogposts(cursor, blogID):
cursor.execute("""SELECT * FROM blogposts
WHERE blog_id = %s
ORDER BY id;""", (blogID))
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_blogpost(cursor, blogID):
cursor.execute("""SELECT * FROM blogposts
WHERE id = %s
ORDER BY id;""", (blogID,))
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_answers(cursor, blogpostID):
cursor.execute("""SELECT * FROM answers
WHERE blogpost_id = %s
ORDER BY id;""", (blogpostID,))
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_about(cursor, blogID):
cursor.execute("""SELECT about FROM blogs
WHERE blog_id = %s;""", (blogID))
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_contact(cursor, blogID):
cursor.execute("""SELECT contact FROM blogs
WHERE blog_id = %s;""", (blogID))
data = cursor.fetchall()
return data
@data_manager.connection_handler
def get_links(cursor, blogID):
cursor.execute("""SELECT links FROM blogs
WHERE blog_id = %s;""", (blogID))
data = cursor.fetchall()
return data
# Submit stuff
@data_manager.connection_handler
def submit_blog(cursor, title, userID):
cursor.execute("""INSERT INTO blogs (name, owner_id)
VALUES (%s, %s);""", (title, userID))
@data_manager.connection_handler
def submit_blogpost(cursor, title, message, img, blogID, userID):
if check_blog_ownership(blogID, userID):
if img != "null":
cursor.execute("""INSERT INTO blogposts (title, message, img, blog_id)
VALUES (%s, %s, %s, %s);""", (title, message, img, blogID))
else:
cursor.execute("""INSERT INTO blogposts (title, message, blog_id)
VALUES (%s, %s, %s);""", (title, message, blogID))
else:
return print("ERROR")
@data_manager.connection_handler
def submit_answer(cursor, blogpostid, userid, username, message):
cursor.execute("""INSERT INTO answers (blogpost_id, owner_id, username, message)
VALUES (%s, %s, %s, %s);""", (blogpostid, userid, username, message))
@data_manager.connection_handler
def edit_blog(cursor, name, blogid, userID):
if check_blog_ownership(blogid, userID):
cursor.execute("""UPDATE blogs
SET name=%s
WHERE id =%s""", (name, blogid))
else:
return "ERROR"
@data_manager.connection_handler
def delete_blog(cursor, blogid, userID):
if check_blog_ownership(blogid, userID):
cursor.execute("""DELETE FROM blogs
WHERE id =%s""", (blogid,))
cursor.execute("""SELECT * FROM blogposts
WHERE blog_id = %s;""", (blogid))
data = cursor.fetchall()
for i in range(len(data)):
delete_blogPost(data[i]["id"])
else:
return "ERROR"
@data_manager.connection_handler
def edit_blogPost(cursor, postid, title, message, userID):
cursor.execute("SELECT blog_id FROM blogposts WHERE id=%s;", (postid,))
blogID = cursor.fetchall()[0]["blog_id"]
if check_blog_ownership(blogID, userID):
cursor.execute("""UPDATE blogposts
SET title=%s, message=%s
WHERE id =%s""", (title, message, postid))
else:
return "ERROR"
@data_manager.connection_handler
def delete_blogPost(cursor, postid, userID):
if check_blog_ownership(blogid, userID):
cursor.execute("""DELETE FROM blogposts
WHERE id =%s""", (postid,))
cursor.execute("""DELETE FROM answers
WHERE blogpost_id =%s""", (postid,))
else:
return "ERROR"
@data_manager.connection_handler
def edit_answer(cursor, answerid, blogpostid, userID, username, message):
if check_answer_ownership(answerid, userID):
cursor.execute("""UPDATE answers
SET blogpost_id=%s, owner_id=%s, username=%s, message=%s
WHERE id =%s""", (blogpostid, userID, username, message, answerid))
else:
return "ERROR"
@data_manager.connection_handler
def delete_answer(cursor, answerid, userID):
if check_answer_ownership(answerid, userID):
cursor.execute("""DELETE FROM answers
WHERE id =%s""", (answerid,))
else:
return "ERROR"
@data_manager.connection_handler
def edit_about(cursor, blogid, message, userID):
if check_blog_ownership(blogid, userID):
cursor.execute("""UPDATE blogs
SET about=%s
WHERE id =%s""", (message, blogid))
else:
return "ERROR"
@data_manager.connection_handler
def edit_contact(cursor, blogid, message, userID):
if check_blog_ownership(blogid, userID):
cursor.execute("""UPDATE blogs
SET contact=%s
WHERE id =%s""", (message, blogid))
else:
return "ERROR"