Skip to content

Commit 43c4a2b

Browse files
committed
implemented, documented and tested database operations
1 parent 8731daf commit 43c4a2b

File tree

5 files changed

+92
-28
lines changed

5 files changed

+92
-28
lines changed

db.sqlite3

3 KB
Binary file not shown.

django_excel/__init__.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,26 @@ def new_file(self, file_name, *args, **kwargs):
7474
make_response_from_book_dict,
7575
)
7676

77-
def make_response_from_a_model(model, file_type, status=200, **keywords):
78-
sheet = pe.get_sheet(model, **keywords)
77+
def make_response_from_a_table(model, file_type, status=200, **keywords):
78+
"""
79+
Produce a single sheet Excel book of *file_type*
80+
81+
:param model: a Django model
82+
:param file_type: same as :meth:`~django_excel.make_response`
83+
:param status: same as :meth:`~django_excel.make_response`
84+
"""
85+
sheet = pe.get_sheet(model=model, **keywords)
7986
return make_response(sheet, file_type, status, **keywords)
8087

81-
def make_response_from_models(models, file_type, status=200, **keywords):
82-
book = pe.get_book(models, **keywords)
88+
def make_response_from_tables(models, file_type, status=200, **keywords):
89+
"""
90+
Produce a multiple sheet Excel book of *file_type*. It becomes the same
91+
as :meth:`~django_excel.make_response_from_a_table` if you pass *tables*
92+
with an array that has a single table
93+
94+
:param models: a list of Django models
95+
:param file_type: same as :meth:`~django_excel.make_response`
96+
:param status: same as :meth:`~django_excel.make_response`
97+
"""
98+
book = pe.get_book(models=models, **keywords)
8399
return make_response(book, file_type, status, **keywords)

doc/source/index.rst

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,8 @@ Response methods
230230
:param file_type: same as :meth:`~django_excel.make_response`
231231
:param status: same as :meth:`~django_excel.make_response`
232232

233-
.. method:: make_response_from_a_table(session, table, file_type status=200)
234-
235-
Produce a single sheet Excel book of *file_type*
236-
237-
:param session: SQLAlchemy session
238-
:param table: a SQLAlchemy table
239-
:param file_type: same as :meth:`~django_excel.make_response`
240-
:param status: same as :meth:`~django_excel.make_response`
241-
242-
.. method:: make_response_from_tables(session, tables, file_type status=200)
243-
244-
Produce a multiple sheet Excel book of *file_type*. It becomes the same
245-
as :meth:`~django_excel.make_response_from_a_table` if you pass *tables*
246-
with an array that has a single table
247-
248-
:param session: SQLAlchemy session
249-
:param tables: SQLAlchemy tables
250-
:param file_type: same as :meth:`~django_excel.make_response`
251-
:param status: same as :meth:`~django_excel.make_response`
233+
.. autofunction:: make_response_from_a_table(model, file_type status=200)
234+
.. autofunction:: make_response_from_tables(models, file_type status=200)
252235

253236

254237
Indices and tables
@@ -257,4 +240,3 @@ Indices and tables
257240
* :ref:`genindex`
258241
* :ref:`modindex`
259242
* :ref:`search`
260-

polls/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def download(request, file_type):
4040

4141
def export_data(request, atype):
4242
if atype == "sheet":
43-
return excel.make_response_from_a_model(Question, 'csv')
43+
return excel.make_response_from_a_table(Question, 'xls')
4444
elif atype == "book":
45-
return excel.make_response_from_a_model([Question, Choice], 'csv')
45+
return excel.make_response_from_tables([Question, Choice], 'xls')
4646

4747
def import_data(request):
4848
if request.method == "POST":
@@ -59,7 +59,7 @@ def choice_func(row):
5959
(Choice, ['question', 'choice_text', 'votes'], choice_func, 0)
6060
]
6161
)
62-
return HttpResponse("OK")
62+
return HttpResponse("OK", status=200)
6363
else:
6464
return HttpResponseBadRequest()
6565
else:

