django-spreadsheets 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_spreadsheets-1.1.0/LICENSE +12 -0
- django_spreadsheets-1.1.0/PKG-INFO +231 -0
- django_spreadsheets-1.1.0/README.md +208 -0
- django_spreadsheets-1.1.0/django_spreadsheets/__init__.py +1 -0
- django_spreadsheets-1.1.0/django_spreadsheets/admin.py +6 -0
- django_spreadsheets-1.1.0/django_spreadsheets/apps.py +20 -0
- django_spreadsheets-1.1.0/django_spreadsheets/autodiscover.py +25 -0
- django_spreadsheets-1.1.0/django_spreadsheets/base_config.py +219 -0
- django_spreadsheets-1.1.0/django_spreadsheets/conf/__init__.py +0 -0
- django_spreadsheets-1.1.0/django_spreadsheets/conf/settings.py +21 -0
- django_spreadsheets-1.1.0/django_spreadsheets/constants.py +18 -0
- django_spreadsheets-1.1.0/django_spreadsheets/errors.py +199 -0
- django_spreadsheets-1.1.0/django_spreadsheets/exceptions.py +14 -0
- django_spreadsheets-1.1.0/django_spreadsheets/exporter.py +171 -0
- django_spreadsheets-1.1.0/django_spreadsheets/forms.py +93 -0
- django_spreadsheets-1.1.0/django_spreadsheets/importer.py +537 -0
- django_spreadsheets-1.1.0/django_spreadsheets/locale/fr/LC_MESSAGES/django.mo +0 -0
- django_spreadsheets-1.1.0/django_spreadsheets/locale/fr/LC_MESSAGES/django.po +328 -0
- django_spreadsheets-1.1.0/django_spreadsheets/registry.py +26 -0
- django_spreadsheets-1.1.0/django_spreadsheets/shortcuts.py +69 -0
- django_spreadsheets-1.1.0/django_spreadsheets/templates/spreadsheets/base.html +52 -0
- django_spreadsheets-1.1.0/django_spreadsheets/templates/spreadsheets/done.html +10 -0
- django_spreadsheets-1.1.0/django_spreadsheets/templates/spreadsheets/export_index.html +13 -0
- django_spreadsheets-1.1.0/django_spreadsheets/templates/spreadsheets/import_index.html +13 -0
- django_spreadsheets-1.1.0/django_spreadsheets/templates/spreadsheets/wizard.html +128 -0
- django_spreadsheets-1.1.0/django_spreadsheets/urls.py +34 -0
- django_spreadsheets-1.1.0/django_spreadsheets/validation.py +162 -0
- django_spreadsheets-1.1.0/django_spreadsheets/views.py +272 -0
- django_spreadsheets-1.1.0/pyproject.toml +58 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Copyright 2024 KAPT.
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
7
|
+
|
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9
|
+
|
|
10
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
11
|
+
|
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: django_spreadsheets
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Import from and export to XLSX files with data validation.
|
|
5
|
+
Home-page: https://gitlab.com/kapt/open-source/django_spreadsheets
|
|
6
|
+
License: BSD-3-Clause
|
|
7
|
+
Author: Kapt dev team
|
|
8
|
+
Author-email: dev@kapt.mobi
|
|
9
|
+
Requires-Python: >=3.8,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Dist: Django (>=3.0,<5)
|
|
18
|
+
Requires-Dist: django-formtools (>=2.5.1,<3.0.0)
|
|
19
|
+
Requires-Dist: openpyxl (>=3.1.5,<4.0.0)
|
|
20
|
+
Project-URL: Repository, https://gitlab.com/kapt/open-source/django_spreadsheets
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# django-spreadsheet
|
|
24
|
+
|
|
25
|
+
Validate your XLSX spreadsheet file before importing it. Export your data to a formatted XLSX spreadsheet with data validation set up.
|
|
26
|
+
|
|
27
|
+
# Requirements
|
|
28
|
+
|
|
29
|
+
- Python 3.8+
|
|
30
|
+
- Django 2.0+
|
|
31
|
+
|
|
32
|
+
# Installation
|
|
33
|
+
|
|
34
|
+
- run `pip install django_spreadsheets`
|
|
35
|
+
- add `django_spreadsheets.apps.SpreadsheetAppConfig` to your `INSTALLED_APPS`
|
|
36
|
+
- add `path("admin/spreadsheets/", include("django_spreadsheets.urls", namespace="spreadsheets"))` to your root `urls.py`
|
|
37
|
+
- run `python manage.py migrate django_spreadsheets`
|
|
38
|
+
|
|
39
|
+
# Usage
|
|
40
|
+
|
|
41
|
+
Start to create a file `spreadsheets.py` in your app. Inside this file, describe your spreadsheets configurations such as the following example:
|
|
42
|
+
|
|
43
|
+
```py
|
|
44
|
+
from django_spreadsheets.base_config import SpreadsheetConfig, SheetConfig
|
|
45
|
+
from django_spreadsheets.registry import register
|
|
46
|
+
from libs.bananas.models import Banana
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class BananaSheetConfig(SheetConfig):
|
|
50
|
+
"""Describes each columns of this sheet."""
|
|
51
|
+
|
|
52
|
+
ID = {
|
|
53
|
+
"name": "Identifier",
|
|
54
|
+
"required": True,
|
|
55
|
+
}
|
|
56
|
+
COLOR = {
|
|
57
|
+
"name": "Color",
|
|
58
|
+
"validation": {
|
|
59
|
+
"type": "list",
|
|
60
|
+
"formula1": ["yellow", "green", "brown"],
|
|
61
|
+
},
|
|
62
|
+
"required": True,
|
|
63
|
+
}
|
|
64
|
+
SIZE = {
|
|
65
|
+
"name": "Size",
|
|
66
|
+
"validation": {
|
|
67
|
+
"type": "whole",
|
|
68
|
+
"operator": "greaterThan",
|
|
69
|
+
"formula1": 0,
|
|
70
|
+
"error": "Please enter a positive number",
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
def get_data_to_export(self):
|
|
75
|
+
return Banana.objects.values_list("id", "color", "size")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@register
|
|
79
|
+
class BananaSpreadsheetConfig(SpreadsheetConfig):
|
|
80
|
+
sheets_config = {
|
|
81
|
+
"Banana": BananaSheetConfig()
|
|
82
|
+
# the key will be used as the name of the sheet, the value is a class describing the sheet's configuration.
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class Meta:
|
|
86
|
+
verbose_name = "Bananas"
|
|
87
|
+
|
|
88
|
+
def update_database(self, sheets):
|
|
89
|
+
for sheet_name, rows in sheets.items():
|
|
90
|
+
for row in rows:
|
|
91
|
+
Banana.objects.get_or_create(
|
|
92
|
+
id=row["ID"],
|
|
93
|
+
color=row["COLOR"],
|
|
94
|
+
size=row["SIZE"],
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Then, you can go to `//localhost:8000/admin/spreadsheets/export/` to export a XSLX file and `//localhost:8000/admin/spreadsheets/import/` to import into your database.
|
|
99
|
+
|
|
100
|
+
# `SpreadsheetConfig` class
|
|
101
|
+
|
|
102
|
+
Inherit this class to describe your spreadsheets' configuration. Define a `sheets_config` dict attribute containing the name of your sheet as a key, and a `SheetConfig` class as a value.
|
|
103
|
+
|
|
104
|
+
## Validations
|
|
105
|
+
|
|
106
|
+
Several validations are run before importing a spreadsheet:
|
|
107
|
+
|
|
108
|
+
### Config version validation
|
|
109
|
+
|
|
110
|
+
Validate that the configuration version stored in the file is the same as the `SpreadsheetConfig.version` used.
|
|
111
|
+
|
|
112
|
+
### Sheets validation
|
|
113
|
+
|
|
114
|
+
Validate that all sheets in this `SpreadsheetConfig` class used, exist in imported file. Validate that all sheets in imported file, exist in this `SpreadsheetConfig` class used.
|
|
115
|
+
|
|
116
|
+
### Columns headers validation
|
|
117
|
+
|
|
118
|
+
Validate that the imported file contains the same columns headers as described in the `SheetConfig` used.
|
|
119
|
+
|
|
120
|
+
### Required data validation
|
|
121
|
+
|
|
122
|
+
Validate that there is a value in the imported file for each `required`, `required_unless`, `required_if` columns.
|
|
123
|
+
|
|
124
|
+
### Uniqueness validation [/!\ not implemented]
|
|
125
|
+
|
|
126
|
+
Validate that multiple cells in a row are unique together in the imported file.
|
|
127
|
+
|
|
128
|
+
### Types validation
|
|
129
|
+
|
|
130
|
+
Validate that each column in the imported file contains data of correct type (date, positive integer, ...).
|
|
131
|
+
|
|
132
|
+
### Data match validation
|
|
133
|
+
|
|
134
|
+
Validate that we find an object in the database matching the value in the imported file. If not, we ask the user to chose from a list of possibles values.
|
|
135
|
+
|
|
136
|
+
## Import data
|
|
137
|
+
|
|
138
|
+
If you want to import data in y our database from this spreadsheet, you need to define a `update_database(self, sheets)` method. This method is called after all data validations have been completed, and receive a python dict with an item for each sheet. The key is the name of the sheet, and the value is a python list of rows. Each row is a list containing a value for each column.
|
|
139
|
+
|
|
140
|
+
```py
|
|
141
|
+
def update_database(self, sheets):
|
|
142
|
+
for sheet_name, rows in sheets.items():
|
|
143
|
+
for row in rows:
|
|
144
|
+
for value in row:
|
|
145
|
+
print(value)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# `SheetConfig` class
|
|
150
|
+
|
|
151
|
+
Inherit this class to describe your sheets' configuration. Each attribute describes a column using a dictionary such as the following:
|
|
152
|
+
|
|
153
|
+
```py
|
|
154
|
+
<column_identifier> = {
|
|
155
|
+
"name": str (required), # The name of the column used in the header row
|
|
156
|
+
"required": bool, # Whether a value in the column is required when importing
|
|
157
|
+
"required_unless": str, # Whether a value in this column is required if the pointed column is empty
|
|
158
|
+
"required_if": str, # Whether a value in this column is required if the pointed column is filled
|
|
159
|
+
"comment": str, # A comment that appears in the header cell
|
|
160
|
+
"admin_only": bool, # Whether this column should be exported only when the current user is an administrator
|
|
161
|
+
"validation": { # A data validation applied to each cell of this column
|
|
162
|
+
"name": str, # A name used as header in the hidden config sheet
|
|
163
|
+
"type": str (required), # <url|date|list|range|whole...> See openpyxl documentation to get all available validation types
|
|
164
|
+
... # Same args as openpyxl's DataValidation objects
|
|
165
|
+
},
|
|
166
|
+
"header_style": str, # The name of a style that already exists in the SpreadsheetConfig to which this sheet belongs
|
|
167
|
+
"style": str, # The name of a style that already exists in the SpreadsheetConfig to which this sheet belongs
|
|
168
|
+
"number_format": str # A value among FORMAT_* constants from openpyxl.styles.numbers or others values supported by XLSX spec
|
|
169
|
+
},
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Note on validation
|
|
173
|
+
|
|
174
|
+
To create a _boolean_ validation, you may use a `list` validation type with `django_spreadsheets.constants.YES_NO_CHOICES` as `formula1` value. To ensure a boolean value is displayed in your exported file, add `"number_format": "BOOLEAN"` in your column configuration.
|
|
175
|
+
|
|
176
|
+
## Note on number format
|
|
177
|
+
|
|
178
|
+
When the value in your exported file is not displayed correctly or not consistent across rows, you may define the `number_format` key in your column configuration.
|
|
179
|
+
|
|
180
|
+
Openxpyxl's format constants are not exhaustive. You may use other supported strings (eg: "BOOLEAN").
|
|
181
|
+
|
|
182
|
+
## Export data
|
|
183
|
+
|
|
184
|
+
If you want to export data to this sheet, you need to define a `get_data_to_export(self)` method. This method must return a python list containing an item by row. Each item in the list is a list containing a value for each column.
|
|
185
|
+
|
|
186
|
+
```py
|
|
187
|
+
def get_data_to_export(self):
|
|
188
|
+
return [
|
|
189
|
+
[1, "yellow", 20],
|
|
190
|
+
[1, "green", 22],
|
|
191
|
+
]
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
# Settings
|
|
195
|
+
|
|
196
|
+
## `SPREADSHEET_CONFIG_SHEET_TITLE`
|
|
197
|
+
|
|
198
|
+
Default: `_("Configuration")`
|
|
199
|
+
|
|
200
|
+
The name of the (hidden) config sheet in the generated file. This special sheet is used to store data needed for validation (eg. lists of choices).
|
|
201
|
+
|
|
202
|
+
## `SPREADSHEET_MAX_VALIDATION_ROWS`
|
|
203
|
+
|
|
204
|
+
Default: `700`
|
|
205
|
+
|
|
206
|
+
When exporting data, the generated spreadsheet will have at least `SPREADSHEET_MAX_VALIDATION_ROWS` rows with data validation set up.
|
|
207
|
+
|
|
208
|
+
## `SPREADSHEET_MAX_STYLED_ROWS`
|
|
209
|
+
|
|
210
|
+
Default: `700`
|
|
211
|
+
|
|
212
|
+
When exporting data, the generated spreadsheet will have at least `SPREADSHEET_MAX_STYLED_ROWS` rows styled.
|
|
213
|
+
|
|
214
|
+
## `SPREADSHEET_COMMENT_AUTHOR`
|
|
215
|
+
|
|
216
|
+
Default: `"Django Spreadsheet"`
|
|
217
|
+
|
|
218
|
+
The name of the author used when adding comments on cells.
|
|
219
|
+
|
|
220
|
+
## `SPREADSHEET_EXPORT_FILE_NAME`
|
|
221
|
+
|
|
222
|
+
Default: `"export {config_name} {date}.xlsx"`
|
|
223
|
+
|
|
224
|
+
The name of the exported file when downloaded. Token `config_name` and `date` will be replaced respectively by the name of the configuration that is currently exported and the current date.
|
|
225
|
+
|
|
226
|
+
## `SPREADSHEET_TEMPLATE_FILE_NAME`
|
|
227
|
+
|
|
228
|
+
Default: `"{config_name} template file.xlsx"`
|
|
229
|
+
|
|
230
|
+
The name of the template file when downloaded. Token `config_name` will be replaced by the name of the configuration that is currently exported.
|
|
231
|
+
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# django-spreadsheet
|
|
2
|
+
|
|
3
|
+
Validate your XLSX spreadsheet file before importing it. Export your data to a formatted XLSX spreadsheet with data validation set up.
|
|
4
|
+
|
|
5
|
+
# Requirements
|
|
6
|
+
|
|
7
|
+
- Python 3.8+
|
|
8
|
+
- Django 2.0+
|
|
9
|
+
|
|
10
|
+
# Installation
|
|
11
|
+
|
|
12
|
+
- run `pip install django_spreadsheets`
|
|
13
|
+
- add `django_spreadsheets.apps.SpreadsheetAppConfig` to your `INSTALLED_APPS`
|
|
14
|
+
- add `path("admin/spreadsheets/", include("django_spreadsheets.urls", namespace="spreadsheets"))` to your root `urls.py`
|
|
15
|
+
- run `python manage.py migrate django_spreadsheets`
|
|
16
|
+
|
|
17
|
+
# Usage
|
|
18
|
+
|
|
19
|
+
Start to create a file `spreadsheets.py` in your app. Inside this file, describe your spreadsheets configurations such as the following example:
|
|
20
|
+
|
|
21
|
+
```py
|
|
22
|
+
from django_spreadsheets.base_config import SpreadsheetConfig, SheetConfig
|
|
23
|
+
from django_spreadsheets.registry import register
|
|
24
|
+
from libs.bananas.models import Banana
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class BananaSheetConfig(SheetConfig):
|
|
28
|
+
"""Describes each columns of this sheet."""
|
|
29
|
+
|
|
30
|
+
ID = {
|
|
31
|
+
"name": "Identifier",
|
|
32
|
+
"required": True,
|
|
33
|
+
}
|
|
34
|
+
COLOR = {
|
|
35
|
+
"name": "Color",
|
|
36
|
+
"validation": {
|
|
37
|
+
"type": "list",
|
|
38
|
+
"formula1": ["yellow", "green", "brown"],
|
|
39
|
+
},
|
|
40
|
+
"required": True,
|
|
41
|
+
}
|
|
42
|
+
SIZE = {
|
|
43
|
+
"name": "Size",
|
|
44
|
+
"validation": {
|
|
45
|
+
"type": "whole",
|
|
46
|
+
"operator": "greaterThan",
|
|
47
|
+
"formula1": 0,
|
|
48
|
+
"error": "Please enter a positive number",
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
def get_data_to_export(self):
|
|
53
|
+
return Banana.objects.values_list("id", "color", "size")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@register
|
|
57
|
+
class BananaSpreadsheetConfig(SpreadsheetConfig):
|
|
58
|
+
sheets_config = {
|
|
59
|
+
"Banana": BananaSheetConfig()
|
|
60
|
+
# the key will be used as the name of the sheet, the value is a class describing the sheet's configuration.
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class Meta:
|
|
64
|
+
verbose_name = "Bananas"
|
|
65
|
+
|
|
66
|
+
def update_database(self, sheets):
|
|
67
|
+
for sheet_name, rows in sheets.items():
|
|
68
|
+
for row in rows:
|
|
69
|
+
Banana.objects.get_or_create(
|
|
70
|
+
id=row["ID"],
|
|
71
|
+
color=row["COLOR"],
|
|
72
|
+
size=row["SIZE"],
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Then, you can go to `//localhost:8000/admin/spreadsheets/export/` to export a XSLX file and `//localhost:8000/admin/spreadsheets/import/` to import into your database.
|
|
77
|
+
|
|
78
|
+
# `SpreadsheetConfig` class
|
|
79
|
+
|
|
80
|
+
Inherit this class to describe your spreadsheets' configuration. Define a `sheets_config` dict attribute containing the name of your sheet as a key, and a `SheetConfig` class as a value.
|
|
81
|
+
|
|
82
|
+
## Validations
|
|
83
|
+
|
|
84
|
+
Several validations are run before importing a spreadsheet:
|
|
85
|
+
|
|
86
|
+
### Config version validation
|
|
87
|
+
|
|
88
|
+
Validate that the configuration version stored in the file is the same as the `SpreadsheetConfig.version` used.
|
|
89
|
+
|
|
90
|
+
### Sheets validation
|
|
91
|
+
|
|
92
|
+
Validate that all sheets in this `SpreadsheetConfig` class used, exist in imported file. Validate that all sheets in imported file, exist in this `SpreadsheetConfig` class used.
|
|
93
|
+
|
|
94
|
+
### Columns headers validation
|
|
95
|
+
|
|
96
|
+
Validate that the imported file contains the same columns headers as described in the `SheetConfig` used.
|
|
97
|
+
|
|
98
|
+
### Required data validation
|
|
99
|
+
|
|
100
|
+
Validate that there is a value in the imported file for each `required`, `required_unless`, `required_if` columns.
|
|
101
|
+
|
|
102
|
+
### Uniqueness validation [/!\ not implemented]
|
|
103
|
+
|
|
104
|
+
Validate that multiple cells in a row are unique together in the imported file.
|
|
105
|
+
|
|
106
|
+
### Types validation
|
|
107
|
+
|
|
108
|
+
Validate that each column in the imported file contains data of correct type (date, positive integer, ...).
|
|
109
|
+
|
|
110
|
+
### Data match validation
|
|
111
|
+
|
|
112
|
+
Validate that we find an object in the database matching the value in the imported file. If not, we ask the user to chose from a list of possibles values.
|
|
113
|
+
|
|
114
|
+
## Import data
|
|
115
|
+
|
|
116
|
+
If you want to import data in y our database from this spreadsheet, you need to define a `update_database(self, sheets)` method. This method is called after all data validations have been completed, and receive a python dict with an item for each sheet. The key is the name of the sheet, and the value is a python list of rows. Each row is a list containing a value for each column.
|
|
117
|
+
|
|
118
|
+
```py
|
|
119
|
+
def update_database(self, sheets):
|
|
120
|
+
for sheet_name, rows in sheets.items():
|
|
121
|
+
for row in rows:
|
|
122
|
+
for value in row:
|
|
123
|
+
print(value)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# `SheetConfig` class
|
|
128
|
+
|
|
129
|
+
Inherit this class to describe your sheets' configuration. Each attribute describes a column using a dictionary such as the following:
|
|
130
|
+
|
|
131
|
+
```py
|
|
132
|
+
<column_identifier> = {
|
|
133
|
+
"name": str (required), # The name of the column used in the header row
|
|
134
|
+
"required": bool, # Whether a value in the column is required when importing
|
|
135
|
+
"required_unless": str, # Whether a value in this column is required if the pointed column is empty
|
|
136
|
+
"required_if": str, # Whether a value in this column is required if the pointed column is filled
|
|
137
|
+
"comment": str, # A comment that appears in the header cell
|
|
138
|
+
"admin_only": bool, # Whether this column should be exported only when the current user is an administrator
|
|
139
|
+
"validation": { # A data validation applied to each cell of this column
|
|
140
|
+
"name": str, # A name used as header in the hidden config sheet
|
|
141
|
+
"type": str (required), # <url|date|list|range|whole...> See openpyxl documentation to get all available validation types
|
|
142
|
+
... # Same args as openpyxl's DataValidation objects
|
|
143
|
+
},
|
|
144
|
+
"header_style": str, # The name of a style that already exists in the SpreadsheetConfig to which this sheet belongs
|
|
145
|
+
"style": str, # The name of a style that already exists in the SpreadsheetConfig to which this sheet belongs
|
|
146
|
+
"number_format": str # A value among FORMAT_* constants from openpyxl.styles.numbers or others values supported by XLSX spec
|
|
147
|
+
},
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Note on validation
|
|
151
|
+
|
|
152
|
+
To create a _boolean_ validation, you may use a `list` validation type with `django_spreadsheets.constants.YES_NO_CHOICES` as `formula1` value. To ensure a boolean value is displayed in your exported file, add `"number_format": "BOOLEAN"` in your column configuration.
|
|
153
|
+
|
|
154
|
+
## Note on number format
|
|
155
|
+
|
|
156
|
+
When the value in your exported file is not displayed correctly or not consistent across rows, you may define the `number_format` key in your column configuration.
|
|
157
|
+
|
|
158
|
+
Openxpyxl's format constants are not exhaustive. You may use other supported strings (eg: "BOOLEAN").
|
|
159
|
+
|
|
160
|
+
## Export data
|
|
161
|
+
|
|
162
|
+
If you want to export data to this sheet, you need to define a `get_data_to_export(self)` method. This method must return a python list containing an item by row. Each item in the list is a list containing a value for each column.
|
|
163
|
+
|
|
164
|
+
```py
|
|
165
|
+
def get_data_to_export(self):
|
|
166
|
+
return [
|
|
167
|
+
[1, "yellow", 20],
|
|
168
|
+
[1, "green", 22],
|
|
169
|
+
]
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
# Settings
|
|
173
|
+
|
|
174
|
+
## `SPREADSHEET_CONFIG_SHEET_TITLE`
|
|
175
|
+
|
|
176
|
+
Default: `_("Configuration")`
|
|
177
|
+
|
|
178
|
+
The name of the (hidden) config sheet in the generated file. This special sheet is used to store data needed for validation (eg. lists of choices).
|
|
179
|
+
|
|
180
|
+
## `SPREADSHEET_MAX_VALIDATION_ROWS`
|
|
181
|
+
|
|
182
|
+
Default: `700`
|
|
183
|
+
|
|
184
|
+
When exporting data, the generated spreadsheet will have at least `SPREADSHEET_MAX_VALIDATION_ROWS` rows with data validation set up.
|
|
185
|
+
|
|
186
|
+
## `SPREADSHEET_MAX_STYLED_ROWS`
|
|
187
|
+
|
|
188
|
+
Default: `700`
|
|
189
|
+
|
|
190
|
+
When exporting data, the generated spreadsheet will have at least `SPREADSHEET_MAX_STYLED_ROWS` rows styled.
|
|
191
|
+
|
|
192
|
+
## `SPREADSHEET_COMMENT_AUTHOR`
|
|
193
|
+
|
|
194
|
+
Default: `"Django Spreadsheet"`
|
|
195
|
+
|
|
196
|
+
The name of the author used when adding comments on cells.
|
|
197
|
+
|
|
198
|
+
## `SPREADSHEET_EXPORT_FILE_NAME`
|
|
199
|
+
|
|
200
|
+
Default: `"export {config_name} {date}.xlsx"`
|
|
201
|
+
|
|
202
|
+
The name of the exported file when downloaded. Token `config_name` and `date` will be replaced respectively by the name of the configuration that is currently exported and the current date.
|
|
203
|
+
|
|
204
|
+
## `SPREADSHEET_TEMPLATE_FILE_NAME`
|
|
205
|
+
|
|
206
|
+
Default: `"{config_name} template file.xlsx"`
|
|
207
|
+
|
|
208
|
+
The name of the template file when downloaded. Token `config_name` will be replaced by the name of the configuration that is currently exported.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.1.0"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Django
|
|
2
|
+
from django.apps import AppConfig
|
|
3
|
+
from django.contrib.admin.apps import AdminConfig
|
|
4
|
+
from django.utils.translation import gettext_lazy as _
|
|
5
|
+
|
|
6
|
+
# Local application / specific library imports
|
|
7
|
+
from .autodiscover import autodiscover
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SpreadsheetAdminConfig(AdminConfig):
|
|
11
|
+
default_site = "django_spreadsheets.admin.SpreadsheetAdminSite"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SpreadsheetAppConfig(AppConfig):
|
|
15
|
+
name = "django_spreadsheets"
|
|
16
|
+
label = "django_spreadsheets"
|
|
17
|
+
verbose_name = _("Spreadsheet")
|
|
18
|
+
|
|
19
|
+
def ready(self) -> None:
|
|
20
|
+
autodiscover()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# myproject/autodiscover.py
|
|
2
|
+
# Standard Library
|
|
3
|
+
import importlib
|
|
4
|
+
|
|
5
|
+
# Django
|
|
6
|
+
from django.apps import apps
|
|
7
|
+
from django.utils.module_loading import module_has_submodule
|
|
8
|
+
|
|
9
|
+
SPREADSHEET_CONFIG_NAME = "spreadsheets"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def autodiscover():
|
|
13
|
+
discovered_classes = []
|
|
14
|
+
|
|
15
|
+
for app in apps.get_app_configs():
|
|
16
|
+
try:
|
|
17
|
+
importlib.import_module(f"{app.name}.{SPREADSHEET_CONFIG_NAME}")
|
|
18
|
+
except: # NOQA
|
|
19
|
+
# If something in spreadsheets.py raises an exception let that
|
|
20
|
+
# exception bubble up. Only catch the exception if
|
|
21
|
+
# spreadsheets.py doesn't exist
|
|
22
|
+
if module_has_submodule(app.module, SPREADSHEET_CONFIG_NAME):
|
|
23
|
+
raise
|
|
24
|
+
|
|
25
|
+
return discovered_classes
|