diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 37abb53..0a57e54 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,8 @@ Change log #. pyexcel-io plugin interface has been rewritten. PyInstaller user will be impacted. please read 'Packaging with Pyinstaller' in the documentation. +#. new query set reader plugin. pyexcel<=0.6.4 has used intrusive way of getting + query set source done. it is against the plugin interface. 0.5.20 - 17.7.2019 -------------------------------------------------------------------------------- diff --git a/changelog.yml b/changelog.yml index 58ce90b..42f52b2 100644 --- a/changelog.yml +++ b/changelog.yml @@ -12,6 +12,8 @@ releases: details: - pyexcel-io plugin interface has been rewritten. PyInstaller user will be impacted. please read 'Packaging with Pyinstaller' in the documentation. + - new query set reader plugin. pyexcel<=0.6.4 has used intrusive way of getting query set + source done. it is against the plugin interface. version: 0.6.0 date: tbd - changes: diff --git a/pyexcel_io/constants.py b/pyexcel_io/constants.py index f969f40..533a196 100644 --- a/pyexcel_io/constants.py +++ b/pyexcel_io/constants.py @@ -49,6 +49,7 @@ FILE_FORMAT_XLSM = "xlsm" DB_SQL = "sql" DB_DJANGO = "django" +DB_QUERYSET = "queryset" KEYWORD_TSV_DIALECT = "excel-tab" KEYWORD_LINE_TERMINATOR = "lineterminator" diff --git a/pyexcel_io/database/__init__.py b/pyexcel_io/database/__init__.py index e5763f7..f28f0d5 100644 --- a/pyexcel_io/database/__init__.py +++ b/pyexcel_io/database/__init__.py @@ -8,9 +8,13 @@ :license: New BSD License, see LICENSE for more details """ from pyexcel_io.plugins import NewIOPluginInfoChain -from pyexcel_io.constants import DB_SQL, DB_DJANGO +from pyexcel_io.constants import DB_SQL, DB_DJANGO, DB_QUERYSET NewIOPluginInfoChain(__name__).add_a_reader( + relative_plugin_class_path="exporters.queryset.QueryReader", + locations=["file", "memory", "content"], + file_types=[DB_QUERYSET], +).add_a_reader( relative_plugin_class_path="exporters.django.DjangoBookReader", locations=["file", "memory", "content"], file_types=[DB_DJANGO], diff --git a/pyexcel_io/database/exporters/queryset.py b/pyexcel_io/database/exporters/queryset.py new file mode 100644 index 0000000..fd8ccdb --- /dev/null +++ b/pyexcel_io/database/exporters/queryset.py @@ -0,0 +1,20 @@ +from pyexcel_io.database.querysets import QuerysetsReader +from pyexcel_io.plugin_api.abstract_reader import IReader + + +class QueryReader(IReader): + def __init__(self, query_sets, _, column_names=None, **keywords): + self.query_sets = query_sets + self.column_names = column_names + self.keywords = keywords + self.content_array = [ + QuerysetsReader( + self.query_sets, self.column_names, **self.keywords + ) + ] + + def read_sheet(self, index): + return self.content_array[index] + + def close(self): + pass diff --git a/pyexcel_io/database/querysets.py b/pyexcel_io/database/querysets.py index 012427b..b1555f1 100644 --- a/pyexcel_io/database/querysets.py +++ b/pyexcel_io/database/querysets.py @@ -10,6 +10,7 @@ import datetime from itertools import chain +from pyexcel_io.constants import DEFAULT_SHEET_NAME from pyexcel_io.plugin_api.abstract_sheet import Sheet @@ -17,6 +18,7 @@ class QuerysetsReader(Sheet): """ turn querysets into an array """ def __init__(self, query_sets, column_names): + self.name = DEFAULT_SHEET_NAME self.__column_names = column_names self.__query_sets = query_sets diff --git a/tests/test_sql_book.py b/tests/test_sql_book.py index d37922b..334a781 100644 --- a/tests/test_sql_book.py +++ b/tests/test_sql_book.py @@ -24,6 +24,7 @@ ) from sqlalchemy.ext.declarative import declarative_base from pyexcel_io.database.querysets import QuerysetsReader +from pyexcel_io.database.exporters.queryset import QueryReader from pyexcel_io.database.exporters.sqlalchemy import ( SQLBookReader, SQLTableReader, @@ -226,6 +227,14 @@ def test_one_table(self): reader = QuerysetsReader(query_sets, self.data[0]) results = reader.to_array() assert list(results) == self.results + + query_sets = mysession.query(Pyexcel).all() + query_reader = QueryReader(query_sets, None, column_names=self.data[0]) + result = query_reader.read_all() + for key in result: + result[key] = list(result[key]) + eq_(result, {"pyexcel_sheet1": self.results}) + query_reader.close() mysession.close() def test_update_existing_row(self):