def test_writeheader(self): io = StringIO() writer = UnicodeDictWriter(io, self.headers) writer.writeheader() reader = csv.reader(StringIO(io.getvalue())) row = reader.next() self.assertEqual( [h.encode('utf-8') for h in self.headers], row)
def export_conversation_messages_unsorted(account_key, conversation_key): """ Export the messages from a conversation as they come from the message store. Completely unsorted. :param str account_key: The account holder's account account_key :param str conversation_key: The key of the conversation we want to export the messages for. """ user_api = VumiUserApi.from_config_sync(account_key, settings.VUMI_API_CONFIG) user_profile = UserProfile.objects.get(user_account=account_key) conversation = user_api.get_wrapped_conversation(conversation_key) io = StringIO() writer = UnicodeDictWriter(io, conversation_export_field_names) writer.writeheader() for messages in load_messages_in_chunks(conversation, 'inbound'): for message in messages: writer.writerow(row_for_inbound_message(message)) for messages in load_messages_in_chunks(conversation, 'outbound'): for message in messages: mdb = user_api.api.mdb writer.writerow(row_for_outbound_message(message, mdb)) email_export(user_profile, conversation, io)
def export_conversation_messages_unsorted(account_key, conversation_key): """ Export the messages from a conversation as they come from the message store. Completely unsorted. :param str account_key: The account holder's account account_key :param str conversation_key: The key of the conversation we want to export the messages for. """ user_api = VumiUserApi.from_config_sync( account_key, settings.VUMI_API_CONFIG) user_profile = UserProfile.objects.get(user_account=account_key) conversation = user_api.get_wrapped_conversation(conversation_key) io = StringIO() writer = UnicodeDictWriter(io, conversation_export_field_names) writer.writeheader() for messages in load_messages_in_chunks(conversation, 'inbound'): for message in messages: writer.writerow(row_for_inbound_message(message)) for messages in load_messages_in_chunks(conversation, 'outbound'): for message in messages: mdb = user_api.api.mdb writer.writerow(row_for_outbound_message(message, mdb)) email_export(user_profile, conversation, io)
def test_writerow(self): io = StringIO() writer = UnicodeDictWriter(io, self.headers) writer.writeheader() writer.writerow({ self.col1: u'føø', self.col2: u'bär', }) reader = csv.reader(StringIO(io.getvalue())) reader.next() # header row = reader.next() self.assertEqual( [u'føø'.encode('utf-8'), u'bär'.encode('utf-8')], row)
def test_writerows(self): io = StringIO() writer = UnicodeDictWriter(io, self.headers) writer.writeheader() for i in range(2): writer.writerows([{ self.col1: u'føø', self.col2: u'bär', }]) reader = csv.reader(StringIO(io.getvalue())) reader.next() # header rows = [row for row in reader] self.assertEqual( [ [u'føø'.encode('utf-8'), u'bär'.encode('utf-8')], [u'føø'.encode('utf-8'), u'bär'.encode('utf-8')], ], rows)