1
|
|
- |
import MySQLdb
|
|
1
|
+ |
import hashlib
|
2
|
2
|
|
import re
|
|
3
|
+ |
import sqlite3
|
3
|
4
|
|
from freepost import random, settings
|
4
|
5
|
|
|
5
|
|
- |
db = MySQLdb.connect (
|
6
|
|
- |
host = settings['mysql']['host'],
|
7
|
|
- |
port = settings['mysql']['port'],
|
8
|
|
- |
db = settings['mysql']['schema'],
|
9
|
|
- |
charset = settings['mysql']['charset'],
|
10
|
|
- |
user = settings['mysql']['username'],
|
11
|
|
- |
passwd = settings['mysql']['password'],
|
12
|
|
- |
autocommit = True)
|
|
6
|
+ |
db = sqlite3.connect(settings['sqlite']['database'])
|
13
|
7
|
|
|
14
|
|
- |
# Since the "db" object is reused for the entire life of website,
|
15
|
|
- |
# this ping should avoid MySQL timing out if no requests are sent in a while.
|
16
|
|
- |
db.ping (True)
|
|
8
|
+ |
# Returns SQLite rows as dictionaries instead of tuples.
|
|
9
|
+ |
# https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.row_factory
|
|
10
|
+ |
db.row_factory = sqlite3.Row
|
|
11
|
+ |
|
|
12
|
+ |
# A custom function to compute SHA-512 because it's not built into SQLite
|
|
13
|
+ |
db.create_function('SHA512', 1, lambda text:
|
|
14
|
+ |
None if text is None else hashlib.sha512(text.encode('UTF-8')).hexdigest())
|
|
15
|
+ |
|
|
16
|
+ |
# The REGEXP operator is a special syntax for the regexp() user function. No
|
|
17
|
+ |
# regexp() user function is defined by default and so use of the REGEXP operator
|
|
18
|
+ |
# will normally result in an error message. If an application-defined SQL
|
|
19
|
+ |
# function named "regexp" is added at run-time, then the "X REGEXP Y" operator
|
|
20
|
+ |
# will be implemented as a call to "regexp(Y,X)".
|
|
21
|
+ |
db.create_function('REGEXP', 2, lambda pattern, string:
|
|
22
|
+ |
re.match(pattern, string, flags=re.IGNORECASE) is not None)
|
17
|
23
|
|
|
18
|
24
|
|
# Store a new session_id for a user that has logged in
|
19
|
25
|
|
# The session token is stored in the user cookies during login, here
|
20
|
26
|
|
# we store the hash value of that token.
|
21
|
|
- |
def new_session (user_id, session_token):
|
22
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
23
|
|
- |
|
24
|
|
- |
cursor.execute (
|
25
|
|
- |
"""
|
26
|
|
- |
UPDATE user
|
27
|
|
- |
SET session = SHA2(%(session)s, 512)
|
28
|
|
- |
WHERE id = %(user)s
|
29
|
|
- |
""",
|
30
|
|
- |
{
|
31
|
|
- |
'user': user_id,
|
32
|
|
- |
'session': session_token
|
33
|
|
- |
}
|
34
|
|
- |
)
|
|
27
|
+ |
def new_session(user_id, session_token):
|
|
28
|
+ |
with db:
|
|
29
|
+ |
db.execute(
|
|
30
|
+ |
"""
|
|
31
|
+ |
UPDATE user
|
|
32
|
+ |
SET session = SHA512(:session)
|
|
33
|
+ |
WHERE id = :user
|
|
34
|
+ |
""",
|
|
35
|
+ |
{
|
|
36
|
+ |
'user': user_id,
|
|
37
|
+ |
'session': session_token
|
|
38
|
+ |
}
|
|
39
|
+ |
)
|
35
|
40
|
|
|
36
|
41
|
|
# Delete user session token on logout
|
37
|
42
|
|
def delete_session (user_id):
|
38
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
39
|
|
- |
|
40
|
|
- |
cursor.execute (
|
41
|
|
- |
"""
|
42
|
|
- |
UPDATE user
|
43
|
|
- |
SET session = NULL
|
44
|
|
- |
WHERE id = %(user)s
|
45
|
|
- |
""",
|
46
|
|
- |
{
|
47
|
|
- |
'user': user_id
|
48
|
|
- |
}
|
49
|
|
- |
)
|
|
43
|
+ |
with db:
|
|
44
|
+ |
db.execute (
|
|
45
|
+ |
"""
|
|
46
|
+ |
UPDATE user
|
|
47
|
+ |
SET session = NULL
|
|
48
|
+ |
WHERE id = :user
|
|
49
|
+ |
""",
|
|
50
|
+ |
{
|
|
51
|
+ |
'user': user_id
|
|
52
|
+ |
}
|
|
53
|
+ |
)
|
50
|
54
|
|
|
51
|
55
|
|
# Check user login credentials
|
52
|
56
|
|
#
|
53
|
57
|
|
# @return None if bad credentials, otherwise return the user
|
54
|
58
|
|
def check_user_credentials (username, password):
|
55
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
56
|
|
- |
|
57
|
|
- |
cursor.execute (
|
58
|
|
- |
"""
|
59
|
|
- |
SELECT *
|
60
|
|
- |
FROM user
|
61
|
|
- |
WHERE
|
62
|
|
- |
username = %(username)s AND
|
63
|
|
- |
password = SHA2(CONCAT(%(password)s, salt), 512) AND
|
64
|
|
- |
isActive = 1
|
65
|
|
- |
""",
|
66
|
|
- |
{
|
67
|
|
- |
'username': username,
|
68
|
|
- |
'password': password
|
69
|
|
- |
}
|
70
|
|
- |
)
|
71
|
|
- |
|
72
|
|
- |
return cursor.fetchone ()
|
|
59
|
+ |
with db:
|
|
60
|
+ |
cursor = db.execute (
|
|
61
|
+ |
"""
|
|
62
|
+ |
SELECT *
|
|
63
|
+ |
FROM user
|
|
64
|
+ |
WHERE username = :username
|
|
65
|
+ |
AND password = SHA512(:password || salt)
|
|
66
|
+ |
AND isActive = 1
|
|
67
|
+ |
""",
|
|
68
|
+ |
{
|
|
69
|
+ |
'username': username,
|
|
70
|
+ |
'password': password
|
|
71
|
+ |
}
|
|
72
|
+ |
)
|
|
73
|
+ |
|
|
74
|
+ |
return cursor.fetchone ()
|
73
|
75
|
|
|
74
|
76
|
|
# Check if username exists
|
75
|
|
- |
def username_exists (username):
|
76
|
|
- |
return get_user_by_username (username) is not None
|
|
77
|
+ |
def username_exists (username, case_sensitive = True):
|
|
78
|
+ |
if not username:
|
|
79
|
+ |
return None
|
|
80
|
+ |
|
|
81
|
+ |
if case_sensitive:
|
|
82
|
+ |
where = 'WHERE username = :username'
|
|
83
|
+ |
else:
|
|
84
|
+ |
where = 'WHERE LOWER(username) = LOWER(:username)'
|
|
85
|
+ |
|
|
86
|
+ |
with db:
|
|
87
|
+ |
cursor = db.execute(
|
|
88
|
+ |
"""
|
|
89
|
+ |
SELECT *
|
|
90
|
+ |
FROM user
|
|
91
|
+ |
""" +
|
|
92
|
+ |
where,
|
|
93
|
+ |
{
|
|
94
|
+ |
'username': username
|
|
95
|
+ |
}
|
|
96
|
+ |
)
|
|
97
|
+ |
|
|
98
|
+ |
return cursor.fetchone() is not None
|
77
|
99
|
|
|
78
|
100
|
|
# Create new user account
|
79
|
101
|
|
def new_user (username, password):
|
84
|
106
|
|
salt = random.ascii_string (16)
|
85
|
107
|
|
|
86
|
108
|
|
# Add user to database
|
87
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
88
|
|
- |
|
89
|
|
- |
cursor.execute (
|
90
|
|
- |
"""
|
91
|
|
- |
INSERT INTO user (hashId, isActive, password, registered, salt, username)
|
92
|
|
- |
VALUES (%(hash_id)s, 1, SHA2(CONCAT(%(password)s, %(salt)s), 512), NOW(), %(salt)s, %(username)s)
|
93
|
|
- |
""",
|
94
|
|
- |
{
|
95
|
|
- |
'hash_id': hash_id,
|
96
|
|
- |
'password': password,
|
97
|
|
- |
'salt': salt,
|
98
|
|
- |
'username': username
|
99
|
|
- |
}
|
100
|
|
- |
)
|
|
109
|
+ |
with db:
|
|
110
|
+ |
db.execute (
|
|
111
|
+ |
"""
|
|
112
|
+ |
INSERT INTO user (hashId, isActive, password, registered, salt, username)
|
|
113
|
+ |
VALUES (:hash_id, 1, SHA512(:password || :salt), DATE(), :salt, :username)
|
|
114
|
+ |
""",
|
|
115
|
+ |
{
|
|
116
|
+ |
'hash_id': hash_id,
|
|
117
|
+ |
'password': password,
|
|
118
|
+ |
'salt': salt,
|
|
119
|
+ |
'username': username
|
|
120
|
+ |
}
|
|
121
|
+ |
)
|
101
|
122
|
|
|
102
|
123
|
|
# Check if session token exists
|
103
|
124
|
|
def is_valid_session (token):
|
105
|
126
|
|
|
106
|
127
|
|
# Return the number of unread replies
|
107
|
128
|
|
def count_unread_messages (user_id):
|
108
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
109
|
|
- |
|
110
|
|
- |
cursor.execute (
|
111
|
|
- |
"""
|
112
|
|
- |
SELECT COUNT(1) as new_messages
|
113
|
|
- |
FROM comment
|
114
|
|
- |
WHERE parentUserId = %(user)s AND userId != %(user)s AND `read` = 0
|
115
|
|
- |
""",
|
116
|
|
- |
{
|
117
|
|
- |
'user': user_id
|
118
|
|
- |
}
|
119
|
|
- |
)
|
|
129
|
+ |
with db:
|
|
130
|
+ |
cursor = db.execute (
|
|
131
|
+ |
"""
|
|
132
|
+ |
SELECT COUNT(1) AS new_messages
|
|
133
|
+ |
FROM comment
|
|
134
|
+ |
WHERE parentUserId = :user AND userId != :user AND `read` = 0
|
|
135
|
+ |
""",
|
|
136
|
+ |
{
|
|
137
|
+ |
'user': user_id
|
|
138
|
+ |
}
|
|
139
|
+ |
)
|
120
|
140
|
|
|
121
|
|
- |
return cursor.fetchone ()['new_messages']
|
|
141
|
+ |
return cursor.fetchone ()['new_messages']
|
122
|
142
|
|
|
123
|
143
|
|
# Retrieve a user
|
124
|
144
|
|
def get_user_by_username (username):
|
125
|
145
|
|
if not username:
|
126
|
146
|
|
return None
|
127
|
147
|
|
|
128
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
129
|
|
- |
|
130
|
|
- |
cursor.execute (
|
131
|
|
- |
"""
|
132
|
|
- |
SELECT *
|
133
|
|
- |
FROM user
|
134
|
|
- |
WHERE username = %(username)s
|
135
|
|
- |
""",
|
136
|
|
- |
{
|
137
|
|
- |
'username': username
|
138
|
|
- |
}
|
139
|
|
- |
)
|
140
|
|
- |
|
141
|
|
- |
return cursor.fetchone ()
|
|
148
|
+ |
with db:
|
|
149
|
+ |
cursor = db.execute(
|
|
150
|
+ |
"""
|
|
151
|
+ |
SELECT *
|
|
152
|
+ |
FROM user
|
|
153
|
+ |
WHERE username = :username
|
|
154
|
+ |
""",
|
|
155
|
+ |
{
|
|
156
|
+ |
'username': username
|
|
157
|
+ |
}
|
|
158
|
+ |
)
|
|
159
|
+ |
|
|
160
|
+ |
return cursor.fetchone()
|
142
|
161
|
|
|
143
|
162
|
|
# Retrieve a user from a session cookie
|
144
|
|
- |
def get_user_by_session_token (session_token):
|
145
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
146
|
|
- |
|
147
|
|
- |
cursor.execute (
|
148
|
|
- |
"""
|
149
|
|
- |
SELECT *
|
150
|
|
- |
FROM user
|
151
|
|
- |
WHERE session = SHA2(%(session)s, 512)
|
152
|
|
- |
""",
|
153
|
|
- |
{
|
154
|
|
- |
'session': session_token
|
155
|
|
- |
}
|
156
|
|
- |
)
|
|
163
|
+ |
def get_user_by_session_token(session_token):
|
|
164
|
+ |
with db:
|
|
165
|
+ |
cursor = db.execute(
|
|
166
|
+ |
"""
|
|
167
|
+ |
SELECT *
|
|
168
|
+ |
FROM user
|
|
169
|
+ |
WHERE session = SHA512(:session)
|
|
170
|
+ |
""",
|
|
171
|
+ |
{
|
|
172
|
+ |
'session': session_token
|
|
173
|
+ |
}
|
|
174
|
+ |
)
|
157
|
175
|
|
|
158
|
|
- |
return cursor.fetchone ()
|
|
176
|
+ |
return cursor.fetchone()
|
159
|
177
|
|
|
160
|
178
|
|
# Get posts by date (for homepage)
|
161
|
179
|
|
def get_posts (page = 0, session_user_id = None, sort = 'hot', topic = None):
|
165
|
183
|
|
sort = 'ORDER BY P.dateCreated DESC, P.vote DESC, P.commentsCount DESC'
|
166
|
184
|
|
|
167
|
185
|
|
if topic:
|
168
|
|
- |
topic_name = 'WHERE T.name = %(topic)s'
|
|
186
|
+ |
topic_name = 'WHERE T.name = :topic'
|
169
|
187
|
|
else:
|
170
|
188
|
|
topic_name = ''
|
171
|
189
|
|
|
172
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
173
|
|
- |
|
174
|
|
- |
cursor.execute (
|
175
|
|
- |
"""
|
176
|
|
- |
SELECT
|
177
|
|
- |
P.*,
|
178
|
|
- |
U.username,
|
179
|
|
- |
V.vote AS user_vote,
|
180
|
|
- |
GROUP_CONCAT(DISTINCT T.name ORDER BY T.name SEPARATOR " ") AS topics
|
181
|
|
- |
FROM post AS P
|
182
|
|
- |
JOIN user AS U ON P.userId = U.id
|
183
|
|
- |
LEFT JOIN vote_post as V ON V.postId = P.id AND V.userId = %(user)s
|
184
|
|
- |
LEFT JOIN topic as T ON T.post_id = P.id
|
185
|
|
- |
{topic}
|
186
|
|
- |
GROUP BY P.id
|
187
|
|
- |
{order}
|
188
|
|
- |
LIMIT %(limit)s
|
189
|
|
- |
OFFSET %(offset)s
|
190
|
|
- |
""".format (topic=topic_name, order=sort),
|
191
|
|
- |
{
|
192
|
|
- |
'user': session_user_id,
|
193
|
|
- |
'limit': settings['defaults']['items_per_page'],
|
194
|
|
- |
'offset': page * settings['defaults']['items_per_page'],
|
195
|
|
- |
'topic': topic
|
196
|
|
- |
}
|
197
|
|
- |
)
|
198
|
|
- |
|
199
|
|
- |
return cursor.fetchall ()
|
|
190
|
+ |
with db:
|
|
191
|
+ |
cursor = db.execute (
|
|
192
|
+ |
"""
|
|
193
|
+ |
SELECT P.*,
|
|
194
|
+ |
U.username,
|
|
195
|
+ |
V.vote AS user_vote,
|
|
196
|
+ |
GROUP_CONCAT(T.name, " ") AS topics
|
|
197
|
+ |
FROM post AS P
|
|
198
|
+ |
JOIN user AS U ON P.userId = U.id
|
|
199
|
+ |
LEFT JOIN vote_post as V ON V.postId = P.id AND V.userId = :user
|
|
200
|
+ |
LEFT JOIN topic as T ON T.post_id = P.id
|
|
201
|
+ |
{topic}
|
|
202
|
+ |
GROUP BY P.id
|
|
203
|
+ |
{order}
|
|
204
|
+ |
LIMIT :limit
|
|
205
|
+ |
OFFSET :offset
|
|
206
|
+ |
""".format (topic=topic_name, order=sort),
|
|
207
|
+ |
{
|
|
208
|
+ |
'user': session_user_id,
|
|
209
|
+ |
'limit': settings['defaults']['items_per_page'],
|
|
210
|
+ |
'offset': page * settings['defaults']['items_per_page'],
|
|
211
|
+ |
'topic': topic
|
|
212
|
+ |
}
|
|
213
|
+ |
)
|
|
214
|
+ |
|
|
215
|
+ |
return cursor.fetchall ()
|
200
|
216
|
|
|
201
|
217
|
|
# Retrieve user's own posts
|
202
|
218
|
|
def get_user_posts (user_id):
|
203
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
204
|
|
- |
|
205
|
|
- |
cursor.execute (
|
206
|
|
- |
"""
|
207
|
|
- |
SELECT *
|
208
|
|
- |
FROM post
|
209
|
|
- |
WHERE userId = %(user)s
|
210
|
|
- |
ORDER BY created DESC
|
211
|
|
- |
LIMIT 50
|
212
|
|
- |
""",
|
213
|
|
- |
{
|
214
|
|
- |
'user': user_id
|
215
|
|
- |
}
|
216
|
|
- |
)
|
217
|
|
- |
|
218
|
|
- |
return cursor.fetchall ()
|
|
219
|
+ |
with db:
|
|
220
|
+ |
cursor = db.execute (
|
|
221
|
+ |
"""
|
|
222
|
+ |
SELECT *
|
|
223
|
+ |
FROM post
|
|
224
|
+ |
WHERE userId = :user
|
|
225
|
+ |
ORDER BY created DESC
|
|
226
|
+ |
LIMIT 50
|
|
227
|
+ |
""",
|
|
228
|
+ |
{
|
|
229
|
+ |
'user': user_id
|
|
230
|
+ |
}
|
|
231
|
+ |
)
|
|
232
|
+ |
|
|
233
|
+ |
return cursor.fetchall()
|
219
|
234
|
|
|
220
|
235
|
|
# Retrieve user's own comments
|
221
|
236
|
|
def get_user_comments (user_id):
|
222
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
223
|
|
- |
|
224
|
|
- |
cursor.execute (
|
225
|
|
- |
"""
|
226
|
|
- |
SELECT
|
227
|
|
- |
C.*,
|
228
|
|
- |
P.title AS postTitle,
|
229
|
|
- |
P.hashId AS postHashId
|
230
|
|
- |
FROM comment AS C
|
231
|
|
- |
JOIN post AS P ON P.id = C.postId
|
232
|
|
- |
WHERE C.userId = %(user)s
|
233
|
|
- |
ORDER BY C.created DESC
|
234
|
|
- |
LIMIT 50
|
235
|
|
- |
""",
|
236
|
|
- |
{
|
237
|
|
- |
'user': user_id
|
238
|
|
- |
}
|
239
|
|
- |
)
|
240
|
|
- |
|
241
|
|
- |
return cursor.fetchall ()
|
|
237
|
+ |
with db:
|
|
238
|
+ |
cursor = db.execute (
|
|
239
|
+ |
"""
|
|
240
|
+ |
SELECT C.*,
|
|
241
|
+ |
P.title AS postTitle,
|
|
242
|
+ |
P.hashId AS postHashId
|
|
243
|
+ |
FROM comment AS C
|
|
244
|
+ |
JOIN post AS P ON P.id = C.postId
|
|
245
|
+ |
WHERE C.userId = :user
|
|
246
|
+ |
ORDER BY C.created DESC
|
|
247
|
+ |
LIMIT 50
|
|
248
|
+ |
""",
|
|
249
|
+ |
{
|
|
250
|
+ |
'user': user_id
|
|
251
|
+ |
}
|
|
252
|
+ |
)
|
|
253
|
+ |
|
|
254
|
+ |
return cursor.fetchall()
|
242
|
255
|
|
|
243
|
256
|
|
# Retrieve user's own replies to other people
|
244
|
257
|
|
def get_user_replies (user_id):
|
245
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
246
|
|
- |
|
247
|
|
- |
cursor.execute (
|
248
|
|
- |
"""
|
249
|
|
- |
SELECT
|
250
|
|
- |
C.*,
|
251
|
|
- |
P.title AS postTitle,
|
252
|
|
- |
P.hashId AS postHashId,
|
253
|
|
- |
U.username AS username
|
254
|
|
- |
FROM comment AS C
|
255
|
|
- |
JOIN post AS P ON P.id = C.postId
|
256
|
|
- |
JOIN user AS U ON U.id = C.userId
|
257
|
|
- |
WHERE C.parentUserId = %(user)s AND C.userId != %(user)s
|
258
|
|
- |
ORDER BY C.created DESC
|
259
|
|
- |
LIMIT 50
|
260
|
|
- |
""",
|
261
|
|
- |
{
|
262
|
|
- |
'user': user_id
|
263
|
|
- |
}
|
264
|
|
- |
)
|
265
|
|
- |
|
266
|
|
- |
return cursor.fetchall ()
|
|
258
|
+ |
with db:
|
|
259
|
+ |
cursor = db.execute(
|
|
260
|
+ |
"""
|
|
261
|
+ |
SELECT C.*,
|
|
262
|
+ |
P.title AS postTitle,
|
|
263
|
+ |
P.hashId AS postHashId,
|
|
264
|
+ |
U.username AS username
|
|
265
|
+ |
FROM comment AS C
|
|
266
|
+ |
JOIN post AS P ON P.id = C.postId
|
|
267
|
+ |
JOIN user AS U ON U.id = C.userId
|
|
268
|
+ |
WHERE C.parentUserId = :user AND C.userId != :user
|
|
269
|
+ |
ORDER BY C.created DESC
|
|
270
|
+ |
LIMIT 50
|
|
271
|
+ |
""",
|
|
272
|
+ |
{
|
|
273
|
+ |
'user': user_id
|
|
274
|
+ |
}
|
|
275
|
+ |
)
|
|
276
|
+ |
|
|
277
|
+ |
return cursor.fetchall()
|
267
|
278
|
|
|
268
|
279
|
|
# Update user information
|
269
|
280
|
|
def update_user (user_id, about, email, email_notifications, preferred_feed):
|
270
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
271
|
|
- |
|
272
|
|
- |
# Update user info, but not email address
|
273
|
|
- |
cursor.execute (
|
274
|
|
- |
"""
|
275
|
|
- |
UPDATE user
|
276
|
|
- |
SET about = %(about)s,
|
277
|
|
- |
email_notifications = %(notifications)s,
|
278
|
|
- |
preferred_feed = %(preferred_feed)s
|
279
|
|
- |
WHERE id = %(user)s
|
280
|
|
- |
""",
|
281
|
|
- |
{
|
282
|
|
- |
'about': about,
|
283
|
|
- |
'notifications': email_notifications,
|
284
|
|
- |
'user': user_id,
|
285
|
|
- |
'preferred_feed': preferred_feed
|
286
|
|
- |
}
|
287
|
|
- |
)
|
|
281
|
+ |
with db:
|
|
282
|
+ |
# Update user info, but not email address
|
|
283
|
+ |
db.execute(
|
|
284
|
+ |
"""
|
|
285
|
+ |
UPDATE user
|
|
286
|
+ |
SET about = :about,
|
|
287
|
+ |
email_notifications = :notifications,
|
|
288
|
+ |
preferred_feed = :preferred_feed
|
|
289
|
+ |
WHERE id = :user
|
|
290
|
+ |
""",
|
|
291
|
+ |
{
|
|
292
|
+ |
'about': about,
|
|
293
|
+ |
'notifications': email_notifications,
|
|
294
|
+ |
'user': user_id,
|
|
295
|
+ |
'preferred_feed': preferred_feed
|
|
296
|
+ |
}
|
|
297
|
+ |
)
|
288
|
298
|
|
|
289
|
|
- |
# Update email address
|
290
|
|
- |
# IGNORE update if the email address is already specified. This is
|
291
|
|
- |
# necessary to avoid an "duplicate key" exception when updating value.
|
292
|
|
- |
cursor.execute (
|
293
|
|
- |
"""
|
294
|
|
- |
UPDATE IGNORE user
|
295
|
|
- |
SET email = %(email)s
|
296
|
|
- |
WHERE id = %(user)s
|
297
|
|
- |
""",
|
298
|
|
- |
{
|
299
|
|
- |
'email': email,
|
300
|
|
- |
'user': user_id
|
301
|
|
- |
}
|
302
|
|
- |
)
|
|
299
|
+ |
# Update email address. Convert all addresses to LOWER() case. This
|
|
300
|
+ |
# prevents two users from using the same address with different case.
|
|
301
|
+ |
# IGNORE update if the email address is already specified. This is
|
|
302
|
+ |
# necessary to avoid an "duplicate key" exception when updating value.
|
|
303
|
+ |
db.execute (
|
|
304
|
+ |
"""
|
|
305
|
+ |
UPDATE OR IGNORE user
|
|
306
|
+ |
SET email = LOWER(:email)
|
|
307
|
+ |
WHERE id = :user
|
|
308
|
+ |
""",
|
|
309
|
+ |
{
|
|
310
|
+ |
'email': email,
|
|
311
|
+ |
'user': user_id
|
|
312
|
+ |
}
|
|
313
|
+ |
)
|
303
|
314
|
|
|
304
|
315
|
|
# Set user replies as read
|
305
|
316
|
|
def set_replies_as_read (user_id):
|
306
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
307
|
|
- |
|
308
|
|
- |
cursor.execute (
|
309
|
|
- |
"""
|
310
|
|
- |
UPDATE comment
|
311
|
|
- |
SET `read` = 1
|
312
|
|
- |
WHERE parentUserId = %(user)s AND `read` = 0
|
313
|
|
- |
""",
|
314
|
|
- |
{
|
315
|
|
- |
'user': user_id
|
316
|
|
- |
}
|
317
|
|
- |
)
|
|
317
|
+ |
with db:
|
|
318
|
+ |
db.execute(
|
|
319
|
+ |
"""
|
|
320
|
+ |
UPDATE comment
|
|
321
|
+ |
SET `read` = 1
|
|
322
|
+ |
WHERE parentUserId = :user AND `read` = 0
|
|
323
|
+ |
""",
|
|
324
|
+ |
{
|
|
325
|
+ |
'user': user_id
|
|
326
|
+ |
}
|
|
327
|
+ |
)
|
318
|
328
|
|
|
319
|
329
|
|
# Submit a new post/link
|
320
|
330
|
|
def new_post (title, link, text, user_id):
|
321
|
331
|
|
# Create a hash_id for the new post
|
322
|
332
|
|
hash_id = random.alphanumeric_string (10)
|
323
|
333
|
|
|
324
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
325
|
|
- |
|
326
|
|
- |
cursor.execute (
|
327
|
|
- |
"""
|
328
|
|
- |
INSERT INTO post (hashId, created, dateCreated, title,
|
329
|
|
- |
link, text, vote, commentsCount, userId)
|
330
|
|
- |
VALUES (%(hash_id)s, NOW(), CURDATE(), %(title)s, %(link)s,
|
331
|
|
- |
%(text)s, 0, 0, %(user)s)
|
332
|
|
- |
""",
|
333
|
|
- |
{
|
334
|
|
- |
'hash_id': hash_id,
|
335
|
|
- |
'title': title,
|
336
|
|
- |
'link': link,
|
337
|
|
- |
'text': text,
|
338
|
|
- |
'user': user_id
|
339
|
|
- |
}
|
340
|
|
- |
)
|
|
334
|
+ |
with db:
|
|
335
|
+ |
db.execute(
|
|
336
|
+ |
"""
|
|
337
|
+ |
INSERT INTO post (hashId, created, dateCreated, title,
|
|
338
|
+ |
link, text, vote, commentsCount, userId)
|
|
339
|
+ |
VALUES (:hash_id, DATETIME(), DATE(), :title, :link,
|
|
340
|
+ |
:text, 0, 0, :user)
|
|
341
|
+ |
""",
|
|
342
|
+ |
{
|
|
343
|
+ |
'hash_id': hash_id,
|
|
344
|
+ |
'title': title,
|
|
345
|
+ |
'link': link,
|
|
346
|
+ |
'text': text,
|
|
347
|
+ |
'user': user_id
|
|
348
|
+ |
}
|
|
349
|
+ |
)
|
341
|
350
|
|
|
342
|
351
|
|
return hash_id
|
343
|
352
|
|
|
358
|
367
|
|
# Remove extra topics if the list is too long
|
359
|
368
|
|
topics = topics[:settings['defaults']['topics_per_post']]
|
360
|
369
|
|
|
361
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
362
|
|
- |
|
363
|
|
- |
# First we delete the existing topics
|
364
|
|
- |
cursor.execute (
|
365
|
|
- |
"""
|
366
|
|
- |
DELETE FROM topic WHERE post_id = %(post)s
|
367
|
|
- |
""",
|
368
|
|
- |
{
|
369
|
|
- |
'post': post_id
|
370
|
|
- |
}
|
371
|
|
- |
)
|
372
|
|
- |
|
373
|
|
- |
# Now insert the new topics
|
374
|
|
- |
cursor.executemany (
|
375
|
|
- |
"""
|
376
|
|
- |
INSERT INTO topic (post_id, name)
|
377
|
|
- |
VALUES (%s, %s)
|
378
|
|
- |
""",
|
379
|
|
- |
[ (post_id, topic) for topic in topics ]
|
380
|
|
- |
)
|
|
370
|
+ |
with db:
|
|
371
|
+ |
# First we delete the existing topics
|
|
372
|
+ |
db.execute (
|
|
373
|
+ |
"""
|
|
374
|
+ |
DELETE
|
|
375
|
+ |
FROM topic
|
|
376
|
+ |
WHERE post_id = :post
|
|
377
|
+ |
""",
|
|
378
|
+ |
{
|
|
379
|
+ |
'post': post_id
|
|
380
|
+ |
}
|
|
381
|
+ |
)
|
|
382
|
+ |
|
|
383
|
+ |
# Now insert the new topics.
|
|
384
|
+ |
# IGNORE duplicates that trigger UNIQUE constraint.
|
|
385
|
+ |
db.executemany (
|
|
386
|
+ |
"""
|
|
387
|
+ |
INSERT OR IGNORE INTO topic (post_id, name)
|
|
388
|
+ |
VALUES (?, ?)
|
|
389
|
+ |
""",
|
|
390
|
+ |
[ (post_id, topic) for topic in topics ]
|
|
391
|
+ |
)
|
381
|
392
|
|
|
382
|
393
|
|
# Retrieve a post
|
383
|
394
|
|
def get_post (hash, session_user_id = None):
|
384
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
385
|
|
- |
|
386
|
|
- |
cursor.execute (
|
387
|
|
- |
"""
|
388
|
|
- |
SELECT P.*, U.username, V.vote AS user_vote
|
389
|
|
- |
FROM post AS P
|
390
|
|
- |
JOIN user AS U ON P.userId = U.id
|
391
|
|
- |
LEFT JOIN vote_post as V ON V.postId = P.id AND V.userId = %(user)s
|
392
|
|
- |
WHERE P.hashId = %(post)s
|
393
|
|
- |
""",
|
394
|
|
- |
{
|
395
|
|
- |
'user': session_user_id,
|
396
|
|
- |
'post': hash
|
397
|
|
- |
}
|
398
|
|
- |
)
|
399
|
|
- |
|
400
|
|
- |
return cursor.fetchone ()
|
|
395
|
+ |
with db:
|
|
396
|
+ |
cursor = db.execute (
|
|
397
|
+ |
"""
|
|
398
|
+ |
SELECT P.*,
|
|
399
|
+ |
U.username,
|
|
400
|
+ |
V.vote AS user_vote
|
|
401
|
+ |
FROM post AS P
|
|
402
|
+ |
JOIN user AS U ON P.userId = U.id
|
|
403
|
+ |
LEFT JOIN vote_post as V ON V.postId = P.id AND V.userId = :user
|
|
404
|
+ |
WHERE P.hashId = :post
|
|
405
|
+ |
""",
|
|
406
|
+ |
{
|
|
407
|
+ |
'user': session_user_id,
|
|
408
|
+ |
'post': hash
|
|
409
|
+ |
}
|
|
410
|
+ |
)
|
|
411
|
+ |
|
|
412
|
+ |
return cursor.fetchone ()
|
401
|
413
|
|
|
402
|
414
|
|
# Update a post
|
403
|
415
|
|
def update_post (title, link, text, post_hash_id, user_id):
|
404
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
405
|
|
- |
|
406
|
|
- |
cursor.execute (
|
407
|
|
- |
"""
|
408
|
|
- |
UPDATE post
|
409
|
|
- |
SET title = %(title)s,
|
410
|
|
- |
link = %(link)s,
|
411
|
|
- |
text = %(text)s
|
412
|
|
- |
WHERE hashId = %(hash_id)s AND userId = %(user)s
|
413
|
|
- |
""",
|
414
|
|
- |
{
|
415
|
|
- |
'title': title,
|
416
|
|
- |
'link': link,
|
417
|
|
- |
'text': text,
|
418
|
|
- |
'hash_id': post_hash_id,
|
419
|
|
- |
'user': user_id
|
420
|
|
- |
}
|
421
|
|
- |
)
|
|
416
|
+ |
with db:
|
|
417
|
+ |
db.execute (
|
|
418
|
+ |
"""
|
|
419
|
+ |
UPDATE post
|
|
420
|
+ |
SET title = :title,
|
|
421
|
+ |
link = :link,
|
|
422
|
+ |
text = :text
|
|
423
|
+ |
WHERE hashId = :hash_id
|
|
424
|
+ |
AND userId = :user
|
|
425
|
+ |
""",
|
|
426
|
+ |
{
|
|
427
|
+ |
'title': title,
|
|
428
|
+ |
'link': link,
|
|
429
|
+ |
'text': text,
|
|
430
|
+ |
'hash_id': post_hash_id,
|
|
431
|
+ |
'user': user_id
|
|
432
|
+ |
}
|
|
433
|
+ |
)
|
422
|
434
|
|
|
423
|
435
|
|
# Retrieve all comments for a specific post
|
424
|
436
|
|
def get_post_comments (post_id, session_user_id = None):
|
425
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
426
|
|
- |
|
427
|
|
- |
cursor.execute (
|
428
|
|
- |
"""
|
429
|
|
- |
SELECT C.*, U.username, V.vote AS user_vote
|
430
|
|
- |
FROM comment AS C
|
431
|
|
- |
JOIN user AS U ON C.userId = U.id
|
432
|
|
- |
LEFT JOIN vote_comment as V ON V.commentId = C.id AND V.userId = %(user)s
|
433
|
|
- |
WHERE C.postId = %(post)s
|
434
|
|
- |
ORDER BY C.vote DESC, C.created ASC
|
435
|
|
- |
""",
|
436
|
|
- |
{
|
437
|
|
- |
'user': session_user_id,
|
438
|
|
- |
'post': post_id
|
439
|
|
- |
}
|
440
|
|
- |
)
|
441
|
|
- |
|
442
|
|
- |
return cursor.fetchall ()
|
|
437
|
+ |
with db:
|
|
438
|
+ |
cursor = db.execute (
|
|
439
|
+ |
"""
|
|
440
|
+ |
SELECT C.*,
|
|
441
|
+ |
U.username,
|
|
442
|
+ |
V.vote AS user_vote
|
|
443
|
+ |
FROM comment AS C
|
|
444
|
+ |
JOIN user AS U ON C.userId = U.id
|
|
445
|
+ |
LEFT JOIN vote_comment as V ON V.commentId = C.id AND V.userId = :user
|
|
446
|
+ |
WHERE C.postId = :post
|
|
447
|
+ |
ORDER BY C.vote DESC,
|
|
448
|
+ |
C.created ASC
|
|
449
|
+ |
""",
|
|
450
|
+ |
{
|
|
451
|
+ |
'user': session_user_id,
|
|
452
|
+ |
'post': post_id
|
|
453
|
+ |
}
|
|
454
|
+ |
)
|
|
455
|
+ |
|
|
456
|
+ |
return cursor.fetchall ()
|
443
|
457
|
|
|
444
|
458
|
|
# Retrieve all topics for a specific post
|
445
|
459
|
|
def get_post_topics (post_id):
|
446
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
447
|
|
- |
|
448
|
|
- |
cursor.execute (
|
449
|
|
- |
"""
|
450
|
|
- |
SELECT T.name
|
451
|
|
- |
FROM topic AS T
|
452
|
|
- |
WHERE T.post_id = %(post)s
|
453
|
|
- |
ORDER BY T.name ASC
|
454
|
|
- |
""",
|
455
|
|
- |
{
|
456
|
|
- |
'post': post_id
|
457
|
|
- |
}
|
458
|
|
- |
)
|
459
|
|
- |
|
460
|
|
- |
return cursor.fetchall ()
|
|
460
|
+ |
with db:
|
|
461
|
+ |
cursor = db.execute (
|
|
462
|
+ |
"""
|
|
463
|
+ |
SELECT T.name
|
|
464
|
+ |
FROM topic AS T
|
|
465
|
+ |
WHERE T.post_id = :post
|
|
466
|
+ |
ORDER BY T.name ASC
|
|
467
|
+ |
""",
|
|
468
|
+ |
{
|
|
469
|
+ |
'post': post_id
|
|
470
|
+ |
}
|
|
471
|
+ |
)
|
|
472
|
+ |
|
|
473
|
+ |
return cursor.fetchall ()
|
461
|
474
|
|
|
462
|
475
|
|
# Submit a new comment to a post
|
463
|
476
|
|
def new_comment (comment_text, post_hash_id, user_id, parent_user_id = None, parent_comment_id = None):
|
467
|
480
|
|
# Retrieve post
|
468
|
481
|
|
post = get_post (post_hash_id)
|
469
|
482
|
|
|
470
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
471
|
|
- |
|
472
|
|
- |
cursor.execute (
|
473
|
|
- |
"""
|
474
|
|
- |
INSERT INTO comment (hashId, created, dateCreated, `read`, text, vote,
|
475
|
|
- |
parentId, parentUserId, postId, userId)
|
476
|
|
- |
VALUES (%(hash_id)s, NOW(), CURDATE(), 0, %(text)s, 0, %(parent_id)s,
|
477
|
|
- |
%(parent_user_id)s, %(post_id)s, %(user)s)
|
478
|
|
- |
""",
|
479
|
|
- |
{
|
480
|
|
- |
'hash_id': hash_id,
|
481
|
|
- |
'text': comment_text,
|
482
|
|
- |
'parent_id': parent_comment_id,
|
483
|
|
- |
'parent_user_id': parent_user_id,
|
484
|
|
- |
'post_id': post['id'],
|
485
|
|
- |
'user': user_id
|
486
|
|
- |
}
|
487
|
|
- |
)
|
488
|
|
- |
|
489
|
|
- |
# Increase comments count for post
|
490
|
|
- |
cursor.execute (
|
491
|
|
- |
"""
|
492
|
|
- |
UPDATE post
|
493
|
|
- |
SET commentsCount = commentsCount + 1
|
494
|
|
- |
WHERE id = %(post)s
|
495
|
|
- |
""",
|
496
|
|
- |
{
|
497
|
|
- |
'post': post['id']
|
498
|
|
- |
}
|
499
|
|
- |
)
|
|
483
|
+ |
with db:
|
|
484
|
+ |
db.execute (
|
|
485
|
+ |
"""
|
|
486
|
+ |
INSERT INTO comment (hashId, created, dateCreated, `read`, text, vote,
|
|
487
|
+ |
parentId, parentUserId, postId, userId)
|
|
488
|
+ |
VALUES (:hash_id, DATETIME(), DATE(), 0, :text, 0, :parent_id,
|
|
489
|
+ |
:parent_user_id, :post_id, :user)
|
|
490
|
+ |
""",
|
|
491
|
+ |
{
|
|
492
|
+ |
'hash_id': hash_id,
|
|
493
|
+ |
'text': comment_text,
|
|
494
|
+ |
'parent_id': parent_comment_id,
|
|
495
|
+ |
'parent_user_id': parent_user_id,
|
|
496
|
+ |
'post_id': post['id'],
|
|
497
|
+ |
'user': user_id
|
|
498
|
+ |
}
|
|
499
|
+ |
)
|
|
500
|
+ |
|
|
501
|
+ |
# Increase comments count for post
|
|
502
|
+ |
db.execute (
|
|
503
|
+ |
"""
|
|
504
|
+ |
UPDATE post
|
|
505
|
+ |
SET commentsCount = commentsCount + 1
|
|
506
|
+ |
WHERE id = :post
|
|
507
|
+ |
""",
|
|
508
|
+ |
{
|
|
509
|
+ |
'post': post['id']
|
|
510
|
+ |
}
|
|
511
|
+ |
)
|
500
|
512
|
|
|
501
|
513
|
|
return hash_id
|
502
|
514
|
|
|
503
|
515
|
|
# Retrieve a single comment
|
504
|
516
|
|
def get_comment (hash_id, session_user_id = None):
|
505
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
506
|
|
- |
|
507
|
|
- |
cursor.execute (
|
508
|
|
- |
"""
|
509
|
|
- |
SELECT
|
510
|
|
- |
C.*,
|
511
|
|
- |
P.hashId AS postHashId,
|
512
|
|
- |
P.title AS postTitle,
|
513
|
|
- |
U.username,
|
514
|
|
- |
V.vote AS user_vote
|
515
|
|
- |
FROM comment AS C
|
516
|
|
- |
JOIN user AS U ON C.userId = U.id
|
517
|
|
- |
JOIN post AS P ON P.id = C.postId
|
518
|
|
- |
LEFT JOIN vote_comment as V ON V.commentId = C.id AND V.userId = %(user)s
|
519
|
|
- |
WHERE C.hashId = %(comment)s
|
520
|
|
- |
""",
|
521
|
|
- |
{
|
522
|
|
- |
'user': session_user_id,
|
523
|
|
- |
'comment': hash_id
|
524
|
|
- |
}
|
525
|
|
- |
)
|
526
|
|
- |
|
527
|
|
- |
return cursor.fetchone ()
|
|
517
|
+ |
with db:
|
|
518
|
+ |
cursor = db.execute(
|
|
519
|
+ |
"""
|
|
520
|
+ |
SELECT C.*,
|
|
521
|
+ |
P.hashId AS postHashId,
|
|
522
|
+ |
P.title AS postTitle,
|
|
523
|
+ |
U.username,
|
|
524
|
+ |
V.vote AS user_vote
|
|
525
|
+ |
FROM comment AS C
|
|
526
|
+ |
JOIN user AS U ON C.userId = U.id
|
|
527
|
+ |
JOIN post AS P ON P.id = C.postId
|
|
528
|
+ |
LEFT JOIN vote_comment as V ON V.commentId = C.id AND V.userId = :user
|
|
529
|
+ |
WHERE C.hashId = :comment
|
|
530
|
+ |
""",
|
|
531
|
+ |
{
|
|
532
|
+ |
'user': session_user_id,
|
|
533
|
+ |
'comment': hash_id
|
|
534
|
+ |
}
|
|
535
|
+ |
)
|
|
536
|
+ |
|
|
537
|
+ |
return cursor.fetchone()
|
528
|
538
|
|
|
529
|
539
|
|
# Retrieve last N newest comments
|
530
|
540
|
|
def get_latest_comments ():
|
531
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
532
|
|
- |
|
533
|
|
- |
cursor.execute (
|
534
|
|
- |
"""
|
535
|
|
- |
SELECT
|
536
|
|
- |
C.*,
|
537
|
|
- |
P.hashId AS postHashId,
|
538
|
|
- |
P.title AS postTitle,
|
539
|
|
- |
U.username
|
540
|
|
- |
FROM comment AS C
|
541
|
|
- |
JOIN user AS U ON C.userId = U.id
|
542
|
|
- |
JOIN post AS P ON P.id = C.postId
|
543
|
|
- |
ORDER BY C.id DESC
|
544
|
|
- |
LIMIT 50
|
545
|
|
- |
""",
|
546
|
|
- |
{
|
547
|
|
- |
}
|
548
|
|
- |
)
|
549
|
|
- |
|
550
|
|
- |
return cursor.fetchall ()
|
|
541
|
+ |
with db:
|
|
542
|
+ |
cursor = db.execute (
|
|
543
|
+ |
"""
|
|
544
|
+ |
SELECT C.*,
|
|
545
|
+ |
P.hashId AS postHashId,
|
|
546
|
+ |
P.title AS postTitle,
|
|
547
|
+ |
U.username
|
|
548
|
+ |
FROM comment AS C
|
|
549
|
+ |
JOIN user AS U ON C.userId = U.id
|
|
550
|
+ |
JOIN post AS P ON P.id = C.postId
|
|
551
|
+ |
ORDER BY C.id DESC
|
|
552
|
+ |
LIMIT 50
|
|
553
|
+ |
""",
|
|
554
|
+ |
{
|
|
555
|
+ |
}
|
|
556
|
+ |
)
|
|
557
|
+ |
|
|
558
|
+ |
return cursor.fetchall ()
|
551
|
559
|
|
|
552
|
560
|
|
# Update a comment
|
553
|
561
|
|
def update_comment (text, comment_hash_id, user_id):
|
554
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
555
|
|
- |
|
556
|
|
- |
cursor.execute (
|
557
|
|
- |
"""
|
558
|
|
- |
UPDATE comment
|
559
|
|
- |
SET text = %(text)s
|
560
|
|
- |
WHERE hashId = %(comment)s AND userId = %(user)s
|
561
|
|
- |
""",
|
562
|
|
- |
{
|
563
|
|
- |
'text': text,
|
564
|
|
- |
'comment': comment_hash_id,
|
565
|
|
- |
'user': user_id
|
566
|
|
- |
}
|
567
|
|
- |
)
|
|
562
|
+ |
with db:
|
|
563
|
+ |
db.execute (
|
|
564
|
+ |
"""
|
|
565
|
+ |
UPDATE comment
|
|
566
|
+ |
SET text = :text
|
|
567
|
+ |
WHERE hashId = :comment AND userId = :user
|
|
568
|
+ |
""",
|
|
569
|
+ |
{
|
|
570
|
+ |
'text': text,
|
|
571
|
+ |
'comment': comment_hash_id,
|
|
572
|
+ |
'user': user_id
|
|
573
|
+ |
}
|
|
574
|
+ |
)
|
568
|
575
|
|
|
569
|
576
|
|
# Add or update vote to a post
|
570
|
577
|
|
def vote_post (post_id, user_id, vote):
|
571
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
572
|
|
- |
|
573
|
|
- |
# Insert or update the user vote
|
574
|
|
- |
cursor.execute (
|
575
|
|
- |
"""
|
576
|
|
- |
INSERT INTO vote_post (vote, datetime, postId, userId)
|
577
|
|
- |
VALUES (%(vote)s, NOW(), %(post)s, %(user)s)
|
578
|
|
- |
ON DUPLICATE KEY UPDATE
|
579
|
|
- |
vote = vote + %(vote)s,
|
580
|
|
- |
datetime = NOW()
|
581
|
|
- |
""",
|
582
|
|
- |
{
|
583
|
|
- |
'vote': vote,
|
584
|
|
- |
'post': post_id,
|
585
|
|
- |
'user': user_id
|
586
|
|
- |
}
|
587
|
|
- |
)
|
588
|
|
- |
|
589
|
|
- |
# Update vote counter for post
|
590
|
|
- |
cursor.execute (
|
591
|
|
- |
"""
|
592
|
|
- |
UPDATE post
|
593
|
|
- |
SET vote = vote + %(vote)s
|
594
|
|
- |
WHERE id = %(post)s
|
595
|
|
- |
""",
|
596
|
|
- |
{
|
597
|
|
- |
'vote': vote,
|
598
|
|
- |
'post': post_id
|
599
|
|
- |
}
|
600
|
|
- |
)
|
|
578
|
+ |
with db:
|
|
579
|
+ |
# Create a new vote for this post, if one doesn't already exist
|
|
580
|
+ |
db.execute(
|
|
581
|
+ |
"""
|
|
582
|
+ |
INSERT OR IGNORE INTO vote_post (vote, datetime, postId, userId)
|
|
583
|
+ |
VALUES (0, DATETIME(), :post, :user)
|
|
584
|
+ |
""",
|
|
585
|
+ |
{
|
|
586
|
+ |
'post': post_id,
|
|
587
|
+ |
'user': user_id
|
|
588
|
+ |
}
|
|
589
|
+ |
)
|
|
590
|
+ |
|
|
591
|
+ |
# Update user vote (+1 or -1)
|
|
592
|
+ |
db.execute(
|
|
593
|
+ |
"""
|
|
594
|
+ |
UPDATE vote_post
|
|
595
|
+ |
SET vote = vote + :vote
|
|
596
|
+ |
WHERE postId = :post AND userId = :user
|
|
597
|
+ |
""",
|
|
598
|
+ |
{
|
|
599
|
+ |
'vote': vote,
|
|
600
|
+ |
'post': post_id,
|
|
601
|
+ |
'user': user_id
|
|
602
|
+ |
}
|
|
603
|
+ |
)
|
|
604
|
+ |
|
|
605
|
+ |
# Update post's total
|
|
606
|
+ |
db.execute (
|
|
607
|
+ |
"""
|
|
608
|
+ |
UPDATE post
|
|
609
|
+ |
SET vote = vote + :vote
|
|
610
|
+ |
WHERE id = :post
|
|
611
|
+ |
""",
|
|
612
|
+ |
{
|
|
613
|
+ |
'vote': vote,
|
|
614
|
+ |
'post': post_id
|
|
615
|
+ |
}
|
|
616
|
+ |
)
|
601
|
617
|
|
|
602
|
618
|
|
# Add or update vote to a comment
|
603
|
619
|
|
def vote_comment (comment_id, user_id, vote):
|
604
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
605
|
|
- |
|
606
|
|
- |
# Insert or update the user vote
|
607
|
|
- |
cursor.execute (
|
608
|
|
- |
"""
|
609
|
|
- |
INSERT INTO vote_comment (vote, datetime, commentId, userId)
|
610
|
|
- |
VALUES (%(vote)s, NOW(), %(comment)s, %(user)s)
|
611
|
|
- |
ON DUPLICATE KEY UPDATE
|
612
|
|
- |
vote = vote + %(vote)s,
|
613
|
|
- |
datetime = NOW()
|
614
|
|
- |
""",
|
615
|
|
- |
{
|
616
|
|
- |
'vote': vote,
|
617
|
|
- |
'comment': comment_id,
|
618
|
|
- |
'user': user_id
|
619
|
|
- |
}
|
620
|
|
- |
)
|
621
|
|
- |
|
622
|
|
- |
# Update vote counter for comment
|
623
|
|
- |
cursor.execute (
|
624
|
|
- |
"""
|
625
|
|
- |
UPDATE comment
|
626
|
|
- |
SET vote = vote + %(vote)s
|
627
|
|
- |
WHERE id = %(comment)s
|
628
|
|
- |
""",
|
629
|
|
- |
{
|
630
|
|
- |
'vote': vote,
|
631
|
|
- |
'comment': comment_id
|
632
|
|
- |
}
|
633
|
|
- |
)
|
|
620
|
+ |
with db:
|
|
621
|
+ |
# Create a new vote for this post, if one doesn't already exist
|
|
622
|
+ |
db.execute (
|
|
623
|
+ |
"""
|
|
624
|
+ |
INSERT INTO vote_comment (vote, datetime, commentId, userId)
|
|
625
|
+ |
VALUES (0, DATETIME(), :comment, :user)
|
|
626
|
+ |
""",
|
|
627
|
+ |
{
|
|
628
|
+ |
'comment': comment_id,
|
|
629
|
+ |
'user': user_id
|
|
630
|
+ |
}
|
|
631
|
+ |
)
|
|
632
|
+ |
|
|
633
|
+ |
# Update user vote (+1 or -1)
|
|
634
|
+ |
db.execute (
|
|
635
|
+ |
"""
|
|
636
|
+ |
UPDATE vote_comment
|
|
637
|
+ |
SET vote = vote + :vote
|
|
638
|
+ |
WHERE commentId = :comment AND userId = :user
|
|
639
|
+ |
""",
|
|
640
|
+ |
{
|
|
641
|
+ |
'vote': vote,
|
|
642
|
+ |
'comment': comment_id,
|
|
643
|
+ |
'user': user_id
|
|
644
|
+ |
}
|
|
645
|
+ |
)
|
|
646
|
+ |
|
|
647
|
+ |
# Update comment's total
|
|
648
|
+ |
db.execute (
|
|
649
|
+ |
"""
|
|
650
|
+ |
UPDATE comment
|
|
651
|
+ |
SET vote = vote + :vote
|
|
652
|
+ |
WHERE id = :comment
|
|
653
|
+ |
""",
|
|
654
|
+ |
{
|
|
655
|
+ |
'vote': vote,
|
|
656
|
+ |
'comment': comment_id
|
|
657
|
+ |
}
|
|
658
|
+ |
)
|
634
|
659
|
|
|
635
|
660
|
|
# Search posts
|
636
|
661
|
|
def search (query, sort='newest', page=0):
|
643
|
668
|
|
if len (query) == 0:
|
644
|
669
|
|
return []
|
645
|
670
|
|
|
646
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
647
|
|
- |
|
648
|
671
|
|
if sort == 'newest':
|
649
|
672
|
|
sort = 'P.created DESC'
|
650
|
673
|
|
if sort == 'points':
|
651
|
674
|
|
sort = 'P.vote DESC'
|
652
|
675
|
|
|
653
|
|
- |
cursor.execute (
|
654
|
|
- |
"""
|
655
|
|
- |
SELECT P.*, U.username
|
656
|
|
- |
FROM post AS P
|
657
|
|
- |
JOIN user AS U ON P.userId = U.id
|
658
|
|
- |
WHERE P.title REGEXP %(query)s
|
659
|
|
- |
ORDER BY {sort}
|
660
|
|
- |
LIMIT %(limit)s
|
661
|
|
- |
OFFSET %(offset)s
|
662
|
|
- |
""".format (sort=sort),
|
663
|
|
- |
{
|
664
|
|
- |
'query': query,
|
665
|
|
- |
'sort': sort,
|
666
|
|
- |
'limit': settings['defaults']['search_results_per_page'],
|
667
|
|
- |
'offset': page * settings['defaults']['search_results_per_page']
|
668
|
|
- |
}
|
669
|
|
- |
)
|
670
|
|
- |
|
671
|
|
- |
return cursor.fetchall ()
|
|
676
|
+ |
with db:
|
|
677
|
+ |
cursor = db.execute (
|
|
678
|
+ |
"""
|
|
679
|
+ |
SELECT P.*,
|
|
680
|
+ |
U.username
|
|
681
|
+ |
FROM post AS P
|
|
682
|
+ |
JOIN user AS U ON P.userId = U.id
|
|
683
|
+ |
WHERE P.title REGEXP :query
|
|
684
|
+ |
ORDER BY {sort}
|
|
685
|
+ |
LIMIT :limit
|
|
686
|
+ |
OFFSET :offset
|
|
687
|
+ |
""".format (sort=sort),
|
|
688
|
+ |
{
|
|
689
|
+ |
'query': query,
|
|
690
|
+ |
'sort': sort,
|
|
691
|
+ |
'limit': settings['defaults']['search_results_per_page'],
|
|
692
|
+ |
'offset': page * settings['defaults']['search_results_per_page']
|
|
693
|
+ |
}
|
|
694
|
+ |
)
|
|
695
|
+ |
|
|
696
|
+ |
return cursor.fetchall ()
|
672
|
697
|
|
|
673
|
698
|
|
# Set reset token for user email
|
674
|
699
|
|
def set_password_reset_token (user_id = None, token = None):
|
675
|
700
|
|
if not user_id or not token:
|
676
|
701
|
|
return
|
677
|
702
|
|
|
678
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
679
|
|
- |
|
680
|
|
- |
cursor.execute (
|
681
|
|
- |
"""
|
682
|
|
- |
UPDATE user
|
683
|
|
- |
SET passwordResetToken = SHA2(%(token)s, 512),
|
684
|
|
- |
passwordResetTokenExpire = NOW() + INTERVAL 1 HOUR
|
685
|
|
- |
WHERE id = %(user)s
|
686
|
|
- |
""",
|
687
|
|
- |
{
|
688
|
|
- |
'user': user_id,
|
689
|
|
- |
'token': token
|
690
|
|
- |
}
|
691
|
|
- |
)
|
|
703
|
+ |
with db:
|
|
704
|
+ |
db.execute (
|
|
705
|
+ |
"""
|
|
706
|
+ |
UPDATE user
|
|
707
|
+ |
SET passwordResetToken = SHA512(:token),
|
|
708
|
+ |
passwordResetTokenExpire = NOW() + INTERVAL 1 HOUR
|
|
709
|
+ |
WHERE id = :user
|
|
710
|
+ |
""",
|
|
711
|
+ |
{
|
|
712
|
+ |
'user': user_id,
|
|
713
|
+ |
'token': token
|
|
714
|
+ |
}
|
|
715
|
+ |
)
|
692
|
716
|
|
|
693
|
717
|
|
# Delete the password reset token for a user
|
694
|
718
|
|
def delete_password_reset_token (user_id = None):
|
695
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
696
|
|
- |
|
697
|
|
- |
cursor.execute (
|
698
|
|
- |
"""
|
699
|
|
- |
UPDATE user
|
700
|
|
- |
SET passwordResetToken = NULL,
|
701
|
|
- |
passwordResetTokenExpire = NULL
|
702
|
|
- |
WHERE id = %(user)s
|
703
|
|
- |
""",
|
704
|
|
- |
{
|
705
|
|
- |
'user': user_id
|
706
|
|
- |
}
|
707
|
|
- |
)
|
|
719
|
+ |
with db:
|
|
720
|
+ |
db.execute (
|
|
721
|
+ |
"""
|
|
722
|
+ |
UPDATE user
|
|
723
|
+ |
SET passwordResetToken = NULL,
|
|
724
|
+ |
passwordResetTokenExpire = NULL
|
|
725
|
+ |
WHERE id = :user
|
|
726
|
+ |
""",
|
|
727
|
+ |
{
|
|
728
|
+ |
'user': user_id
|
|
729
|
+ |
}
|
|
730
|
+ |
)
|
708
|
731
|
|
|
709
|
732
|
|
# Check if a reset token has expired.
|
710
|
733
|
|
def is_password_reset_token_valid (user_id = None):
|
711
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
712
|
|
- |
|
713
|
|
- |
cursor.execute (
|
714
|
|
- |
"""
|
715
|
|
- |
SELECT COUNT(1) AS valid
|
716
|
|
- |
FROM user
|
717
|
|
- |
WHERE id = %(user)s AND
|
718
|
|
- |
passwordResetToken IS NOT NULL AND
|
719
|
|
- |
passwordResetTokenExpire IS NOT NULL AND
|
720
|
|
- |
passwordResetTokenExpire > NOW()
|
721
|
|
- |
""",
|
722
|
|
- |
{
|
723
|
|
- |
'user': user_id
|
724
|
|
- |
}
|
725
|
|
- |
)
|
726
|
|
- |
|
727
|
|
- |
return cursor.fetchone ()['valid'] == 1
|
|
734
|
+ |
with db:
|
|
735
|
+ |
cursor = db.execute(
|
|
736
|
+ |
"""
|
|
737
|
+ |
SELECT COUNT(1) AS valid
|
|
738
|
+ |
FROM user
|
|
739
|
+ |
WHERE id = :user
|
|
740
|
+ |
AND passwordResetToken IS NOT NULL
|
|
741
|
+ |
AND passwordResetTokenExpire IS NOT NULL
|
|
742
|
+ |
AND passwordResetTokenExpire > DATE()
|
|
743
|
+ |
""",
|
|
744
|
+ |
{
|
|
745
|
+ |
'user': user_id
|
|
746
|
+ |
}
|
|
747
|
+ |
)
|
|
748
|
+ |
|
|
749
|
+ |
return cursor.fetchone()['valid'] == 1
|
728
|
750
|
|
|
729
|
751
|
|
# Reset user password
|
730
|
752
|
|
def reset_password (username = None, email = None, new_password = None, secret_token = None):
|
731
|
753
|
|
if not new_password:
|
732
|
754
|
|
return
|
733
|
755
|
|
|
734
|
|
- |
cursor = db.cursor (MySQLdb.cursors.DictCursor)
|
735
|
|
- |
|
736
|
|
- |
cursor.execute (
|
737
|
|
- |
"""
|
738
|
|
- |
UPDATE user
|
739
|
|
- |
SET password = SHA2(CONCAT(%(password)s, `salt`), 512),
|
740
|
|
- |
passwordResetToken = NULL,
|
741
|
|
- |
passwordResetTokenExpire = NULL
|
742
|
|
- |
WHERE username = %(user)s AND
|
743
|
|
- |
email = %(email)s AND
|
744
|
|
- |
passwordResetToken = SHA2(%(token)s, 512) AND
|
745
|
|
- |
passwordResetTokenExpire > NOW()
|
746
|
|
- |
""",
|
747
|
|
- |
{
|
748
|
|
- |
'password': new_password,
|
749
|
|
- |
'user': username,
|
750
|
|
- |
'email': email,
|
751
|
|
- |
'token': secret_token
|
752
|
|
- |
}
|
753
|
|
- |
)
|
|
756
|
+ |
with db:
|
|
757
|
+ |
db.execute (
|
|
758
|
+ |
"""
|
|
759
|
+ |
UPDATE user
|
|
760
|
+ |
SET password = SHA512(:password || `salt`),
|
|
761
|
+ |
passwordResetToken = NULL,
|
|
762
|
+ |
passwordResetTokenExpire = NULL
|
|
763
|
+ |
WHERE username = :user
|
|
764
|
+ |
AND email = :email
|
|
765
|
+ |
AND passwordResetToken = SHA512(:token)
|
|
766
|
+ |
AND passwordResetTokenExpire > DATE()
|
|
767
|
+ |
""",
|
|
768
|
+ |
{
|
|
769
|
+ |
'password': new_password,
|
|
770
|
+ |
'user': username,
|
|
771
|
+ |
'email': email,
|
|
772
|
+ |
'token': secret_token
|
|
773
|
+ |
}
|
|
774
|
+ |
)
|
754
|
775
|
|
|
755
|
776
|
|
|
756
|
777
|
|
|