1111from django .shortcuts import get_object_or_404 , redirect , render
1212from django .template .context_processors import csrf
1313from django .urls import reverse as r
14- from django .utils .html import escape , format_html
14+ from django .utils .html import escape
1515from django .utils .translation import gettext as _
1616from django .views .decorators .http import require_POST
1717
@@ -237,6 +237,14 @@ def build_quality_assessment_table(request, review, order):
237237 quality_questions = review .get_quality_assessment_questions ()
238238 quality_answers = review .get_quality_assessment_answers ()
239239
240+ csrf_token = csrf (request )["csrf_token" ]
241+
242+ quality_index = {}
243+ for study in selected_studies :
244+ quality_index [study .pk ] = {}
245+ for qa in study .qualityassessment_set .all ():
246+ quality_index [study .pk ][qa .question_id ] = qa .answer_id
247+
240248 if quality_questions and quality_answers :
241249 str_table = ""
242250 for study in selected_studies :
@@ -248,31 +256,27 @@ def build_quality_assessment_table(request, review, order):
248256
249257 <table class="table" id="tbl-quality" article-id="{2}" csrf-token="{3}">
250258 <tbody>""" .format (
251- escape (study .title ), study .score , study .id , str ( csrf ( request )[ " csrf_token" ]) , escape (study .year )
259+ escape (study .title ), study .score , study .id , csrf_token , escape (study .year )
252260 )
253261
254262 for question in quality_questions :
255- str_table += format_html (
256- '<tr question-id="{question_id}"><td>{question_description}</td>' ,
263+ str_table += '<tr question-id="{question_id}"><td>{question_description}</td>' .format (
257264 question_id = question .pk ,
258- question_description = question .description ,
265+ question_description = escape ( question .description ) ,
259266 )
260267
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
268+ question_answer_id = quality_index [study .pk ].get (question .pk )
266269
267270 for answer in quality_answers :
268271 selected_answer = ""
269272 if answer .id == question_answer_id :
270273 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 ,
274+ str_table += (
275+ '<td class="answer {selected}" answer-id="{answer_id}">{answer_description}</td>' .format (
276+ selected = selected_answer ,
277+ answer_id = answer .pk ,
278+ answer_description = escape (answer .description ),
279+ )
276280 )
277281 str_table += "</tr>"
278282
0 commit comments