Skip to content

Commit 153de2b

Browse files
committed
bring isave_to_database and isave_book_to_database to sqlalchemy based extensions: Flask-Excel and pyramid-excel
1 parent 3dbc88f commit 153de2b

File tree

7 files changed

+85
-7
lines changed

7 files changed

+85
-7
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
Change log
22
================================================================================
33

4-
0.1.2 - deferred
4+
0.1.2 - unreleased
55
--------------------------------------------------------------------------------
66

77
Added
88
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
99

1010
#. To bring isave_as and isave_book_as to web clients
1111

12+
Updated
13+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14+
15+
#. replaced monkey-patching initialization step. For all extension developer,
16+
please call init_webio(your_response_function)
17+
1218
0.1.1 - 07.07.2017
1319
--------------------------------------------------------------------------------
1420

pyexcel_webio.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ version: "0.1.2"
55
current_version: "0.1.2"
66
release: "0.1.1"
77
dependencies:
8-
- pyexcel>=0.4.0
8+
- pyexcel>=0.6.0
99
description:
1010
A generic request and response interface for pyexcel web extensions.

pyexcel_webio/__init__.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ def save_to_database(self, session=None, table=None,
169169
params['dest_auto_commit'] = auto_commit
170170
pe.save_as(**params)
171171

172+
def isave_to_database(self, session=None, table=None,
173+
initializer=None, mapdict=None,
174+
auto_commit=True,
175+
**keywords):
176+
"""
177+
Save data from a sheet to database
178+
179+
:param session: a SQLAlchemy session
180+
:param table: a database table
181+
:param initializer: a custom table initialization function if
182+
you have one
183+
:param mapdict: the explicit table column names if your excel
184+
data do not have the exact column names
185+
:param keywords: additional keywords to
186+
:meth:`pyexcel.Sheet.save_to_database`
187+
"""
188+
params = self.get_params(**keywords)
189+
params['dest_session'] = session
190+
params['dest_table'] = table
191+
params['dest_initializer'] = initializer
192+
params['dest_mapdict'] = mapdict
193+
params['dest_auto_commit'] = auto_commit
194+
pe.isave_as(**params)
195+
172196
def get_book(self, **keywords):
173197
"""Get a instance of :class:`Book` from the file
174198
@@ -212,6 +236,31 @@ def save_book_to_database(self, session=None, tables=None,
212236
params['dest_auto_commit'] = auto_commit
213237
pe.save_book_as(**params)
214238

239+
def isave_book_to_database(self, session=None, tables=None,
240+
initializers=None, mapdicts=None,
241+
auto_commit=True, **keywords):
242+
"""
243+
Save a book into database
244+
245+
:param session: a SQLAlchemy session
246+
:param tables: a list of database tables
247+
:param initializers: a list of model
248+
initialization functions.
249+
:param mapdicts: a list of explicit table column names
250+
if your excel data sheets do not have
251+
the exact column names
252+
:param keywords: additional keywords to
253+
:meth:`pyexcel.Book.save_to_database`
254+
255+
"""
256+
params = self.get_params(**keywords)
257+
params['dest_session'] = session
258+
params['dest_tables'] = tables
259+
params['dest_initializers'] = initializers
260+
params['dest_mapdicts'] = mapdicts
261+
params['dest_auto_commit'] = auto_commit
262+
pe.isave_book_as(**params)
263+
215264
def free_resources(self):
216265
"""
217266
After you have used iget_array and iget_records, it's

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pyexcel>=0.4.0
1+
pyexcel>=0.6.0

rnd_requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
https://github.com/chfw/lml/archive/master.zip
21
https://github.com/pyexcel/pyexcel/archive/master.zip
3-
https://github.com/pyexcel/pyexcel-io/archive/v0.4.x.zip
4-
https://github.com/pyexcel/pyexcel-xls/archive/v0.4.x.zip
52

63

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
]
5050

5151
INSTALL_REQUIRES = [
52-
'pyexcel>=0.4.0',
52+
'pyexcel>=0.6.0',
5353
]
5454

5555

tests/test_webio.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ def test_save_to_database(self):
141141
assert array == self.data
142142
self.session.close()
143143

144+
def test_isave_to_database(self):
145+
Base.metadata.drop_all(engine)
146+
Base.metadata.create_all(engine)
147+
self.session = Session()
148+
myinput = TestInput()
149+
myinput.isave_to_database(file_name=self.testfile,
150+
session=self.session,
151+
table=Signature)
152+
array = pe.get_array(session=self.session, table=Signature)
153+
assert array == self.data
154+
self.session.close()
155+
144156
def tearDown(self):
145157
os.unlink(self.testfile)
146158

@@ -227,6 +239,20 @@ def test_save_to_database(self):
227239
assert array == self.data1
228240
self.session.close()
229241

242+
def test_isave_to_database(self):
243+
Base.metadata.drop_all(engine)
244+
Base.metadata.create_all(engine)
245+
self.session = Session()
246+
myinput = TestInput()
247+
myinput.isave_book_to_database(file_name=self.testfile,
248+
session=self.session,
249+
tables=[Signature, Signature2])
250+
array = pe.get_array(session=self.session, table=Signature)
251+
self.assertEqual(array, self.data)
252+
array = pe.get_array(session=self.session, table=Signature2)
253+
assert array == self.data1
254+
self.session.close()
255+
230256
def tearDown(self):
231257
os.unlink(self.testfile)
232258

0 commit comments

Comments
 (0)