Skip to content

Commit

Permalink
Adding missing test of my last commit and moving custom csv UnicodeWr…
Browse files Browse the repository at this point in the history
…iter to compat.py module
  • Loading branch information
Nicolás Mendoza committed Jul 1, 2017
1 parent db0f971 commit a027bc8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
43 changes: 43 additions & 0 deletions django_extensions/compat.py
@@ -1,6 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from io import BytesIO

import csv
import six
import codecs
import importlib
import django

Expand Down Expand Up @@ -43,3 +48,41 @@ def get_template_setting(template_key, default=None):
value = getattr(settings, pre18_template_key, default)
return value
return default


class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
We are using this custom UnicodeWriter for python versions 2.x
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.queue = BytesIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)

def writerows(self, rows):
for row in rows:
self.writerow(row)


from csv import writer # noqa

# Default csv.writer for PY3 versions
csv_writer = writer
if six.PY2:
# unicode CSVWriter for PY2
csv_writer = UnicodeWriter # noqa
7 changes: 2 additions & 5 deletions django_extensions/management/commands/export_emails.py
@@ -1,17 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
import sys
import six
from csv import writer # noqa

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand, CommandError

from django_extensions.management.utils import signalcommand, UnicodeWriter
from django_extensions.compat import csv_writer as writer
from django_extensions.management.utils import signalcommand

if six.PY2:
writer = UnicodeWriter # noqa

FORMATS = [
'address',
Expand Down
32 changes: 0 additions & 32 deletions django_extensions/management/utils.py
Expand Up @@ -2,10 +2,6 @@
import logging
import os
import sys
import csv
import codecs

from io import BytesIO

from django_extensions.management.signals import post_command, pre_command

Expand Down Expand Up @@ -72,31 +68,3 @@ def has_ipdb():
return True
except ImportError:
return False


class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.queue = BytesIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)

def writerows(self, rows):
for row in rows:
self.writerow(row)
16 changes: 13 additions & 3 deletions tests/management/commands/test_export_emails.py
Expand Up @@ -36,9 +36,7 @@ def django_db_setup(django_db_setup, django_db_blocker): # noqa
def test_do_export_emails_stdout_start(capsys):
"""Testing export_emails command without args.stdout starts."""
export_emails = Command()
export_emails.run_from_argv(
['manage.py', 'export_emails']
)
export_emails.run_from_argv(['manage.py', 'export_emails'])

out, err = capsys.readouterr()
assert out.startswith('"Mijaíl Bulgakóv')
Expand Down Expand Up @@ -84,6 +82,18 @@ def test_do_export_emails_format_google(capsys):
assert out.startswith('Name,Email')


@pytest.mark.django_db()
def test_do_export_emails_format_linkedin(capsys):
"""Testing python manage.py export_emails -f linkedin"""
export_emails = Command()
export_emails.run_from_argv(['manage.py', 'export_emails', '--format=linkedin'])

out, err = capsys.readouterr()

assert out.startswith('First Name,')
assert 'Gabriel Garcia,Marquéz' in out


@pytest.mark.django_db()
def test_do_export_emails_format_outlook(capsys):
"""Testing python manage.py export_emails -f outlook"""
Expand Down

0 comments on commit a027bc8

Please sign in to comment.