@@ -36,7 +36,7 @@ def create(*, user_id, text, review_id, is_draft=False):
36
36
Returns:
37
37
the newly created comment in dict form
38
38
"""
39
- with db .engine .connect () as connection :
39
+ with db .engine .begin () as connection :
40
40
result = connection .execute (sqlalchemy .text ("""
41
41
INSERT INTO comment (user_id, review_id, is_draft)
42
42
VALUES (:user_id, :review_id, :is_draft)
@@ -46,8 +46,8 @@ def create(*, user_id, text, review_id, is_draft=False):
46
46
'review_id' : review_id ,
47
47
'is_draft' : is_draft ,
48
48
})
49
- comment_id = result .fetchone ()[ 'id' ]
50
- db_comment_revision .create (comment_id , text )
49
+ comment_id = result .fetchone (). id
50
+ db_comment_revision .create (connection , comment_id , text )
51
51
return get_by_id (comment_id )
52
52
53
53
@@ -88,6 +88,7 @@ def get_by_id(comment_id):
88
88
"user".created as user_created,
89
89
"user".display_name,
90
90
"user".musicbrainz_id,
91
+ COALESCE("user".musicbrainz_id, "user".id::text) as user_ref,
91
92
"user".is_blocked
92
93
FROM comment c
93
94
JOIN comment_revision cr ON c.id = cr.comment_id
@@ -99,9 +100,9 @@ def get_by_id(comment_id):
99
100
'comment_id' : comment_id ,
100
101
})
101
102
102
- comment = result .fetchone ()
103
+ comment = result .mappings (). first ()
103
104
if not comment :
104
- raise db_exceptions .NoDataFoundException ('Can\' t find comment with ID: {id}' .format (id = comment_id ))
105
+ raise db_exceptions .NoDataFoundException ('Can’ t find comment with ID: {id}' .format (id = comment_id ))
105
106
106
107
comment = dict (comment )
107
108
comment ['last_revision' ] = {
@@ -115,6 +116,7 @@ def get_by_id(comment_id):
115
116
'email' : comment .pop ('email' ),
116
117
'created' : comment .pop ('user_created' ),
117
118
'musicbrainz_username' : comment .pop ('musicbrainz_id' ),
119
+ 'user_ref' : comment .pop ('user_ref' ),
118
120
'is_blocked' : comment .pop ('is_blocked' )
119
121
})
120
122
return comment
@@ -167,6 +169,7 @@ def list_comments(*, review_id=None, user_id=None, limit=20, offset=0, inc_hidde
167
169
"user".created as user_created,
168
170
"user".display_name,
169
171
"user".musicbrainz_id,
172
+ COALESCE("user".musicbrainz_id, "user".id::text) as user_ref,
170
173
"user".is_blocked,
171
174
MIN(comment_revision.timestamp) as created,
172
175
latest_revision.id as last_revision_id,
@@ -196,7 +199,7 @@ def list_comments(*, review_id=None, user_id=None, limit=20, offset=0, inc_hidde
196
199
OFFSET :offset
197
200
""" .format (where_clause = filterstr )), query_vals )
198
201
199
- rows = [dict (row ) for row in result .fetchall ()]
202
+ rows = [dict (row ) for row in result .mappings ()]
200
203
for row in rows :
201
204
row ['last_revision' ] = {
202
205
'id' : row .pop ('last_revision_id' ),
@@ -208,6 +211,7 @@ def list_comments(*, review_id=None, user_id=None, limit=20, offset=0, inc_hidde
208
211
'display_name' : row .pop ('display_name' ),
209
212
'is_blocked' : row .pop ('is_blocked' ),
210
213
'musicbrainz_username' : row .pop ('musicbrainz_id' ),
214
+ 'user_ref' : row .pop ('user_ref' ),
211
215
'email' : row .pop ('email' ),
212
216
'created' : row .pop ('user_created' ),
213
217
})
@@ -221,7 +225,7 @@ def delete(comment_id):
221
225
Args:
222
226
comment_id (uuid): the ID of the comment to be deleted.
223
227
"""
224
- with db .engine .connect () as connection :
228
+ with db .engine .begin () as connection :
225
229
connection .execute (sqlalchemy .text ("""
226
230
DELETE
227
231
FROM comment
@@ -262,15 +266,15 @@ def update(comment_id, *, text=None, is_draft=None, is_hidden=None):
262
266
263
267
setstr = ', ' .join (updates )
264
268
update_data ['comment_id' ] = comment_id
265
- with db .engine .connect () as connection :
269
+ with db .engine .begin () as connection :
266
270
connection .execute (sqlalchemy .text ("""
267
271
UPDATE comment
268
272
SET {setstr}
269
273
WHERE id = :comment_id
270
274
""" .format (setstr = setstr )), update_data )
271
275
272
- if text is not None :
273
- db_comment_revision .create (comment_id , text )
276
+ if text is not None :
277
+ db_comment_revision .create (connection , comment_id , text )
274
278
275
279
276
280
def count_comments (* , review_id = None , user_id = None , is_hidden = None , is_draft = None ):
@@ -313,4 +317,4 @@ def count_comments(*, review_id=None, user_id=None, is_hidden=None, is_draft=Non
313
317
FROM comment
314
318
{where_condition}
315
319
""" .format (where_condition = filterstr )), filter_data )
316
- return result .fetchone ()[ 0 ]
320
+ return result .fetchone (). count
0 commit comments