Skip to content

Commit 4b8a71f

Browse files
committed
✨ expose bulk_save parameter. related to pyexcel/pyexcel-io#46
1 parent 684d787 commit 4b8a71f

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

django_excel/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def save_to_database(self, model=None, initializer=None, mapdict=None,
4949
pe.save_as(**params)
5050

5151
def save_book_to_database(self, models=None, initializers=None,
52-
mapdicts=None, batch_size=None,
52+
mapdicts=None, batch_size=None, bulk_save=None,
5353
**keywords):
5454
"""
5555
Save data from a book to a nominated django models
@@ -59,6 +59,7 @@ def save_book_to_database(self, models=None, initializers=None,
5959
params['dest_initializers'] = initializers
6060
params['dest_mapdicts'] = mapdicts
6161
params['dest_batch_size'] = batch_size
62+
params['dest_bulk_save'] = bulk_save
6263
pe.save_book_as(**params)
6364

6465
def isave_to_database(self, model=None, initializer=None, mapdict=None,
@@ -74,7 +75,7 @@ def isave_to_database(self, model=None, initializer=None, mapdict=None,
7475
self.free_resources()
7576

7677
def isave_book_to_database(self, models=None, initializers=None,
77-
mapdicts=None, batch_size=None,
78+
mapdicts=None, batch_size=None, bulk_save=None,
7879
**keywords):
7980
"""
8081
Save data from a book to a nominated django models
@@ -84,6 +85,7 @@ def isave_book_to_database(self, models=None, initializers=None,
8485
params['dest_initializers'] = initializers
8586
params['dest_mapdicts'] = mapdicts
8687
params['dest_batch_size'] = batch_size
88+
params['dest_bulk_save'] = bulk_save
8789
pe.isave_book_as(**params)
8890

8991

polls/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@
2828
url(r'^import_using_isave/',
2929
views.import_data_using_isave_book_as),
3030
url(r'^import_sheet_using_isave/',
31-
views.import_sheet_using_isave_to_database)
31+
views.import_sheet_using_isave_to_database),
32+
url(r'^import_without_bulk_save/',
33+
views.import_without_bulk_save, name="import_no_bulk_save")
3234
]

polls/views.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,36 @@ def choice_func(row):
266266
'title': 'Import excel data into database example',
267267
'header': 'Please upload sample-data.xls:'
268268
})
269+
270+
271+
def import_without_bulk_save(request):
272+
if request.method == "POST":
273+
form = UploadFileForm(request.POST,
274+
request.FILES)
275+
276+
def choice_func(row):
277+
q = Question.objects.filter(slug=row[0])[0]
278+
row[0] = q
279+
return row
280+
if form.is_valid():
281+
request.FILES['file'].save_book_to_database(
282+
models=[Question, Choice],
283+
initializers=[None, choice_func],
284+
mapdicts=[
285+
['question_text', 'pub_date', 'slug'],
286+
['question', 'choice_text', 'votes']],
287+
bulk_save=False
288+
)
289+
return redirect('handson_view')
290+
else:
291+
return HttpResponseBadRequest()
292+
else:
293+
form = UploadFileForm()
294+
return render(
295+
request,
296+
'upload_form.html',
297+
{
298+
'form': form,
299+
'title': 'Import excel data into database example',
300+
'header': 'Please upload sample-data.xls:'
301+
})

rnd_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
https://github.com/pyexcel/pyexcel/archive/master.zip
2+
https://github.com/pyexcel/pyexcel/archive/master.zip
23
https://github.com/pyexcel/pyexcel-webio/archive/master.zip

testResponse.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,42 @@ def testBook(self):
218218
+---------------+----+-------------+-------+""").strip('\n') # noqa
219219
self.assertEqual(str(book), content)
220220

221+
def testBookWithoutBulkSave(self):
222+
fp = open(self.testfile, "rb")
223+
response = self.client.post('/polls/import/', data={"file": fp})
224+
eq_(response.status_code, 302)
225+
response2 = self.client.get('/polls/export/book')
226+
assert response2.status_code == 200
227+
book = pe.get_book(file_type='xls', file_content=response2.content)
228+
content = dedent("""
229+
question:
230+
+----+---------------------------+----------------------------------------------+----------+
231+
| id | pub_date | question_text | slug |
232+
+----+---------------------------+----------------------------------------------+----------+
233+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
234+
+----+---------------------------+----------------------------------------------+----------+
235+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
236+
+----+---------------------------+----------------------------------------------+----------+
237+
choice:
238+
+---------------+----+-------------+-------+
239+
| choice_text | id | question_id | votes |
240+
+---------------+----+-------------+-------+
241+
| Java | 1 | 1 | 0 |
242+
+---------------+----+-------------+-------+
243+
| C++ | 2 | 1 | 0 |
244+
+---------------+----+-------------+-------+
245+
| C | 3 | 1 | 0 |
246+
+---------------+----+-------------+-------+
247+
| Eclipse | 4 | 2 | 0 |
248+
+---------------+----+-------------+-------+
249+
| Visual Studio | 5 | 2 | 0 |
250+
+---------------+----+-------------+-------+
251+
| PyCharm | 6 | 2 | 0 |
252+
+---------------+----+-------------+-------+
253+
| IntelliJ | 7 | 2 | 0 |
254+
+---------------+----+-------------+-------+""").strip('\n') # noqa
255+
self.assertEqual(str(book), content)
256+
221257
def testBookUsingIsave(self):
222258
fp = open(self.testfile, "rb")
223259
response = self.client.post('/polls/import_using_isave/',

0 commit comments

Comments
 (0)