Commit 1a6a5cd9 by Xavier Antoviaque

Simplify CSV export processing by using `itertools.groupby()`

parent 6fc5aa7c
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
import logging import logging
from itertools import groupby
from webob import Response from webob import Response
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fragment import Fragment from xblock.fragment import Fragment
...@@ -75,20 +76,22 @@ class MentoringDataExportBlock(XBlock): ...@@ -75,20 +76,22 @@ class MentoringDataExportBlock(XBlock):
# Header line # Header line
yield list2csv([u'student_id'] + list(answers_names)) yield list2csv([u'student_id'] + list(answers_names))
row = [] if answers_names:
cur_student_id = None for k, student_answers in groupby(answers, lambda x: x.student_id):
cur_col = None row = []
for answer in answers: next_answer_idx = 0
if answer.student_id != cur_student_id: for answer in student_answers:
if not row:
row = [answer.student_id]
while answer.name != answers_names[next_answer_idx]:
# Still add answer row to CSV when they don't exist in DB
row.append('')
next_answer_idx += 1
row.append(answer.student_input)
next_answer_idx += 1
if row: if row:
yield list2csv(row) yield list2csv(row)
row = [answer.student_id]
cur_student_id = answer.student_id
cur_col = 0
while answer.name != answers_names[cur_col]:
row.append('')
cur_col += 1
row.append(answer.student_input)
cur_col += 1
if row:
yield list2csv(row)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment