Skip to content

Commit

Permalink
✨ expose bulk_save parameter. related to pyexcel/pyexcel-io#46
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Jan 11, 2018
1 parent 684d787 commit 4b8a71f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
6 changes: 4 additions & 2 deletions django_excel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def save_to_database(self, model=None, initializer=None, mapdict=None,
pe.save_as(**params)

def save_book_to_database(self, models=None, initializers=None,
mapdicts=None, batch_size=None,
mapdicts=None, batch_size=None, bulk_save=None,
**keywords):
"""
Save data from a book to a nominated django models
Expand All @@ -59,6 +59,7 @@ def save_book_to_database(self, models=None, initializers=None,
params['dest_initializers'] = initializers
params['dest_mapdicts'] = mapdicts
params['dest_batch_size'] = batch_size
params['dest_bulk_save'] = bulk_save
pe.save_book_as(**params)

def isave_to_database(self, model=None, initializer=None, mapdict=None,
Expand All @@ -74,7 +75,7 @@ def isave_to_database(self, model=None, initializer=None, mapdict=None,
self.free_resources()

def isave_book_to_database(self, models=None, initializers=None,
mapdicts=None, batch_size=None,
mapdicts=None, batch_size=None, bulk_save=None,
**keywords):
"""
Save data from a book to a nominated django models
Expand All @@ -84,6 +85,7 @@ def isave_book_to_database(self, models=None, initializers=None,
params['dest_initializers'] = initializers
params['dest_mapdicts'] = mapdicts
params['dest_batch_size'] = batch_size
params['dest_bulk_save'] = bulk_save
pe.isave_book_as(**params)


Expand Down
4 changes: 3 additions & 1 deletion polls/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
url(r'^import_using_isave/',
views.import_data_using_isave_book_as),
url(r'^import_sheet_using_isave/',
views.import_sheet_using_isave_to_database)
views.import_sheet_using_isave_to_database),
url(r'^import_without_bulk_save/',
views.import_without_bulk_save, name="import_no_bulk_save")
]
33 changes: 33 additions & 0 deletions polls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,36 @@ def choice_func(row):
'title': 'Import excel data into database example',
'header': 'Please upload sample-data.xls:'
})


def import_without_bulk_save(request):
if request.method == "POST":
form = UploadFileForm(request.POST,
request.FILES)

def choice_func(row):
q = Question.objects.filter(slug=row[0])[0]
row[0] = q
return row
if form.is_valid():
request.FILES['file'].save_book_to_database(
models=[Question, Choice],
initializers=[None, choice_func],
mapdicts=[
['question_text', 'pub_date', 'slug'],
['question', 'choice_text', 'votes']],
bulk_save=False
)
return redirect('handson_view')
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(
request,
'upload_form.html',
{
'form': form,
'title': 'Import excel data into database example',
'header': 'Please upload sample-data.xls:'
})
1 change: 1 addition & 0 deletions rnd_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
https://github.com/pyexcel/pyexcel/archive/master.zip
https://github.com/pyexcel/pyexcel/archive/master.zip
https://github.com/pyexcel/pyexcel-webio/archive/master.zip
36 changes: 36 additions & 0 deletions testResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,42 @@ def testBook(self):
+---------------+----+-------------+-------+""").strip('\n') # noqa
self.assertEqual(str(book), content)

def testBookWithoutBulkSave(self):
fp = open(self.testfile, "rb")
response = self.client.post('/polls/import/', data={"file": fp})
eq_(response.status_code, 302)
response2 = self.client.get('/polls/export/book')
assert response2.status_code == 200
book = pe.get_book(file_type='xls', file_content=response2.content)
content = dedent("""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+
choice:
+---------------+----+-------------+-------+
| choice_text | id | question_id | votes |
+---------------+----+-------------+-------+
| Java | 1 | 1 | 0 |
+---------------+----+-------------+-------+
| C++ | 2 | 1 | 0 |
+---------------+----+-------------+-------+
| C | 3 | 1 | 0 |
+---------------+----+-------------+-------+
| Eclipse | 4 | 2 | 0 |
+---------------+----+-------------+-------+
| Visual Studio | 5 | 2 | 0 |
+---------------+----+-------------+-------+
| PyCharm | 6 | 2 | 0 |
+---------------+----+-------------+-------+
| IntelliJ | 7 | 2 | 0 |
+---------------+----+-------------+-------+""").strip('\n') # noqa
self.assertEqual(str(book), content)

def testBookUsingIsave(self):
fp = open(self.testfile, "rb")
response = self.client.post('/polls/import_using_isave/',
Expand Down

0 comments on commit 4b8a71f

Please sign in to comment.