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))
if answers_names:
for k, student_answers in groupby(answers, lambda x: x.student_id):
row = [] row = []
cur_student_id = None next_answer_idx = 0
cur_col = None for answer in student_answers:
for answer in answers: if not row:
if answer.student_id != cur_student_id:
if row:
yield list2csv(row)
row = [answer.student_id] row = [answer.student_id]
cur_student_id = answer.student_id
cur_col = 0 while answer.name != answers_names[next_answer_idx]:
while answer.name != answers_names[cur_col]: # Still add answer row to CSV when they don't exist in DB
row.append('') row.append('')
cur_col += 1 next_answer_idx += 1
row.append(answer.student_input) row.append(answer.student_input)
cur_col += 1 next_answer_idx += 1
if row: if row:
yield list2csv(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