Ejemplo n.º 1
0
 def get(self, *args, **kwargs):
     poll_id = kwargs['poll_id']
     poll = Poll.objects.get(id=poll_id)
     export_service = ExportPollResponsesService(poll)
     formatted_responses = export_service.get_formatted_responses()
     response = HttpResponse(content_type='text/csv')
     poll_name = poll.name
     response['Content-Disposition'] = 'attachment; filename="%s_responses.csv"' % poll_name.replace(" ", "_")
     response.write("\r\n".join(formatted_responses))
     return response
    def test_exports_poll_responses(self):
        response = PollResponse(**self.poll_response_attr).save()
        poll_response_attr2 = self.poll_response_attr.copy()
        poll_response_attr2['text'] = "NECOCPoll %s whatever2"%self.poll['keyword']
        response2 = PollResponse(**poll_response_attr2).save()

        expected_data = [self.delimiter_line, self.headings, self.get_poll_response_csv_row(response), self.get_poll_response_csv_row(response2)]

        export_poll_service = ExportPollResponsesService(self.poll)
        actual_data = export_poll_service.get_formatted_responses()

        self.assertEqual(len(expected_data), len(actual_data))
        self.assertIn(expected_data[0], actual_data)
        self.assertIn(expected_data[1], actual_data)
        self.assertIn(expected_data[2], actual_data)