testResponse.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from polls.models import Question, Choice
12
from django.test import Client, TestCase
23
from django.test.utils import override_settings
4+
from textwrap import dedent
35
import pyexcel as pe
46
import pyexcel.ext.xls
57
import pyexcel.ext.xlsx
@@ -11,7 +13,6 @@
1113
from ordereddict import OrderedDict
1214
else:
1315
from collections import OrderedDict
14-
1516
if PY2:
1617
import pyexcel.ext.ods
1718
from StringIO import StringIO
@@ -112,6 +113,71 @@ def test_exchange(self):
112113
os.unlink(tmp_filename)
113114

114115

116+
class DatabaseOperationsTestCase(TestCase):
117+
def setUp(self):
118+
self.testfile = "sample-data.xls"
119+
Question.objects.all().delete()
120+
Choice.objects.all().delete()
121+
122+
def testBook(self):
123+
fp = open(self.testfile, "rb")
124+
response = self.client.post('/polls/import/', data={"file": fp})
125+
assert response.status_code == 200
126+
response2 = self.client.get('/polls/export/book')
127+
assert response2.status_code == 200
128+
book = pe.load_book_from_memory('xls', response2.content)
129+
content = dedent("""
130+
Sheet Name: question
131+
+----+---------------------------+----------------------------------------------+----------+
132+
| id | pub_date | question_text | slug |
133+
+----+---------------------------+----------------------------------------------+----------+
134+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
135+
+----+---------------------------+----------------------------------------------+----------+
136+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
137+
+----+---------------------------+----------------------------------------------+----------+
138+
Sheet Name: choice
139+
+---------------+----+-------------+-------+
140+
| choice_text | id | question_id | votes |
141+
+---------------+----+-------------+-------+
142+
| Java | 1 | 1 | 0 |
143+
+---------------+----+-------------+-------+
144+
| C++ | 2 | 1 | 0 |
145+
+---------------+----+-------------+-------+
146+
| C | 3 | 1 | 0 |
147+
+---------------+----+-------------+-------+
148+
| Eclipse | 4 | 2 | 0 |
149+
+---------------+----+-------------+-------+
150+
| Visual Studio | 5 | 2 | 0 |
151+
+---------------+----+-------------+-------+
152+
| PyCharm | 6 | 2 | 0 |
153+
+---------------+----+-------------+-------+
154+
| IntelliJ | 7 | 2 | 0 |
155+
+---------------+----+-------------+-------+""").strip('\n')
156+
assert str(book) == content
157+
158+
def testSheet(self):
159+
fp = open(self.testfile, "rb")
160+
response = self.client.post('/polls/import/', data={"file": fp})
161+
assert response.status_code == 200
162+
response2 = self.client.get('/polls/export/sheet')
163+
assert response2.status_code == 200
164+
sheet = pe.load_from_memory('xls', response2.content)
165+
content = dedent("""
166+
Sheet Name: question
167+
+----+---------------------------+----------------------------------------------+----------+
168+
| id | pub_date | question_text | slug |
169+
+----+---------------------------+----------------------------------------------+----------+
170+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
171+
+----+---------------------------+----------------------------------------------+----------+
172+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
173+
+----+---------------------------+----------------------------------------------+----------+""").strip('\n')
174+
assert str(sheet) == content
175+
176+
115177
@override_settings(FILE_UPLOAD_MAX_MEMORY_SIZE=1)
116178
class ExcelResponseUsingFileTestCase(ExcelResponseTestCase):
179+
pass
180+
181+
@override_settings(FILE_UPLOAD_MAX_MEMORY_SIZE=1)
182+
class DatabaseOperationsUsingFileTestCase(DatabaseOperationsTestCase):
117183
pass

0 commit comments

Comments
 (0)