8
8
:copyright: (c) 2015 by Onni Software Ltd.
9
9
:license: New BSD License
10
10
"""
11
- from django .core .files .uploadhandler import (
12
- MemoryFileUploadHandler , TemporaryFileUploadHandler )
13
- from django .core .files .uploadedfile import (
14
- InMemoryUploadedFile , TemporaryUploadedFile )
15
- from django .http import HttpResponse
16
11
import pyexcel as pe
17
12
import pyexcel_webio as webio
13
+ from django .core .files .uploadedfile import (
14
+ InMemoryUploadedFile ,
15
+ TemporaryUploadedFile ,
16
+ )
17
+ from django .core .files .uploadhandler import (
18
+ MemoryFileUploadHandler ,
19
+ TemporaryFileUploadHandler ,
20
+ )
21
+ from django .http import HttpResponse
22
+
18
23
from ._compact import DJANGO_ONE_SIX , PY2_VERSION , urllib_quote
19
24
20
25
21
26
class ExcelMixin (webio .ExcelInput ):
22
27
"""
23
28
Provide additional pyexcel-webio methods to Django's UploadedFiles
24
29
"""
30
+
25
31
def get_params (self , ** keywords ):
26
32
extension = self .name .split ("." )[- 1 ]
27
- keywords [' file_type' ] = extension
33
+ keywords [" file_type" ] = extension
28
34
self .file .seek (0 )
29
35
content = self .file .read ()
30
36
if content :
31
- keywords [' file_content' ] = content
37
+ keywords [" file_content" ] = content
32
38
else :
33
39
raise IOError ("No content was uploaded." )
34
40
return keywords
35
41
36
- def save_to_database (self , model = None , initializer = None , mapdict = None ,
37
- ** keywords ):
42
+ def save_to_database (
43
+ self , model = None , initializer = None , mapdict = None , ** keywords
44
+ ):
38
45
"""
39
46
Save data from a sheet to a nominated django model
40
47
"""
41
48
params = self .get_params (** keywords )
42
- if ' name_columns_by_row' not in params :
43
- params [' name_columns_by_row' ] = 0
44
- if ' name_rows_by_column' not in params :
45
- params [' name_rows_by_column' ] = - 1
46
- params [' dest_model' ] = model
47
- params [' dest_initializer' ] = initializer
48
- params [' dest_mapdict' ] = mapdict
49
+ if " name_columns_by_row" not in params :
50
+ params [" name_columns_by_row" ] = 0
51
+ if " name_rows_by_column" not in params :
52
+ params [" name_rows_by_column" ] = - 1
53
+ params [" dest_model" ] = model
54
+ params [" dest_initializer" ] = initializer
55
+ params [" dest_mapdict" ] = mapdict
49
56
pe .save_as (** params )
50
57
51
- def save_book_to_database (self , models = None , initializers = None ,
52
- mapdicts = None , batch_size = None , bulk_save = None ,
53
- ** keywords ):
58
+ def save_book_to_database (
59
+ self ,
60
+ models = None ,
61
+ initializers = None ,
62
+ mapdicts = None ,
63
+ batch_size = None ,
64
+ bulk_save = None ,
65
+ ** keywords
66
+ ):
54
67
"""
55
68
Save data from a book to a nominated django models
56
69
"""
57
70
params = self .get_params (** keywords )
58
- params [' dest_models' ] = models
59
- params [' dest_initializers' ] = initializers
60
- params [' dest_mapdicts' ] = mapdicts
61
- params [' dest_batch_size' ] = batch_size
62
- params [' dest_bulk_save' ] = bulk_save
71
+ params [" dest_models" ] = models
72
+ params [" dest_initializers" ] = initializers
73
+ params [" dest_mapdicts" ] = mapdicts
74
+ params [" dest_batch_size" ] = batch_size
75
+ params [" dest_bulk_save" ] = bulk_save
63
76
pe .save_book_as (** params )
64
77
65
- def isave_to_database (self , model = None , initializer = None , mapdict = None ,
66
- ** keywords ):
78
+ def isave_to_database (
79
+ self , model = None , initializer = None , mapdict = None , ** keywords
80
+ ):
67
81
"""
68
82
Save data from a sheet to a nominated django model
69
83
"""
70
84
params = self .get_params (** keywords )
71
- params [' dest_model' ] = model
72
- params [' dest_initializer' ] = initializer
73
- params [' dest_mapdict' ] = mapdict
85
+ params [" dest_model" ] = model
86
+ params [" dest_initializer" ] = initializer
87
+ params [" dest_mapdict" ] = mapdict
74
88
pe .isave_as (** params )
75
89
self .free_resources ()
76
90
77
- def isave_book_to_database (self , models = None , initializers = None ,
78
- mapdicts = None , batch_size = None , bulk_save = None ,
79
- ** keywords ):
91
+ def isave_book_to_database (
92
+ self ,
93
+ models = None ,
94
+ initializers = None ,
95
+ mapdicts = None ,
96
+ batch_size = None ,
97
+ bulk_save = None ,
98
+ ** keywords
99
+ ):
80
100
"""
81
101
Save data from a book to a nominated django models
82
102
"""
83
103
params = self .get_params (** keywords )
84
- params [' dest_models' ] = models
85
- params [' dest_initializers' ] = initializers
86
- params [' dest_mapdicts' ] = mapdicts
87
- params [' dest_batch_size' ] = batch_size
88
- params [' dest_bulk_save' ] = bulk_save
104
+ params [" dest_models" ] = models
105
+ params [" dest_initializers" ] = initializers
106
+ params [" dest_mapdicts" ] = mapdicts
107
+ params [" dest_batch_size" ] = batch_size
108
+ params [" dest_bulk_save" ] = bulk_save
89
109
pe .isave_book_as (** params )
90
110
91
111
92
112
class ExcelInMemoryUploadedFile (ExcelMixin , InMemoryUploadedFile ):
93
113
"""
94
114
Mix-in pyexcel-webio methods in InMemoryUploadedFile
95
115
"""
116
+
96
117
pass
97
118
98
119
99
120
class TemporaryUploadedExcelFile (ExcelMixin , TemporaryUploadedFile ):
100
121
"""
101
122
Mix-in pyexcel-webio methods in TemporaryUploadedFile
102
123
"""
124
+
103
125
pass
104
126
105
127
106
128
class ExcelMemoryFileUploadHandler (MemoryFileUploadHandler ):
107
129
"""
108
130
Override MemoryFileUploadHandler to bring in ExcelInMemoryUploadedFile
109
131
"""
132
+
110
133
def file_complete (self , file_size ):
111
134
if not self .activated :
112
135
return
@@ -117,7 +140,7 @@ def file_complete(self, file_size):
117
140
name = self .file_name ,
118
141
content_type = self .content_type ,
119
142
size = file_size ,
120
- charset = self .charset
143
+ charset = self .charset ,
121
144
)
122
145
if not DJANGO_ONE_SIX :
123
146
keywords ["content_type_extra" ] = self .content_type_extra
@@ -128,19 +151,15 @@ class TemporaryExcelFileUploadHandler(TemporaryFileUploadHandler):
128
151
"""
129
152
Override TemporaryFileUploadHandler to bring in TemporaryUploadedExcelFile
130
153
"""
154
+
131
155
def new_file (self , file_name , * args , ** kwargs ):
132
156
"""
133
157
Create the file object to append to as data is coming in.
134
158
"""
135
159
super (TemporaryFileUploadHandler , self ).new_file (
136
- file_name ,
137
- * args ,
138
- ** kwargs )
139
- custom_args = [
140
- self .file_name ,
141
- self .content_type ,
142
- 0 ,
143
- self .charset ]
160
+ file_name , * args , ** kwargs
161
+ )
162
+ custom_args = [self .file_name , self .content_type , 0 , self .charset ]
144
163
if not DJANGO_ONE_SIX :
145
164
custom_args .append (self .content_type_extra )
146
165
self .file = TemporaryUploadedExcelFile (* custom_args )
@@ -153,30 +172,33 @@ def _make_response(content, content_type, status, file_name=None):
153
172
response = HttpResponse (content , content_type = content_type , status = status )
154
173
if file_name :
155
174
if PY2_VERSION and isinstance (file_name , unicode ):
156
- file_name = file_name .encode (' utf-8' )
175
+ file_name = file_name .encode (" utf-8" )
157
176
url_encoded_file_name = urllib_quote (file_name )
158
- response ["Content-Disposition" ] = (
159
- "attachment; filename=%s;filename*=utf-8''%s"
160
- % (url_encoded_file_name , url_encoded_file_name )
177
+ response [
178
+ "Content-Disposition"
179
+ ] = "attachment; filename=%s;filename*=utf-8''%s" % (
180
+ url_encoded_file_name ,
181
+ url_encoded_file_name ,
161
182
)
162
183
return response
163
184
164
185
165
186
webio .init_webio (_make_response )
166
187
167
188
168
- from pyexcel_webio import ( # noqa
189
+ from pyexcel_webio import (
169
190
make_response ,
170
- make_response_from_array ,
191
+ make_response_from_array , # noqa
192
+ make_response_from_book_dict ,
171
193
make_response_from_dict ,
194
+ make_response_from_query_sets ,
172
195
make_response_from_records ,
173
- make_response_from_book_dict ,
174
- make_response_from_query_sets
175
196
)
176
197
177
198
178
- def make_response_from_a_table (model , file_type ,
179
- status = 200 , file_name = None , ** keywords ):
199
+ def make_response_from_a_table (
200
+ model , file_type , status = 200 , file_name = None , ** keywords
201
+ ):
180
202
"""
181
203
Produce a single sheet Excel book of *file_type*
182
204
@@ -185,12 +207,14 @@ def make_response_from_a_table(model, file_type,
185
207
:param status: same as :meth:`~django_excel.make_response`
186
208
"""
187
209
sheet = pe .get_sheet (model = model , ** keywords )
188
- return make_response (sheet , file_type , status ,
189
- file_name = file_name , ** keywords )
210
+ return make_response (
211
+ sheet , file_type , status , file_name = file_name , ** keywords
212
+ )
190
213
191
214
192
- def make_response_from_tables (models , file_type ,
193
- status = 200 , file_name = None , ** keywords ):
215
+ def make_response_from_tables (
216
+ models , file_type , status = 200 , file_name = None , ** keywords
217
+ ):
194
218
"""
195
219
Produce a multiple sheet Excel book of *file_type*. It becomes the same
196
220
as :meth:`~django_excel.make_response_from_a_table` if you pass *tables*
@@ -201,5 +225,6 @@ def make_response_from_tables(models, file_type,
201
225
:param status: same as :meth:`~django_excel.make_response`
202
226
"""
203
227
book = pe .get_book (models = models , ** keywords )
204
- return make_response (book , file_type , status ,
205
- file_name = file_name , ** keywords )
228
+ return make_response (
229
+ book , file_type , status , file_name = file_name , ** keywords
230
+ )
0 commit comments