Skip to content

Commit

Permalink
A few more changes for v2.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Jul 26, 2018
1 parent 48ad2ac commit 546888b
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 21 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -7,7 +7,6 @@ cache:

python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"

Expand Down
20 changes: 20 additions & 0 deletions Pipfile
@@ -0,0 +1,20 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
Django = "*"
"psycopg2" = "*"
Sphinx = "*"
sphinx-autobuild = "*"
twine = "*"
"flake8" = "*"
coverage = "*"
python-coveralls = "*"
tox = "*"

[dev-packages]

[requires]
python_version = "3.6"
401 changes: 401 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/index.rst
Expand Up @@ -461,9 +461,9 @@ The ``to_csv`` manager method only requires one argument, the path to where the
================= =========================================================
Argument Description
================= =========================================================
``csv_path`` The path to a file to write out the CSV. Optional.
If you don't provide one, the comma-delimited data is
returned as a string.
``csv_path`` The path to a file to write out the CSV. Also accepts
file-like objects. Optional. If you don't provide one,
the comma-delimited data is returned as a string.

``fields`` Strings corresponding to the model fields to be exported.
All fields on the model are exported by default. Fields
Expand Down
2 changes: 1 addition & 1 deletion postgres_copy/__init__.py
Expand Up @@ -3,7 +3,7 @@
from .copy_from import CopyMapping
from .copy_to import SQLCopyToCompiler, CopyToQuery
from .managers import CopyManager, CopyQuerySet
__version__ = '2.3.2'
__version__ = '2.3.3'


__all__ = (
Expand Down
12 changes: 6 additions & 6 deletions postgres_copy/copy_to.py
Expand Up @@ -34,11 +34,11 @@ def setup_query(self):
)
self.select.append(selection)

def execute_sql(self, csv_path=None):
def execute_sql(self, csv_path_or_obj=None):
"""
Run the COPY TO query.
"""
logger.debug("Copying data to {}".format(csv_path))
logger.debug("Copying data to {}".format(csv_path_or_obj))

# adapt SELECT query parameters to SQL syntax
params = self.as_sql()[1]
Expand Down Expand Up @@ -67,12 +67,12 @@ def execute_sql(self, csv_path=None):
logger.debug(copy_to_sql)

# If a file-like object was provided, write it out there.
if hasattr(csv_path, 'write'):
c.cursor.copy_expert(copy_to_sql, csv_path)
if hasattr(csv_path_or_obj, 'write'):
c.cursor.copy_expert(copy_to_sql, csv_path_or_obj)
return
# If a file path was provided, write it out there.
elif csv_path:
with open(csv_path, 'wb') as stdout:
elif csv_path_or_obj:
with open(csv_path_or_obj, 'wb') as stdout:
c.cursor.copy_expert(copy_to_sql, stdout)
return
# If there's no csv_path, return the output as a string.
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Expand Up @@ -72,7 +72,7 @@ def run(self):

setup(
name='django-postgres-copy',
version='2.3.2',
version='2.3.3',
description="Quickly import and export delimited data with Django support for PostgreSQL's COPY command",
author='Ben Welsh',
author_email='ben.welsh@gmail.com',
Expand All @@ -85,7 +85,6 @@ def run(self):
'Development Status :: 5 - Production/Stable',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Framework :: Django',
Expand Down
20 changes: 12 additions & 8 deletions tests/tests.py
@@ -1,7 +1,7 @@
import io
import os
import csv
from datetime import date
import io
from .models import (
MockObject,
MockFKObject,
Expand Down Expand Up @@ -49,7 +49,10 @@ class PostgresCopyToTest(BaseTest):
def setUp(self):
super(PostgresCopyToTest, self).setUp()
self.export_path = os.path.join(os.path.dirname(__file__), 'export.csv')
self.export_file = io.StringIO()
self.export_files = [
io.StringIO(),
io.BytesIO()
]

def tearDown(self):
super(PostgresCopyToTest, self).tearDown()
Expand All @@ -74,12 +77,13 @@ def test_export(self):

def test_export_to_file(self):
self._load_objects(self.name_path)
MockObject.objects.to_csv(self.export_file)
reader = csv.DictReader(self.export_file)
self.assertTrue(
['BEN', 'JOE', 'JANE'],
[i['name'] for i in reader]
)
for f in self.export_files:
MockObject.objects.to_csv(f)
reader = csv.DictReader(f)
self.assertTrue(
['BEN', 'JOE', 'JANE'],
[i['name'] for i in reader]
)

def test_export_to_str(self):
self._load_objects(self.name_path)
Expand Down

0 comments on commit 546888b

Please sign in to comment.