Skip to content

Commit

Permalink
issue #221
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtepkeev committed Mar 27, 2019
1 parent 0299808 commit 137ea84
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
---------

2.2.2 (2019-04-XX)
++++++++++++++++++

**Improvements**:

- ResourceSet's ``export()`` method now supports ``columns`` keyword argument which can be either an iterable
of column names or an "all" string which tells Python-Redmine to export all available columns

2.2.1 (2019-02-28)
++++++++++++++++++

Expand Down
4 changes: 2 additions & 2 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ formats of each resource in its documentation.
.. code-block:: python
>>> issues = redmine.issue.filter(project_id='vacation', status_id='*')
>>> issues.export('pdf', savepath='/home/jsmith')
'/home/jsmith/issues.pdf'
>>> issues.export('csv', savepath='/home/jsmith', columns='all')
'/home/jsmith/issues.csv'
Methods
+++++++
Expand Down
6 changes: 4 additions & 2 deletions docs/resources/issue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Export
>>> issue.export('pdf', savepath='/home/jsmith')
'/home/jsmith/123.pdf'
.. py:method:: export(fmt, savepath=None, filename=None)
.. py:method:: export(fmt, savepath=None, filename=None, columns=None)
:module: redminelib.resultsets.ResourceSet
:noindex:

Expand All @@ -469,12 +469,14 @@ Export
:param string fmt: (required). Format to use for export.
:param string savepath: (optional). Path where to save the file.
:param string filename: (optional). Name that will be used for the file.
:param columns: (optional). Iterable of column names or "all" for all columns.
:type columns: iterable or string
:return: String or Object

.. code-block:: python
>>> issues = redmine.issue.all()
>>> issues.export('csv', savepath='/home/jsmith', filename='issues.csv')
>>> issues.export('csv', savepath='/home/jsmith', filename='issues.csv', columns='all')
'/home/jsmith/issues.csv'
Journals
Expand Down
6 changes: 4 additions & 2 deletions docs/resources/time_entry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Export

.. versionadded:: 2.0.0

.. py:method:: export(fmt, savepath=None, filename=None)
.. py:method:: export(fmt, savepath=None, filename=None, columns=None)
:module: redminelib.resultsets.ResourceSet
:noindex:

Expand All @@ -269,10 +269,12 @@ Export
:param string fmt: (required). Format to use for export.
:param string savepath: (optional). Path where to save the file.
:param string filename: (optional). Name that will be used for the file.
:param columns: (optional). Iterable of column names or "all" for all columns.
:type columns: iterable or string
:return: String or Object

.. code-block:: python
>>> entries = redmine.time_entry.all()
>>> entries.export('csv', savepath='/home/jsmith', filename='entries.csv')
>>> entries.export('csv', savepath='/home/jsmith', filename='entries.csv', columns='all')
'/home/jsmith/entries.csv'
14 changes: 12 additions & 2 deletions redminelib/resultsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ def total_count(self):

return self._total_count

def export(self, fmt, savepath=None, filename=None):
def export(self, fmt, savepath=None, filename=None, columns=None):
"""
Exports all resources from resource set to requested format if Resource supports that.
:param string fmt: (required). Format to use for export, e.g. atom, csv, txt, pdf, html etc.
:param string savepath: (optional). Path where to save the file.
:param string filename: (optional). Name that will be used for the file.
:param columns: (optional). Iterable of column names or "all" for all columns.
:type columns: iterable or string
"""
if self.manager.resource_class.query_all_export is None:
raise exceptions.ExportNotSupported
Expand All @@ -58,8 +60,16 @@ def export(self, fmt, savepath=None, filename=None):
url = self.manager.redmine.url + formatter.format(
self.manager.resource_class.query_all_export, format=fmt, **self.manager.params)

params = formatter.unused_kwargs

if columns is not None:
if columns == 'all':
columns = 'all_inline'

params.update({'c[]': columns, 'encoding': 'UTF-8'})

try:
return self.manager.redmine.download(url, savepath, filename, params=formatter.unused_kwargs)
return self.manager.redmine.download(url, savepath, filename, params=params)
except exceptions.UnknownError as e:
if e.status_code == 406:
raise exceptions.ExportFormatNotSupportedError
Expand Down
10 changes: 10 additions & 0 deletions tests/test_resultsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ def test_export(self):
self.response.iter_content = lambda chunk_size: (str(num) for num in range(0, 5))
self.assertEqual(self.redmine.issue.all().export('txt', '/foo/bar'), '/foo/bar/issues.txt')

@mock.patch('redminelib.open', mock.mock_open(), create=True)
def test_export_with_all_columns(self):
self.response.iter_content = lambda chunk_size: (str(num) for num in range(0, 5))
self.assertEqual(self.redmine.issue.all().export('txt', '/foo/bar', columns='all'), '/foo/bar/issues.txt')

@mock.patch('redminelib.open', mock.mock_open(), create=True)
def test_export_with_custom_columns(self):
self.response.iter_content = lambda chunk_size: (str(num) for num in range(0, 5))
self.assertEqual(self.redmine.issue.all().export('txt', '/foo/bar', columns=['status']), '/foo/bar/issues.txt')

def test_export_not_supported_exception(self):
self.assertRaises(exceptions.ExportNotSupported, lambda: self.redmine.custom_field.all().export('pdf'))

Expand Down

0 comments on commit 137ea84

Please sign in to comment.