diff options
author | Code Raguet <ignacio.code@gmail.com> | 2013-07-17 14:48:39 (GMT) |
---|---|---|
committer | Code Raguet <ignacio.code@gmail.com> | 2013-07-17 16:23:09 (GMT) |
commit | 54d4ade34a8577a604aba77ca97b3b275e446a07 (patch) | |
tree | 2b05255c2cd04673b720b9fea240eedc809fa4c3 | |
parent | 4f13e859ac5565aac957c702a5f67614c9a89641 (diff) |
CSV with sorted columns (#4160)
-rw-r--r-- | webapp/polls/models.py | 46 | ||||
-rw-r--r-- | webapp/polls/tests/result_tests.py | 176 |
2 files changed, 207 insertions, 15 deletions
diff --git a/webapp/polls/models.py b/webapp/polls/models.py index 05346f8..c0c58e2 100644 --- a/webapp/polls/models.py +++ b/webapp/polls/models.py @@ -404,16 +404,22 @@ class Poll(Document, AbstracErrorObject): "ImageRadioButton" ] - structure = self.structure + structure = self.structure.to_python() header = [] - for group in structure.groups: - for field in group.fields: - if field.widget_type in ONLY_ANSWER: - header.append(field.name) - elif field.widget_type in MANY_ANSWERS: - for option in field.options: - col_name = option.text if option.text else option.id + groups = structure['groups'] + sorted_groups = sorted(map(lambda x: int(x), groups.keys())) + + for group_id in sorted_groups: + fields = groups[str(group_id)]['fields'] + sorted_fields = sorted(map(lambda x: int(x), fields.keys())) + for field_id in sorted_fields: + field = fields[str(field_id)] + if field['widget_type'] in ONLY_ANSWER: + header.append(field['name']) + elif field['widget_type'] in MANY_ANSWERS: + for option_id, option in field['options'].iteritems(): + col_name = option.get("text", option_id) header.append(col_name) return ";".join(header) @@ -1134,6 +1140,23 @@ class Structure(AbstractObject, ComponentStructure): pass +class NodePollResult(object): + + def __init__(self, id_, answers): + self.id = id_ + self.answers = answers + + def sorted(self): + answers = self.answers['answers'] + sorted_groups = sorted(map(lambda x: int(x), answers.keys())) + + for group_id in sorted_groups: + fields = answers[str(group_id)]['fields'] + sorted_fields = sorted(map(lambda x: int(x), fields.keys())) + for field_id in sorted_fields: + yield fields[str(field_id)] + + class PollResult(AbstractObject): def __init__(self, data=None): @@ -1307,3 +1330,10 @@ class PollResult(AbstractObject): results.append(record) return poll_csv_header + "\n" + ";\n".join(results) + + def sorted(self): + data = self._data['result'] + sorted_keys = sorted(map(lambda x: int(x), data.keys())) + + for result_id in sorted_keys: + yield NodePollResult(str(result_id), answers=data[str(result_id)]) diff --git a/webapp/polls/tests/result_tests.py b/webapp/polls/tests/result_tests.py index acd43e9..6ec4fdb 100644 --- a/webapp/polls/tests/result_tests.py +++ b/webapp/polls/tests/result_tests.py @@ -182,7 +182,7 @@ class PollResultTests(MongoTestCase): } } }, - "0": { + "10": { "name": "Questions that admit only one answer", "fields": { "1": { @@ -255,7 +255,7 @@ class PollResultTests(MongoTestCase): "pollster_id": "51b0e946421aa90df17182e9", "pollster_username": "encuestador 1", "result": { - "0": { + "10": { "answers": { "0": { "fields": { @@ -346,15 +346,177 @@ class PollResultTests(MongoTestCase): poll_result = PollResult(data=result_data) + # expected_header = unicode( + # "RUEE;DEPARTAMENTO;NUM_ESC;GRADO;GRUPO;" + + # "TIPO_GRUPO;Q: TextInput con ácento;Q: RadioButton con ácento;" + + # "Q: DropDownList;" + + # "Q: ImageRadioButton;option 1;option 2;" + + # "1370786178812;1370786178813;", 'utf-8') expected_header = unicode( "RUEE;DEPARTAMENTO;NUM_ESC;GRADO;GRUPO;" + - "TIPO_GRUPO;Q: TextInput con ácento;Q: RadioButton con ácento;" + - "Q: DropDownList;" + - "Q: ImageRadioButton;option 1;option 2;" + - "1370786178812;1370786178813;", 'utf-8') + "TIPO_GRUPO;option 1;option 2;1370786178812;1370786178813;" + + "Q: TextInput con ácento;Q: RadioButton con ácento;" + + "Q: DropDownList;Q: ImageRadioButton;", 'utf-8') self.assertEqual(expected_header, poll_result.get_csv_header()) csv = poll_result.to_csv() - record = "1101236;MONTEVIDEO;236;2;A;1;some text;1;1;1;1;2;1;;" + record = "1101236;MONTEVIDEO;236;2;A;1;1;2;1;;some text;1;1;1;" self.assertTrue(expected_header in csv) self.assertTrue(record in csv) + + +class PollResultShouldBeSortedByGrupoAndFields(MongoTestCase): + + def test_load_unsorted_and_fetch_should_be_sorted(self): + field_1_1_3 = { + "answer": { + "1370787034900": { + "text": "some text" + } + }, + "name": "Q: TextInput con ácento", + "widget_type": "TextInput" + } + field_1_1_1 = { + "answer": { + "1370786178802": { + "text": "option 1", + "weight": 1 + } + }, + "name": "Q: RadioButton con ácento", + "widget_type": "RadioButton" + } + field_1_1_0 = { + "answer": { + "1370786178804": { + "text": "option 1", + "weight": 1 + } + }, + "name": "Q: DropDownList", + "widget_type": "DropDownList" + } + field_1_1_2 = { + "answer": { + "1370786178806": { + "type": "img_input", + "weight": 1 + } + }, + "name": "Q: ImageRadioButton", + "widget_type": "ImageRadioButton" + } + field_1_0_0 = { + "answer": { + "1370786178809": { + "text": "option 1", + "weight": 1 + }, + "1370786178810": { + "text": "option 2", + "weight": 2 + } + }, + "name": "Q: CheckBox", + "widget_type": "MultipleCheckBox" + } + field_1_0_1 = { + "answer": { + "1370786178812": { + "type": "img_input", + "weight": 1 + } + }, + "name": "Q: ImageCheckBox", + "widget_type": "ImageCheckBox" + } + field_0_1_3 = field_1_1_3 + field_0_0_1 = field_1_0_1 + result_data = { + "poll_id": "poll_id", + "poll_name": "", + "poll_type": "general", + "pollster_id": "51b0e946421aa90df17182e9", + "pollster_username": "encuestador 1", + "result": { + "1": { + "answers": { + "1": { + "fields": { + "3": field_1_1_3, + "1": field_1_1_1, + "0": field_1_1_0, + "2": field_1_1_2, + }, + "name": "Questions that admit only one answer" + }, + "0": { + "fields": { + "0": field_1_0_0, + "1": field_1_0_1 + }, + "name": "Questions that admit many answers" + } + }, + "polled": { + "DEPARTAMENTO": "MONTEVIDEO", + "GRADO": "2", + "GRUPO": "A", + "ID": "0", + "NUM_ESC": "236", + "RUEE": "1101236", + "TIPO_GRUPO": "1" + } + }, + "0": { + "answers": { + "1": { + "fields": { + "3": field_0_1_3 + }, + "name": "Questions that admit only one answer" + }, + "0": { + "fields": { + "1": field_0_0_1 + }, + "name": "Questions that admit many answers" + } + }, + "polled": { + "DEPARTAMENTO": "MONTEVIDEO", + "GRADO": "2", + "GRUPO": "A", + "ID": "0", + "NUM_ESC": "236", + "RUEE": "1101236", + "TIPO_GRUPO": "1" + } + } + } + } + poll_result = PollResult(data=result_data) + results = poll_result.sorted() + + sorted_answers = [] + sorted_result = [] + for a_result in results: + sorted_result.append(a_result.id) + for answer in a_result.sorted(): + sorted_answers.append(answer) + + expected_result = ['0', '1'] + self.assertListEqual(expected_result, sorted_result) + + expected_answers = [ + field_0_0_1, + field_0_1_3, + field_1_0_0, + field_1_0_1, + field_1_1_0, + field_1_1_1, + field_1_1_2, + field_1_1_3, + ] + self.assertListEqual(expected_answers, sorted_answers) |