This repository has been archived by the owner on May 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.py
2553 lines (2331 loc) · 98 KB
/
api.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# This file is part of Invenio.
# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012,
# 2013, 2014, 2015 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""Comment and review for records."""
import cgi
import math
import os
import re
import shutil
import time
from datetime import datetime, timedelta
from six import iteritems
from invenio_base.i18n import gettext_set_language, wash_language
from invenio.config import CFG_COMMENTSDIR, CFG_SITE_LANG, CFG_SITE_NAME, \
CFG_SITE_RECORD, CFG_SITE_SUPPORT_EMAIL, CFG_SITE_URL, \
CFG_WEBALERT_ALERT_ENGINE_EMAIL, CFG_WEBCOMMENT_ADMIN_NOTIFICATION_LEVEL, \
CFG_WEBCOMMENT_ALERT_ENGINE_EMAIL, CFG_WEBCOMMENT_ALLOW_COMMENTS, \
CFG_WEBCOMMENT_ALLOW_REVIEWS, CFG_WEBCOMMENT_ALLOW_SHORT_REVIEWS, \
CFG_WEBCOMMENT_DEFAULT_MODERATOR, CFG_WEBCOMMENT_EMAIL_REPLIES_TO, \
CFG_WEBCOMMENT_MAX_COMMENT_THREAD_DEPTH, \
CFG_WEBCOMMENT_NB_REPORTS_BEFORE_SEND_EMAIL_TO_ADMIN, \
CFG_WEBCOMMENT_RESTRICTION_DATAFIELD, CFG_WEBCOMMENT_ROUND_DATAFIELD, \
CFG_WEBCOMMENT_TIMELIMIT_PROCESSING_COMMENTS_IN_SECONDS
from invenio_ext.email import send_email
from invenio_ext.logging import register_exception
from invenio_ext.login.legacy_user import UserInfo
from invenio.legacy.bibrecord import get_fieldvalues
from invenio.legacy.dbquery import datetime_format, run_sql
from invenio.legacy.search_engine import guess_primary_collection_of_a_record
from invenio_utils.date import convert_datestruct_to_datetext, \
convert_datetext_to_dategui, datetext_default
from invenio_utils.html import tidy_html
from invenio_utils.htmlwasher import EmailWasher
from invenio_utils.mail import email_quote_txt, email_quoted_txt2html
from invenio_utils.url import wash_url_argument
from invenio_accounts.models import User
from invenio_access.engine import acc_authorize_action
from invenio_records.access import check_user_can_view_record
from .config import CFG_WEBCOMMENT_ACTION_CODE, InvenioWebCommentError, \
InvenioWebCommentWarning
from .models import CmtRECORDCOMMENT
try:
import invenio.legacy.template
webcomment_templates = invenio.legacy.template.load('webcomment')
except:
pass
def perform_request_display_comments_or_remarks(
req,
recID,
display_order='od',
display_since='all',
nb_per_page=100,
page=1,
ln=CFG_SITE_LANG,
voted=-1,
reported=-1,
subscribed=0,
reviews=0,
uid=-1,
can_send_comments=False,
can_attach_files=False,
user_is_subscribed_to_discussion=False,
user_can_unsubscribe_from_discussion=False,
display_comment_rounds=None):
"""
Returns all the comments (reviews) of a specific internal record or external basket record.
:param recID: record id where (internal record IDs > 0) or (external basket record IDs < -100)
:param display_order: hh = highest helpful score, review only
lh = lowest helpful score, review only
hs = highest star score, review only
ls = lowest star score, review only
od = oldest date
nd = newest date
:param display_since: all= no filtering by date
nd = n days ago
nw = n weeks ago
nm = n months ago
ny = n years ago
where n is a single digit integer between 0 and 9
:param nb_per_page: number of results per page
:param page: results page
:param voted: boolean, active if user voted for a review, see perform_request_vote function
:param reported: boolean, active if user reported a certain comment/review, perform_request_report function
:param subscribed: int, 1 if user just subscribed to discussion, -1 if unsubscribed
:param reviews: boolean, enabled if reviews, disabled for comments
:param uid: the id of the user who is reading comments
:param can_send_comments: if user can send comment or not
:param can_attach_files: if user can attach file to comment or not
:param user_is_subscribed_to_discussion: True if user already receives new comments by email
:param user_can_unsubscribe_from_discussion: True is user is allowed to unsubscribe from discussion
:return html body.
"""
_ = gettext_set_language(ln)
warnings = []
nb_reviews = 0
nb_comments = 0
# wash arguments
recID = wash_url_argument(recID, 'int')
ln = wash_language(ln)
display_order = wash_url_argument(display_order, 'str')
display_since = wash_url_argument(display_since, 'str')
nb_per_page = wash_url_argument(nb_per_page, 'int')
page = wash_url_argument(page, 'int')
voted = wash_url_argument(voted, 'int')
reported = wash_url_argument(reported, 'int')
reviews = wash_url_argument(reviews, 'int')
# vital argument check
(valid, error_body) = check_recID_is_in_range(recID, warnings, ln)
if not(valid):
return error_body
# CERN hack begins: filter out ATLAS comments
from invenio.config import CFG_CERN_SITE
if CFG_CERN_SITE:
restricted_comments_p = False
for report_number in get_fieldvalues(recID, '088__a'):
if report_number.startswith("ATL-"):
restricted_comments_p = True
break
if restricted_comments_p:
err_code, err_msg = acc_authorize_action(
uid, 'viewrestrcoll', collection='ATLAS Communications')
if err_code:
return err_msg
# CERN hack ends
# Query the database and filter results
user_info = UserInfo(uid)
res = query_retrieve_comments_or_remarks(
recID,
display_order,
display_since,
reviews,
user_info=user_info)
# res2 = query_retrieve_comments_or_remarks(recID, display_order, display_since, not reviews, user_info=user_info)
nb_res = len(res)
filters = [
CmtRECORDCOMMENT.id_bibrec == recID,
CmtRECORDCOMMENT.status.notin_(['dm', 'da'])
]
nb_reviews = CmtRECORDCOMMENT.count(*(
filters + [
CmtRECORDCOMMENT.star_score > 0
]
))
nb_comments = CmtRECORDCOMMENT.count(*(
filters + [
CmtRECORDCOMMENT.star_score == 0
]
))
# checking non vital arguemnts - will be set to default if wrong
# if page <= 0 or page.lower() != 'all':
if page < 0:
page = 1
try:
raise InvenioWebCommentWarning(
_('Bad page number --> showing first page.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_INVALID_PAGE_NB',))
if nb_per_page < 0:
nb_per_page = 100
try:
raise InvenioWebCommentWarning(
_('Bad number of results per page --> showing 10 results per page.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_INVALID_NB_RESULTS_PER_PAGE',))
if CFG_WEBCOMMENT_ALLOW_REVIEWS and reviews:
if display_order not in ['od', 'nd', 'hh', 'lh', 'hs', 'ls']:
display_order = 'hh'
try:
raise InvenioWebCommentWarning(
_('Bad display order --> showing most helpful first.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_INVALID_REVIEW_DISPLAY_ORDER',))
else:
if display_order not in ['od', 'nd']:
display_order = 'od'
try:
raise InvenioWebCommentWarning(
_('Bad display order --> showing oldest first.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_INVALID_DISPLAY_ORDER',))
if not display_comment_rounds:
display_comment_rounds = []
# filter results according to page and number of reults per page
if nb_per_page > 0:
if nb_res > 0:
last_page = int(math.ceil(nb_res / float(nb_per_page)))
else:
last_page = 1
if page > last_page:
page = 1
try:
raise InvenioWebCommentWarning(
_('Bad page number --> showing first page.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(("WRN_WEBCOMMENT_INVALID_PAGE_NB",))
if nb_res > nb_per_page: # if more than one page of results
if page < last_page:
res = res[(page - 1) * (nb_per_page): (page * nb_per_page)]
else:
res = res[(page - 1) * (nb_per_page):]
else: # one page of results
pass
else:
last_page = 1
# Add information regarding visibility of comment for user
user_collapsed_comments = get_user_collapsed_comments_for_record(
uid,
recID)
if reviews:
res = [row[:] + (row[10] in user_collapsed_comments,) for row in res]
else:
res = [row[:] + (row[6] in user_collapsed_comments,) for row in res]
# Send to template
avg_score = 0.0
# comments not allowed by admin
if not CFG_WEBCOMMENT_ALLOW_COMMENTS and not CFG_WEBCOMMENT_ALLOW_REVIEWS:
try:
raise InvenioWebCommentError(
_('Comments on records have been disallowed by the administrator.'))
except InvenioWebCommentError as exc:
register_exception(req=req)
body = webcomment_templates.tmpl_error(exc.message, ln)
return body
# errors.append(('ERR_WEBCOMMENT_COMMENTS_NOT_ALLOWED',))
if reported > 0:
try:
raise InvenioWebCommentWarning(
_('Your feedback has been recorded, many thanks.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, 'green'))
# warnings.append(('WRN_WEBCOMMENT_FEEDBACK_RECORDED',))
elif reported == 0:
try:
raise InvenioWebCommentWarning(
_('You have already reported an abuse for this comment.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_ALREADY_REPORTED',))
elif reported == -2:
try:
raise InvenioWebCommentWarning(
_('The comment you have reported no longer exists.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_INVALID_REPORT',))
if CFG_WEBCOMMENT_ALLOW_REVIEWS and reviews:
avg_score = calculate_avg_score(res)
if voted > 0:
try:
raise InvenioWebCommentWarning(
_('Your feedback has been recorded, many thanks.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, 'green'))
# warnings.append(('WRN_WEBCOMMENT_FEEDBACK_RECORDED',))
elif voted == 0:
try:
raise InvenioWebCommentWarning(
_('Sorry, you have already voted. This vote has not been recorded.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, ''))
# warnings.append(('WRN_WEBCOMMENT_ALREADY_VOTED',))
if subscribed == 1:
try:
raise InvenioWebCommentWarning(
_('You have been subscribed to this discussion. From now on, you will receive an email whenever a new comment is posted.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, 'green'))
# warnings.append(('WRN_WEBCOMMENT_SUBSCRIBED',))
elif subscribed == -1:
try:
raise InvenioWebCommentWarning(
_('You have been unsubscribed from this discussion.'))
except InvenioWebCommentWarning as exc:
register_exception(stream='warning', req=req)
warnings.append((exc.message, 'green'))
# warnings.append(('WRN_WEBCOMMENT_UNSUBSCRIBED',))
grouped_comments = group_comments_by_round(res, reviews)
# Clean list of comments round names
if not display_comment_rounds:
display_comment_rounds = []
elif 'all' in display_comment_rounds:
display_comment_rounds = [cmtgrp[0] for cmtgrp in grouped_comments]
elif 'latest' in display_comment_rounds:
if grouped_comments:
display_comment_rounds.append(grouped_comments[-1][0])
display_comment_rounds.remove('latest')
body = webcomment_templates.tmpl_get_comments(
req,
recID,
ln,
nb_per_page,
page,
last_page,
display_order,
display_since,
CFG_WEBCOMMENT_ALLOW_REVIEWS,
grouped_comments,
nb_comments,
avg_score,
warnings,
border=0,
reviews=reviews,
total_nb_reviews=nb_reviews,
uid=uid,
can_send_comments=can_send_comments,
can_attach_files=can_attach_files,
user_is_subscribed_to_discussion=user_is_subscribed_to_discussion,
user_can_unsubscribe_from_discussion=user_can_unsubscribe_from_discussion,
display_comment_rounds=display_comment_rounds)
return body
def perform_request_vote(cmt_id, client_ip_address, value, uid=-1):
"""
Vote positively or negatively for a comment/review
:param cmt_id: review id
:param value: +1 for voting positively
-1 for voting negatively
:return: integer 1 if successful, integer 0 if not
"""
cmt_id = wash_url_argument(cmt_id, 'int')
client_ip_address = wash_url_argument(client_ip_address, 'str')
value = wash_url_argument(value, 'int')
uid = wash_url_argument(uid, 'int')
if cmt_id > 0 and value in [-1,
1] and check_user_can_vote(cmt_id,
client_ip_address,
uid):
action_date = convert_datestruct_to_datetext(time.localtime())
action_code = CFG_WEBCOMMENT_ACTION_CODE['VOTE']
# FIXME compatibility with postgresql
query = """INSERT INTO "cmtACTIONHISTORY" ("id_cmtRECORDCOMMENT",
id_bibrec, id_user, client_host, action_time,
action_code)
VALUES (%s, NULL ,%s, inet_aton(%s), %s, %s)"""
params = (cmt_id, uid, client_ip_address, action_date, action_code)
run_sql(query, params)
return query_record_useful_review(cmt_id, value)
else:
return 0
def check_user_can_comment(recID, client_ip_address, uid=-1):
""" Check if a user hasn't already commented within the last seconds
time limit: CFG_WEBCOMMENT_TIMELIMIT_PROCESSING_COMMENTS_IN_SECONDS
:param recID: record id
:param client_ip_address: IP => use: str(req.remote_ip)
:param uid: user id, as given by invenio.legacy.webuser.getUid(req)
"""
recID = wash_url_argument(recID, 'int')
client_ip_address = wash_url_argument(client_ip_address, 'str')
uid = wash_url_argument(uid, 'int')
max_action_time = time.time() - \
CFG_WEBCOMMENT_TIMELIMIT_PROCESSING_COMMENTS_IN_SECONDS
max_action_time = convert_datestruct_to_datetext(
time.localtime(max_action_time))
action_code = CFG_WEBCOMMENT_ACTION_CODE['ADD_COMMENT']
query = """SELECT id_bibrec
FROM "cmtACTIONHISTORY"
WHERE id_bibrec=%s AND
action_code=%s AND
action_time>%s
"""
params = (recID, action_code, max_action_time)
if uid < 0:
query += " AND client_host=inet_aton(%s)"
params += (client_ip_address,)
else:
query += " AND id_user=%s"
params += (uid,)
res = run_sql(query, params)
return len(res) == 0
def check_user_can_review(recID, client_ip_address, uid=-1):
""" Check if a user hasn't already reviewed within the last seconds
time limit: CFG_WEBCOMMENT_TIMELIMIT_PROCESSING_REVIEWS_IN_SECONDS
:param recID: record ID
:param client_ip_address: IP => use: str(req.remote_ip)
:param uid: user id, as given by invenio.legacy.webuser.getUid(req)
"""
action_code = CFG_WEBCOMMENT_ACTION_CODE['ADD_REVIEW']
query = """SELECT id_bibrec
FROM "cmtACTIONHISTORY"
WHERE id_bibrec=%s AND
action_code=%s
"""
params = (recID, action_code)
if uid < 0:
query += " AND client_host=inet_aton(%s)"
params += (client_ip_address,)
else:
query += " AND id_user=%s"
params += (uid,)
res = run_sql(query, params)
return len(res) == 0
def check_user_can_vote(cmt_id, client_ip_address, uid=-1):
""" Checks if a user hasn't already voted
:param cmt_id: comment id
:param client_ip_address: IP => use: str(req.remote_ip)
:param uid: user id, as given by invenio.legacy.webuser.getUid(req)
"""
cmt_id = wash_url_argument(cmt_id, 'int')
client_ip_address = wash_url_argument(client_ip_address, 'str')
uid = wash_url_argument(uid, 'int')
query = """SELECT "id_cmtRECORDCOMMENT"
FROM "cmtACTIONHISTORY"
WHERE "id_cmtRECORDCOMMENT"=%s"""
params = (cmt_id,)
if uid < 0:
query += " AND client_host=inet_aton(%s)"
params += (client_ip_address,)
else:
query += " AND id_user=%s"
params += (uid, )
res = run_sql(query, params)
return (len(res) == 0)
def get_comment_collection(cmt_id):
"""
Extract the collection where the comment is written
"""
query = """SELECT id_bibrec FROM "cmtRECORDCOMMENT" WHERE id=%s"""
recid = run_sql(query, (cmt_id,))
record_primary_collection = guess_primary_collection_of_a_record(
recid[0][0])
return record_primary_collection
def get_collection_moderators(collection):
"""
Return the list of comment moderators for the given collection.
"""
from invenio_access.engine import acc_get_authorized_emails
res = list(
acc_get_authorized_emails(
'moderatecomments',
collection=collection))
if not res:
return [CFG_WEBCOMMENT_DEFAULT_MODERATOR, ]
return res
def perform_request_report(cmt_id, client_ip_address, uid=-1):
"""
Report a comment/review for inappropriate content.
Will send an email to the administrator if number of reports is a multiple
of CFG_WEBCOMMENT_NB_REPORTS_BEFORE_SEND_EMAIL_TO_ADMIN
:param cmt_id: comment id
:return: integer 1 if successful, integer 0 if not. -2 if comment does not exist
"""
cmt_id = wash_url_argument(cmt_id, 'int')
if cmt_id <= 0:
return 0
(query_res, nb_abuse_reports) = query_record_report_this(cmt_id)
if query_res == 0:
return 0
elif query_res == -2:
return -2
if not(check_user_can_report(cmt_id, client_ip_address, uid)):
return 0
action_date = convert_datestruct_to_datetext(time.localtime())
action_code = CFG_WEBCOMMENT_ACTION_CODE['REPORT_ABUSE']
query = """INSERT INTO "cmtACTIONHISTORY" ("id_cmtRECORDCOMMENT", id_bibrec,
id_user, client_host, action_time, action_code)
VALUES (%s, NULL, %s, inet_aton(%s), %s, %s)"""
params = (cmt_id, uid, client_ip_address, action_date, action_code)
run_sql(query, params)
if nb_abuse_reports % CFG_WEBCOMMENT_NB_REPORTS_BEFORE_SEND_EMAIL_TO_ADMIN == 0:
(cmt_id2,
id_bibrec,
id_user,
cmt_body,
cmt_date,
cmt_star,
cmt_vote, cmt_nb_votes_total,
cmt_title,
cmt_reported,
round_name,
restriction) = query_get_comment(cmt_id)
(user_nb_abuse_reports,
user_votes,
user_nb_votes_total) = query_get_user_reports_and_votes(int(id_user))
(nickname,
user_email,
last_login) = query_get_user_contact_info(id_user)
from_addr = '%s Alert Engine <%s>' % (
CFG_SITE_NAME, CFG_WEBALERT_ALERT_ENGINE_EMAIL)
comment_collection = get_comment_collection(cmt_id)
to_addrs = get_collection_moderators(comment_collection)
subject = "A comment has been reported as inappropriate by a user"
body = '''
The following comment has been reported a total of %(cmt_reported)s times.
Author: nickname = %(nickname)s
email = %(user_email)s
user_id = %(uid)s
This user has:
total number of reports = %(user_nb_abuse_reports)s
%(votes)s
Comment: comment_id = %(cmt_id)s
record_id = %(id_bibrec)s
date written = %(cmt_date)s
nb reports = %(cmt_reported)s
%(review_stuff)s
body =
---start body---
%(cmt_body)s
---end body---
Please go to the record page %(comment_admin_link)s to delete this message if necessary. A warning will be sent to the user in question.''' % \
{'cfg-report_max': CFG_WEBCOMMENT_NB_REPORTS_BEFORE_SEND_EMAIL_TO_ADMIN,
'nickname': nickname,
'user_email': user_email,
'uid': id_user,
'user_nb_abuse_reports': user_nb_abuse_reports,
'user_votes': user_votes,
'votes': CFG_WEBCOMMENT_ALLOW_REVIEWS and
"total number of positive votes\t= %s\n\t\ttotal number of negative votes\t= %s" %
(user_votes,
(user_nb_votes_total - user_votes)) or "\n",
'cmt_id': cmt_id,
'id_bibrec': id_bibrec,
'cmt_date': cmt_date,
'cmt_reported': cmt_reported,
'review_stuff': CFG_WEBCOMMENT_ALLOW_REVIEWS and
"star score\t= %s\n\treview title\t= %s" % (
cmt_star,
cmt_title) or "",
'cmt_body': cmt_body,
'comment_admin_link': CFG_SITE_URL + "/" + CFG_SITE_RECORD + "/" + str(id_bibrec) + '/comments#' + str(cmt_id),
'user_admin_link': "user_admin_link" # ! FIXME
}
# FIXME to be added to email when websession module is over:
# If you wish to ban the user, you can do so via the User Admin Panel
# %(user_admin_link)s.
send_email(from_addr, to_addrs, subject, body)
return 1
def check_user_can_report(cmt_id, client_ip_address, uid=-1):
""" Checks if a user hasn't already reported a comment
:param cmt_id: comment id
:param client_ip_address: IP => use: str(req.remote_ip)
:param uid: user id, as given by invenio.legacy.webuser.getUid(req)
"""
cmt_id = wash_url_argument(cmt_id, 'int')
client_ip_address = wash_url_argument(client_ip_address, 'str')
uid = wash_url_argument(uid, 'int')
query = """SELECT "id_cmtRECORDCOMMENT"
FROM "cmtACTIONHISTORY"
WHERE "id_cmtRECORDCOMMENT"=%s"""
params = (uid,)
if uid < 0:
query += " AND client_host=inet_aton(%s)"
params += (client_ip_address,)
else:
query += " AND id_user=%s"
params += (uid,)
res = run_sql(query, params)
return (len(res) == 0)
def query_get_user_contact_info(uid):
"""
Get the user contact information
:return: tuple (nickname, email, last_login), if none found return ()
Note: for the moment, if no nickname, will return email address up to the ':'
"""
# FIXME compatibility with postgresql
query1 = """SELECT nickname, email,
""" + \
datetime_format('last_login') + \
"""
FROM "user" WHERE id=%s"""
params1 = (uid,)
res1 = run_sql(query1, params1)
if res1:
return res1[0]
else:
return ()
def query_get_user_reports_and_votes(uid):
"""
Retrieve total number of reports and votes of a particular user
:param uid: user id
:return: tuple (total_nb_reports, total_nb_votes_yes, total_nb_votes_total)
if none found return ()
"""
query1 = """SELECT nb_votes_yes,
nb_votes_total,
nb_abuse_reports
FROM "cmtRECORDCOMMENT"
WHERE id_user=%s"""
params1 = (uid,)
res1 = run_sql(query1, params1)
if len(res1) == 0:
return ()
nb_votes_yes = nb_votes_total = nb_abuse_reports = 0
for cmt_tuple in res1:
nb_votes_yes += int(cmt_tuple[0])
nb_votes_total += int(cmt_tuple[1])
nb_abuse_reports += int(cmt_tuple[2])
return (nb_abuse_reports, nb_votes_yes, nb_votes_total)
def query_get_comment(comID):
"""
Get all fields of a comment
:param comID: comment id
:return: tuple (comID, id_bibrec, id_user, body, date_creation, star_score, nb_votes_yes, nb_votes_total, title, nb_abuse_reports, round_name, restriction)
if none found return ()
"""
query1 = """SELECT id,
id_bibrec,
id_user,
body,
""" + \
datetime_format('date_creation') + ', ' \
"""
star_score,
nb_votes_yes,
nb_votes_total,
title,
nb_abuse_reports,
round_name,
restriction
FROM "cmtRECORDCOMMENT"
WHERE id=%s"""
params1 = (comID,)
res1 = run_sql(query1, params1)
if len(res1) > 0:
return res1[0]
else:
return ()
def query_record_report_this(comID):
"""
Increment the number of reports for a comment
:param comID: comment id
:return: tuple (success, new_total_nb_reports_for_this_comment) where
success is integer 1 if success, integer 0 if not, -2 if comment does not exist
"""
# retrieve nb_abuse_reports
query1 = """SELECT nb_abuse_reports FROM "cmtRECORDCOMMENT" WHERE id=%s"""
params1 = (comID,)
res1 = run_sql(query1, params1)
if len(res1) == 0:
return (-2, 0)
#increment and update
nb_abuse_reports = int(res1[0][0]) + 1
query2 = """UPDATE "cmtRECORDCOMMENT" SET nb_abuse_reports=%s WHERE id=%s"""
params2 = (nb_abuse_reports, comID)
res2 = run_sql(query2, params2)
return (int(res2), nb_abuse_reports)
def query_record_useful_review(comID, value):
"""
private funciton
Adjust the number of useful votes and number of total votes for a comment.
:param comID: comment id
:param value: +1 or -1
:return: integer 1 if successful, integer 0 if not
"""
# retrieve nb_useful votes
query1 = """SELECT nb_votes_total, nb_votes_yes FROM "cmtRECORDCOMMENT" WHERE id=%s"""
params1 = (comID,)
res1 = run_sql(query1, params1)
if len(res1) == 0:
return 0
# modify and insert new nb_useful votes
nb_votes_yes = int(res1[0][1])
if value >= 1:
nb_votes_yes = int(res1[0][1]) + 1
nb_votes_total = int(res1[0][0]) + 1
query2 = """UPDATE "cmtRECORDCOMMENT" SET nb_votes_total=%s, nb_votes_yes=%s WHERE id=%s"""
params2 = (nb_votes_total, nb_votes_yes, comID)
res2 = run_sql(query2, params2)
return int(res2)
def query_retrieve_comments_or_remarks(
recID,
display_order='od',
display_since='0000-00-00 00:00:00',
ranking=0,
limit='all',
user_info=None):
"""
Private function
Retrieve tuple of comments or remarks from the database
:param recID: record id
:param display_order: hh = highest helpful score
lh = lowest helpful score
hs = highest star score
ls = lowest star score
od = oldest date
nd = newest date
:param display_since: datetime, e.g. 0000-00-00 00:00:00
:param ranking: boolean, enabled if reviews, disabled for comments
:param limit: number of comments/review to return
:return: tuple of comment where comment is
tuple (nickname, uid, date_creation, body, status, id) if ranking disabled or
tuple (nickname, uid, date_creation, body, status, nb_votes_yes, nb_votes_total, star_score, title, id)
Note: for the moment, if no nickname, will return email address up to ':'
"""
display_since = calculate_start_date(display_since)
order_dict = {
'hh': "cmt.nb_votes_yes/(cmt.nb_votes_total+1) DESC, cmt.date_creation DESC ",
'lh': "cmt.nb_votes_yes/(cmt.nb_votes_total+1) ASC, cmt.date_creation ASC ",
'ls': "cmt.star_score ASC, cmt.date_creation DESC ",
'hs': "cmt.star_score DESC, cmt.date_creation DESC ",
'nd': "cmt.reply_order_cached_data DESC ",
'od': "cmt.reply_order_cached_data ASC "}
# Ranking only done for comments and when allowed
if ranking and recID > 0:
try:
display_order = order_dict[display_order]
except:
display_order = order_dict['od']
else:
# in case of recID > 0 => external record => no ranking!
ranking = 0
try:
if display_order[-1] == 'd':
display_order = order_dict[display_order]
else:
display_order = order_dict['od']
except:
display_order = order_dict['od']
#display_order = order_dict['nd']
query = """SELECT user.nickname,
cmt.id_user,
""" + \
datetime_format('cmt.date_creation', False, True) + ', ' \
"""
cmt.body,
cmt.status,
cmt.nb_abuse_reports,
%(ranking)s cmt.id,
cmt.round_name,
cmt.restriction,
%(reply_to_column)s
FROM "cmtRECORDCOMMENT" cmt LEFT JOIN "user" ON
"user".id=cmt.id_user
WHERE cmt.id_bibrec=%%s
%(ranking_only)s
%(display_since)s
ORDER BY %(display_order)s
""" % {'ranking' : ranking and ' cmt.nb_votes_yes, cmt.nb_votes_total, cmt.star_score, cmt.title, ' or '',
'ranking_only': ranking and ' AND cmt.star_score>0 ' or ' AND cmt.star_score=0 ',
# 'id_bibrec' : recID > 0 and 'cmt.id_bibrec' or 'cmt.id_bibrec_or_bskEXTREC',
# 'table' : recID > 0 and 'cmtRECORDCOMMENT' or 'bskRECORDCOMMENT',
'display_since': display_since == '0000-00-00 00:00:00' and ' ' or 'AND cmt.date_creation>=\'%s\' ' % display_since,
'display_order': display_order,
'reply_to_column': recID > 0 and 'cmt."in_reply_to_id_cmtRECORDCOMMENT"' or 'cmt."in_reply_to_id_bskRECORDCOMMENT"'}
params = (recID,)
res = run_sql(query, params)
# return res
new_limit = limit
comments_list = []
for row in res:
if ranking:
# when dealing with reviews, row[12] holds restriction info:
restriction = row[12]
else:
# when dealing with comments, row[8] holds restriction info:
restriction = row[8]
if user_info and check_user_can_view_comment(
user_info, None, restriction)[0] != 0:
# User cannot view comment. Look further
continue
comments_list.append(row)
if limit.isdigit():
new_limit -= 1
if limit < 1:
break
if comments_list:
if limit.isdigit():
return comments_list[:limit]
else:
return comments_list
return ()
# def get_comment_children(comID):
# """
# Returns the list of children (i.e. direct descendants) ordered by time
# of addition.
# :param comID: the ID of the comment for which we want to retrieve children
# :type comID: int
# :return the list of children
# :rtype: list
# """
# res = run_sql("SELECT id FROM cmtRECORDCOMMENT WHERE in_reply_to_id_cmtRECORDCOMMENT=%s", (comID,))
# return [row[0] for row in res]
# def get_comment_descendants(comID, depth=None):
# """
# Returns the list of descendants of the given comment, orderd from
# oldest to newest ("top-down"), down to depth specified as parameter.
# :param comID: the ID of the comment for which we want to retrieve descendant
# :type comID: int
# :param depth: the max depth down to which we want to retrieve
# descendants. Specify None for no limit, 1 for direct
# children only, etc.
# :return the list of ancestors
# :rtype: list(tuple(comment ID, descendants comments IDs))
# """
# if depth == 0:
# return (comID, [])
# res = run_sql("SELECT id FROM cmtRECORDCOMMENT WHERE in_reply_to_id_cmtRECORDCOMMENT=%s", (comID,))
# if res:
# children_comID = [row[0] for row in res]
# children_descendants = []
# if depth:
# depth -= 1
# children_descendants = [get_comment_descendants(child_comID, depth) for child_comID in children_comID]
# return (comID, children_descendants)
# else:
# return (comID, [])
def get_comment_ancestors(comID, depth=None):
"""
Returns the list of ancestors of the given comment, ordered from
oldest to newest ("top-down": direct parent of comID is at last position),
up to given depth
:param comID: the ID of the comment for which we want to retrieve ancestors
:type comID: int
:param depth: the maximum of levels up from the given comment we
want to retrieve ancestors. None for no limit, 1 for
direct parent only, etc.
:type depth: int
:return the list of ancestors
:rtype: list
"""
if depth == 0:
return []
res = run_sql(
"""SELECT "in_reply_to_id_cmtRECORDCOMMENT" FROM "cmtRECORDCOMMENT" WHERE id=%s""",
(comID,
))
if res:
parent_comID = res[0][0]
if parent_comID == 0:
return []
parent_ancestors = []
if depth:
depth -= 1
parent_ancestors = get_comment_ancestors(parent_comID, depth)
parent_ancestors.append(parent_comID)
return parent_ancestors
else:
return []
def get_reply_order_cache_data(comid):
"""
Prepare a representation of the comment ID given as parameter so
that it is suitable for byte ordering in MySQL.
"""
return "%s%s%s%s" % (chr((comid >> 24) % 256), chr((comid >> 16) % 256),
chr((comid >> 8) % 256), chr(comid % 256))
def query_add_comment_or_remark(reviews=0, recID=0, uid=-1, msg="",
note="", score=0, priority=0,
client_ip_address='', editor_type='textarea',
req=None, reply_to=None, attached_files=None):
"""
Private function
Insert a comment/review or remarkinto the database
:param recID: record id
:param uid: user id
:param msg: comment body
:param note: comment title
:param score: review star score
:param priority: remark priority #!FIXME
:param editor_type: the kind of editor used to submit the comment: 'textarea', 'ckeditor'
:param req: request object. If provided, email notification are sent after we reply to user request.
:param reply_to: the id of the comment we are replying to with this inserted comment.
:return: integer >0 representing id if successful, integer 0 if not
"""
current_date = calculate_start_date('0d')
# change utf-8 message into general unicode
msg = msg.decode('utf-8')
note = note.decode('utf-8')
# change general unicode back to utf-8
msg = msg.encode('utf-8')
note = note.encode('utf-8')
(restriction, round_name) = get_record_status(recID)
if attached_files is None:
attached_files = {}
if reply_to and CFG_WEBCOMMENT_MAX_COMMENT_THREAD_DEPTH >= 0:
# Check that we have not reached max depth
comment_ancestors = get_comment_ancestors(reply_to)
if len(comment_ancestors) >= CFG_WEBCOMMENT_MAX_COMMENT_THREAD_DEPTH:
if CFG_WEBCOMMENT_MAX_COMMENT_THREAD_DEPTH == 0:
reply_to = None
else:
reply_to = comment_ancestors[
CFG_WEBCOMMENT_MAX_COMMENT_THREAD_DEPTH -
1]
# Inherit restriction and group/round of 'parent'
comment = query_get_comment(reply_to)
if comment:
(round_name, restriction) = comment[10:12]
if editor_type == 'ckeditor':
# Here we remove the line feeds introduced by CKEditor (they
# have no meaning for the user) and replace the HTML line
# breaks by linefeeds, so that we are close to an input that
# would be done without the CKEditor. That's much better if a