Skip to content

Commit a3f2149

Browse files
minhlongdochfw
andauthored
Make library django 3 compatible (#64)
* Exclude venv folder from flake8 * Use MIDDLEWARE instead of MIDDLEWARE_CLASSES if django version is greater than 1 * Ran 'make format' * Update changelog * Update django_excel.yml * Update lint.sh * Update lint.sh * Update .travis.yml * Update django_excel.yml Co-authored-by: jaska <[email protected]>
1 parent e6f9ddf commit a3f2149

File tree

7 files changed

+117
-70
lines changed

7 files changed

+117
-70
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ env:
1010
- DJANGO_VERSION=1.11.15
1111
- DJANGO_VERSION=2.0.8
1212
- DJANGO_VERSION=2.1
13+
- DJANGO_VERSION=3.1.2
1314
python:
1415
- 3.6
1516
- 3.7

changelog.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
name: django-excel
22
organisation: pyexcel-webwares
33
releases:
4+
- changes:
5+
- action: Added
6+
details:
7+
- 'Support for django 3.x.x'
8+
- 'Tested against django 3.x.x'
49
- changes:
510
- action: Added
611
details:

django_excel.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ keywords:
2828
- Django
2929
description:
3030
A django middleware that provides one application programming interface to read and write data in different excel file formats
31+
flake8_options: --ignore=F401,E402
32+
moban_command: false

django_excel/__init__.py

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,105 +8,128 @@
88
:copyright: (c) 2015 by Onni Software Ltd.
99
:license: New BSD License
1010
"""
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
1611
import pyexcel as pe
1712
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+
1823
from ._compact import DJANGO_ONE_SIX, PY2_VERSION, urllib_quote
1924

2025

2126
class ExcelMixin(webio.ExcelInput):
2227
"""
2328
Provide additional pyexcel-webio methods to Django's UploadedFiles
2429
"""
30+
2531
def get_params(self, **keywords):
2632
extension = self.name.split(".")[-1]
27-
keywords['file_type'] = extension
33+
keywords["file_type"] = extension
2834
self.file.seek(0)
2935
content = self.file.read()
3036
if content:
31-
keywords['file_content'] = content
37+
keywords["file_content"] = content
3238
else:
3339
raise IOError("No content was uploaded.")
3440
return keywords
3541

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+
):
3845
"""
3946
Save data from a sheet to a nominated django model
4047
"""
4148
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
4956
pe.save_as(**params)
5057

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+
):
5467
"""
5568
Save data from a book to a nominated django models
5669
"""
5770
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
6376
pe.save_book_as(**params)
6477

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+
):
6781
"""
6882
Save data from a sheet to a nominated django model
6983
"""
7084
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
7488
pe.isave_as(**params)
7589
self.free_resources()
7690

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+
):
80100
"""
81101
Save data from a book to a nominated django models
82102
"""
83103
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
89109
pe.isave_book_as(**params)
90110

91111

92112
class ExcelInMemoryUploadedFile(ExcelMixin, InMemoryUploadedFile):
93113
"""
94114
Mix-in pyexcel-webio methods in InMemoryUploadedFile
95115
"""
116+
96117
pass
97118

98119

99120
class TemporaryUploadedExcelFile(ExcelMixin, TemporaryUploadedFile):
100121
"""
101122
Mix-in pyexcel-webio methods in TemporaryUploadedFile
102123
"""
124+
103125
pass
104126

105127

106128
class ExcelMemoryFileUploadHandler(MemoryFileUploadHandler):
107129
"""
108130
Override MemoryFileUploadHandler to bring in ExcelInMemoryUploadedFile
109131
"""
132+
110133
def file_complete(self, file_size):
111134
if not self.activated:
112135
return
@@ -117,7 +140,7 @@ def file_complete(self, file_size):
117140
name=self.file_name,
118141
content_type=self.content_type,
119142
size=file_size,
120-
charset=self.charset
143+
charset=self.charset,
121144
)
122145
if not DJANGO_ONE_SIX:
123146
keywords["content_type_extra"] = self.content_type_extra
@@ -128,19 +151,15 @@ class TemporaryExcelFileUploadHandler(TemporaryFileUploadHandler):
128151
"""
129152
Override TemporaryFileUploadHandler to bring in TemporaryUploadedExcelFile
130153
"""
154+
131155
def new_file(self, file_name, *args, **kwargs):
132156
"""
133157
Create the file object to append to as data is coming in.
134158
"""
135159
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]
144163
if not DJANGO_ONE_SIX:
145164
custom_args.append(self.content_type_extra)
146165
self.file = TemporaryUploadedExcelFile(*custom_args)
@@ -153,30 +172,33 @@ def _make_response(content, content_type, status, file_name=None):
153172
response = HttpResponse(content, content_type=content_type, status=status)
154173
if file_name:
155174
if PY2_VERSION and isinstance(file_name, unicode):
156-
file_name = file_name.encode('utf-8')
175+
file_name = file_name.encode("utf-8")
157176
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,
161182
)
162183
return response
163184

164185

165186
webio.init_webio(_make_response)
166187

167188

168-
from pyexcel_webio import ( # noqa
189+
from pyexcel_webio import (
169190
make_response,
170-
make_response_from_array,
191+
make_response_from_array, # noqa
192+
make_response_from_book_dict,
171193
make_response_from_dict,
194+
make_response_from_query_sets,
172195
make_response_from_records,
173-
make_response_from_book_dict,
174-
make_response_from_query_sets
175196
)
176197

177198

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+
):
180202
"""
181203
Produce a single sheet Excel book of *file_type*
182204
@@ -185,12 +207,14 @@ def make_response_from_a_table(model, file_type,
185207
:param status: same as :meth:`~django_excel.make_response`
186208
"""
187209
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+
)
190213

191214

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+
):
194218
"""
195219
Produce a multiple sheet Excel book of *file_type*. It becomes the same
196220
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,
201225
:param status: same as :meth:`~django_excel.make_response`
202226
"""
203227
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+
)

django_excel/_compact.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import pkg_resources
21
from distutils.version import LooseVersion
32

3+
import pkg_resources
4+
45
try:
56
# if in py2
67
from urllib import quote as urllib_quote
8+
79
PY2_VERSION = True
810
except ImportError:
911
# else (aka in py3)
1012
from urllib.parse import quote as urllib_quote # noqa: F401
13+
1114
PY2_VERSION = False
1215

1316

14-
django_version = pkg_resources.get_distribution('django').version
17+
django_version = pkg_resources.get_distribution("django").version
1518

1619

17-
DJANGO_ONE_SIX = (
18-
LooseVersion(django_version) < LooseVersion("1.7.0") and
19-
LooseVersion(django_version) > LooseVersion("1.5.12")
20-
)
20+
DJANGO_ONE_SIX = LooseVersion(django_version) < LooseVersion(
21+
"1.7.0"
22+
) and LooseVersion(django_version) > LooseVersion("1.5.12")

lint.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pip install flake8
2-
flake8 --exclude=.moban.d,docs,setup.py --builtins=unicode,xrange,long . && python setup.py checkdocs
2+
3+
flake8 --exclude=.moban.d,docs,setup.py --ignore=F401,E402,W504,E226 --builtins=unicode,xrange,long . && python setup.py checkdocs
4+

mysite/settings.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
1212
import os
13+
import django
1314

1415
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
1516

@@ -40,7 +41,7 @@
4041
'polls'
4142
)
4243

43-
MIDDLEWARE_CLASSES = (
44+
COMMON_MIDDLEWARE = (
4445
'django.contrib.sessions.middleware.SessionMiddleware',
4546
'django.middleware.common.CommonMiddleware',
4647
'django.middleware.csrf.CsrfViewMiddleware',
@@ -49,6 +50,15 @@
4950
'django.middleware.clickjacking.XFrameOptionsMiddleware'
5051
)
5152

53+
if django.VERSION[0] > 1:
54+
MIDDLEWARE = (
55+
'django.middleware.security.SecurityMiddleware',
56+
*COMMON_MIDDLEWARE
57+
)
58+
59+
else:
60+
MIDDLEWARE_CLASSES = COMMON_MIDDLEWARE
61+
5262

5363
ROOT_URLCONF = 'mysite.urls'
5464

0 commit comments

Comments
 (0)