5
5
from django .conf import settings as django_settings
6
6
from django .contrib import messages
7
7
from django .contrib .auth .decorators import login_required
8
- from django .db .models import Count
8
+ from django .db .models import Count , Sum , Value
9
+ from django .db .models .functions import Coalesce
9
10
from django .http import Http404 , HttpResponse , HttpResponseBadRequest
10
11
from django .shortcuts import get_object_or_404 , redirect , render
11
12
from django .template .context_processors import csrf
12
13
from django .urls import reverse as r
13
- from django .utils .html import escape
14
+ from django .utils .html import escape , format_html
14
15
from django .utils .translation import gettext as _
15
16
from django .views .decorators .http import require_POST
16
17
@@ -227,7 +228,12 @@ def study_selection(request, username, review_name):
227
228
228
229
229
230
def build_quality_assessment_table (request , review , order ):
230
- selected_studies = review .get_accepted_articles ().order_by (order )
231
+ selected_studies = (
232
+ review .get_accepted_articles ()
233
+ .prefetch_related ("qualityassessment_set" )
234
+ .annotate (score = Coalesce (Sum ("qualityassessment__answer__weight" ), Value (0.0 )))
235
+ .order_by (order )
236
+ )
231
237
quality_questions = review .get_quality_assessment_questions ()
232
238
quality_answers = review .get_quality_assessment_answers ()
233
239
@@ -242,43 +248,35 @@ def build_quality_assessment_table(request, review, order):
242
248
243
249
<table class="table" id="tbl-quality" article-id="{2}" csrf-token="{3}">
244
250
<tbody>""" .format (
245
- escape (study .title ), study .get_score () , study .id , str (csrf (request )["csrf_token" ]), escape (study .year )
251
+ escape (study .title ), study .score , study .id , str (csrf (request )["csrf_token" ]), escape (study .year )
246
252
)
247
253
248
- quality_assessment = study .get_quality_assesment ()
249
-
250
254
for question in quality_questions :
251
- str_table += (
252
- '''<tr question-id="'''
253
- + str (question .id )
254
- + """">
255
- <td>"""
256
- + escape (question .description )
257
- + """</td>"""
255
+ str_table += format_html (
256
+ '<tr question-id="{question_id}"><td>{question_description}</td>' ,
257
+ question_id = question .pk ,
258
+ question_description = question .description ,
258
259
)
259
260
260
- try :
261
- question_answer = quality_assessment .filter (question__id = question .id ).get ()
262
- except Exception :
263
- question_answer = None
261
+ question_answer_id = None
262
+ for qa in study .qualityassessment_set .all ():
263
+ if qa .question_id == question .pk :
264
+ question_answer_id = qa .answer_id
265
+ break
264
266
265
267
for answer in quality_answers :
266
268
selected_answer = ""
267
- if question_answer is not None :
268
- if answer .id == question_answer .answer .id :
269
- selected_answer = " selected-answer"
270
- str_table += (
271
- """<td class="answer"""
272
- + selected_answer
273
- + '''" answer-id="'''
274
- + str (answer .id )
275
- + """">"""
276
- + escape (answer .description )
277
- + """</td>"""
269
+ if answer .id == question_answer_id :
270
+ selected_answer = " selected-answer"
271
+ str_table += format_html (
272
+ '<td class="answer {selected}" answer-id="{answer_id}">{answer_description}</td>' ,
273
+ selected = selected_answer ,
274
+ answer_id = answer .pk ,
275
+ answer_description = answer .description ,
278
276
)
279
- str_table += """ </tr>"" "
277
+ str_table += "</tr>"
280
278
281
- str_table += """ </tbody></table></div>"" "
279
+ str_table += "</tbody></table></div>"
282
280
return str_table
283
281
else :
284
282
return ""
@@ -367,10 +365,11 @@ def quality_assessment(request, username, review_name):
367
365
def build_data_extraction_field_row (article , field ):
368
366
str_field = ""
369
367
370
- try :
371
- extraction = DataExtraction .objects .get (article = article , field = field )
372
- except Exception :
373
- extraction = None
368
+ extraction = None
369
+ for data_extraction in article .dataextraction_set .all ():
370
+ if data_extraction .field_id == field .pk :
371
+ extraction = data_extraction
372
+ break
374
373
375
374
if field .field_type == DataExtractionField .BOOLEAN_FIELD :
376
375
true = ""
@@ -403,7 +402,7 @@ def build_data_extraction_field_row(article, field):
403
402
<option value="">Select...</option>""" .format (
404
403
article .id , field .id
405
404
)
406
- for value in field .get_select_values ():
405
+ for value in field .dataextractionlookup_set . all ():
407
406
if extraction is not None and extraction .get_value () is not None and extraction .get_value ().id == value .id :
408
407
selected = " selected"
409
408
else :
@@ -412,7 +411,7 @@ def build_data_extraction_field_row(article, field):
412
411
str_field += "</select>"
413
412
414
413
elif field .field_type == DataExtractionField .SELECT_MANY_FIELD :
415
- for value in field .get_select_values ():
414
+ for value in field .dataextractionlookup_set . all ():
416
415
if extraction is not None and value in extraction .get_value ():
417
416
checked = " checked"
418
417
else :
@@ -442,10 +441,13 @@ def build_data_extraction_field_row(article, field):
442
441
443
442
444
443
def build_data_extraction_table (review , is_finished ):
445
- selected_studies = review .get_final_selection_articles ()
444
+ selected_studies = review .get_final_selection_articles ().prefetch_related (
445
+ "dataextraction_set__field" ,
446
+ "dataextraction_set__select_values" ,
447
+ )
446
448
if is_finished is not None :
447
449
selected_studies = selected_studies .filter (finished_data_extraction = is_finished )
448
- data_extraction_fields = review .get_data_extraction_fields ()
450
+ data_extraction_fields = review .get_data_extraction_fields (). prefetch_related ( "dataextractionlookup_set" )
449
451
has_quality_assessment = review .has_quality_assessment_checklist ()
450
452
if selected_studies and data_extraction_fields :
451
453
str_table = '<div class="panel-group">'
@@ -455,7 +457,7 @@ def build_data_extraction_table(review, is_finished):
455
457
<div class="panel-heading">
456
458
<h3 class="panel-title">{0}
457
459
<span class="badge">{1}</span>""" .format (
458
- escape (study .title ), study .get_score ()
460
+ escape (study .title ), study .score
459
461
)
460
462
461
463
if study .finished_data_extraction :
@@ -552,6 +554,7 @@ def data_extraction(request, username, review_name):
552
554
try :
553
555
data_extraction_table = build_data_extraction_table (review , is_finished )
554
556
except Exception :
557
+ logger .exception ("An error occurred while trying to build data extraction table." )
555
558
data_extraction_table = "<h3>Something went wrong while rendering the data extraction form.</h3>"
556
559
557
560
return render (
0 commit comments