django-spreadsheets 1.1.0__py3-none-any.whl
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/__init__.py +1 -0
- django_spreadsheets/admin.py +6 -0
- django_spreadsheets/apps.py +20 -0
- django_spreadsheets/autodiscover.py +25 -0
- django_spreadsheets/base_config.py +219 -0
- django_spreadsheets/conf/__init__.py +0 -0
- django_spreadsheets/conf/settings.py +21 -0
- django_spreadsheets/constants.py +18 -0
- django_spreadsheets/errors.py +199 -0
- django_spreadsheets/exceptions.py +14 -0
- django_spreadsheets/exporter.py +171 -0
- django_spreadsheets/forms.py +93 -0
- django_spreadsheets/importer.py +537 -0
- django_spreadsheets/locale/fr/LC_MESSAGES/django.mo +0 -0
- django_spreadsheets/locale/fr/LC_MESSAGES/django.po +328 -0
- django_spreadsheets/registry.py +26 -0
- django_spreadsheets/shortcuts.py +69 -0
- django_spreadsheets/templates/spreadsheets/base.html +52 -0
- django_spreadsheets/templates/spreadsheets/done.html +10 -0
- django_spreadsheets/templates/spreadsheets/export_index.html +13 -0
- django_spreadsheets/templates/spreadsheets/import_index.html +13 -0
- django_spreadsheets/templates/spreadsheets/wizard.html +128 -0
- django_spreadsheets/urls.py +34 -0
- django_spreadsheets/validation.py +162 -0
- django_spreadsheets/views.py +272 -0
- django_spreadsheets-1.1.0.dist-info/LICENSE +12 -0
- django_spreadsheets-1.1.0.dist-info/METADATA +231 -0
- django_spreadsheets-1.1.0.dist-info/RECORD +29 -0
- django_spreadsheets-1.1.0.dist-info/WHEEL +4 -0
|
@@ -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
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Third party
|
|
2
|
+
from openpyxl.styles import Alignment, Font, NamedStyle, PatternFill
|
|
3
|
+
|
|
4
|
+
# Local application / specific library imports
|
|
5
|
+
from .conf import settings as local_settings
|
|
6
|
+
from .constants import DEFAULT_HEADER_STYLE_NAME
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class IndexedAttributesMeta(type):
|
|
10
|
+
def __new__(cls, name, bases, dct):
|
|
11
|
+
index = 0
|
|
12
|
+
for key, value in dct.items():
|
|
13
|
+
if isinstance(value, dict):
|
|
14
|
+
value["index"] = index
|
|
15
|
+
index += 1
|
|
16
|
+
return super().__new__(cls, name, bases, dct)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SheetConfig(metaclass=IndexedAttributesMeta):
|
|
20
|
+
"""An class describing all columns configurations for a sheet:
|
|
21
|
+
```
|
|
22
|
+
column_identifier = {
|
|
23
|
+
"name": "name of a column",
|
|
24
|
+
"required": bool,
|
|
25
|
+
"required_unless": str, wether the column is required if the pointed column is empty
|
|
26
|
+
"required_if": str, wether the column is required if the pointed column is filled
|
|
27
|
+
"comment": str, a comment that appear on the header
|
|
28
|
+
"admin_only": bool, only exported on admin mode
|
|
29
|
+
"validation": {
|
|
30
|
+
"type": <url|date|list|range|whole...>,
|
|
31
|
+
... see openpyxl validation
|
|
32
|
+
},
|
|
33
|
+
"header_style": "a named style"
|
|
34
|
+
"style": "a named style"
|
|
35
|
+
},
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Each attribute of the class is given a 0-based index.
|
|
39
|
+
Example:
|
|
40
|
+
````
|
|
41
|
+
>>> class GipsyKingsConfig(SheetConfig):
|
|
42
|
+
... djobi = {"name: "Djobi"}
|
|
43
|
+
... djoba = {"name": "Djoba"}
|
|
44
|
+
|
|
45
|
+
>>> GipsyKingsEnum.djoba["index"]
|
|
46
|
+
1
|
|
47
|
+
```
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __getitem__(self, key):
|
|
51
|
+
return getattr(self, key)
|
|
52
|
+
|
|
53
|
+
def items(self):
|
|
54
|
+
return (
|
|
55
|
+
(key, value)
|
|
56
|
+
for key, value in self.__class__.__dict__.items()
|
|
57
|
+
if not key.startswith("__") and not callable(value)
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def keys(self):
|
|
61
|
+
return [
|
|
62
|
+
key
|
|
63
|
+
for key, value in self.__class__.__dict__.items()
|
|
64
|
+
if not key.startswith("__") and not callable(value)
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
def values(self):
|
|
68
|
+
return [
|
|
69
|
+
value
|
|
70
|
+
for key, value in self.__class__.__dict__.items()
|
|
71
|
+
if not key.startswith("__") and not callable(value)
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
def get_data_to_export(self):
|
|
75
|
+
raise NotImplementedError
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class SpreadsheetConfig:
|
|
79
|
+
# A dict containing all sheets of the spreadsheet, with their names as key
|
|
80
|
+
sheets_config = {}
|
|
81
|
+
|
|
82
|
+
# Sheets that are ignored for non admin users
|
|
83
|
+
admin_only_sheets = []
|
|
84
|
+
|
|
85
|
+
# The name of the configuration sheet
|
|
86
|
+
config_sheet_title = local_settings.CONFIG_SHEET_TITLE
|
|
87
|
+
|
|
88
|
+
# Number of rows that are pre-configured with validation and formulae
|
|
89
|
+
max_validation_rows = local_settings.MAX_VALIDATION_ROWS
|
|
90
|
+
|
|
91
|
+
# Number of rows that are styled
|
|
92
|
+
max_styled_rows = local_settings.MAX_STYLED_ROWS
|
|
93
|
+
|
|
94
|
+
# List of named styles available on the whole workbook
|
|
95
|
+
named_styles = [
|
|
96
|
+
NamedStyle(
|
|
97
|
+
name=DEFAULT_HEADER_STYLE_NAME,
|
|
98
|
+
fill=PatternFill(
|
|
99
|
+
start_color="000000", end_color="000000", fill_type="solid"
|
|
100
|
+
),
|
|
101
|
+
font=Font(name="Calibri", color="FFFFFF", bold=True),
|
|
102
|
+
),
|
|
103
|
+
NamedStyle(
|
|
104
|
+
name="protected",
|
|
105
|
+
fill=PatternFill("solid", fgColor="DDDDDD"),
|
|
106
|
+
font=Font(name="Calibri"),
|
|
107
|
+
alignment=Alignment(vertical="top", wrap_text=True),
|
|
108
|
+
),
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
comment_author = local_settings.COMMENT_AUTHOR
|
|
112
|
+
|
|
113
|
+
version = 1
|
|
114
|
+
|
|
115
|
+
class Meta:
|
|
116
|
+
...
|
|
117
|
+
|
|
118
|
+
def __init__(self, include_admin_fields=False):
|
|
119
|
+
self._include_admin_fields = include_admin_fields
|
|
120
|
+
|
|
121
|
+
meta_options = self.Meta
|
|
122
|
+
self.verbose_name = getattr(
|
|
123
|
+
meta_options, "verbose_name", self.__class__.__name__
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
def __str__(self) -> str:
|
|
127
|
+
return self.verbose_name
|
|
128
|
+
|
|
129
|
+
def get_config_sheets_name(self):
|
|
130
|
+
"""Return a list of name of sheets valid for the current user"""
|
|
131
|
+
if self._include_admin_fields:
|
|
132
|
+
return self.sheets_config.keys()
|
|
133
|
+
return [
|
|
134
|
+
title
|
|
135
|
+
for title in self.sheets_config.keys()
|
|
136
|
+
if title not in self.admin_only_sheets
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
def excluded_columns_indexes(self, sheet):
|
|
140
|
+
"""Considering the _include_admin_fields attribute, return the indexes of the columns that should not be exported.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
sheet (Worksheet): The worksheet to consider
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
list: a list of indexes to exclude
|
|
147
|
+
"""
|
|
148
|
+
columns_config = self.sheets_config.get(sheet.title, {})
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
[
|
|
152
|
+
index
|
|
153
|
+
for index, (key, config) in enumerate(columns_config.items())
|
|
154
|
+
if config.get("admin_only", False)
|
|
155
|
+
]
|
|
156
|
+
if not self._include_admin_fields
|
|
157
|
+
else []
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
def get_column_header(self, sheet, index):
|
|
161
|
+
excluded_columns_indexes = self.excluded_columns_indexes(sheet)
|
|
162
|
+
sheet_columns_names = [
|
|
163
|
+
column_header
|
|
164
|
+
for index, column_header in enumerate(
|
|
165
|
+
self.sheets_config.get(sheet.title, {}).keys()
|
|
166
|
+
)
|
|
167
|
+
if index not in excluded_columns_indexes
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
try:
|
|
171
|
+
return sheet_columns_names[index - 1]
|
|
172
|
+
except IndexError:
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
def get_column_header_with_suffix(self, column_header, column_config):
|
|
176
|
+
"""A "*" is appended to required columns' header"""
|
|
177
|
+
if column_config.get("required", False) or column_config.get(
|
|
178
|
+
"required_unless", False
|
|
179
|
+
):
|
|
180
|
+
column_header = f"{column_header}*"
|
|
181
|
+
|
|
182
|
+
return column_header
|
|
183
|
+
|
|
184
|
+
def get_sheet_columns_configs(self, sheet):
|
|
185
|
+
columns_configs = self.sheets_config.get(sheet.title, {})
|
|
186
|
+
|
|
187
|
+
if not self._include_admin_fields:
|
|
188
|
+
excluded_columns_indexes = self.excluded_columns_indexes(sheet)
|
|
189
|
+
columns_configs = {
|
|
190
|
+
key: value
|
|
191
|
+
for index, (key, value) in enumerate(columns_configs.items())
|
|
192
|
+
if index not in excluded_columns_indexes
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return columns_configs
|
|
196
|
+
|
|
197
|
+
def get_sheet_columns_configs_list(self, sheet):
|
|
198
|
+
columns_configs = self.get_sheet_columns_configs(sheet)
|
|
199
|
+
columns_configs = list(columns_configs.items())
|
|
200
|
+
return columns_configs
|
|
201
|
+
|
|
202
|
+
def get_validation_config(self, sheet):
|
|
203
|
+
validation_config = {}
|
|
204
|
+
for key, config in self.sheets_config[sheet.title].items():
|
|
205
|
+
if "validation" in config:
|
|
206
|
+
validation_config[key] = config["validation"]
|
|
207
|
+
|
|
208
|
+
return validation_config
|
|
209
|
+
|
|
210
|
+
def init_named_styles(self):
|
|
211
|
+
for named_style in self.named_styles:
|
|
212
|
+
try:
|
|
213
|
+
self.workbook.add_named_style(named_style)
|
|
214
|
+
except ValueError:
|
|
215
|
+
# If the named style is already registered, do nothing
|
|
216
|
+
...
|
|
217
|
+
|
|
218
|
+
def update_database(self, sheets):
|
|
219
|
+
raise NotImplementedError
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Django
|
|
2
|
+
from django.conf import settings
|
|
3
|
+
from django.utils.translation import gettext as _
|
|
4
|
+
|
|
5
|
+
CONFIG_SHEET_TITLE = getattr(
|
|
6
|
+
settings, "SPREADSHEETS_CONFIG_SHEET_TITLE", _("Configuration")
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
MAX_VALIDATION_ROWS = getattr(settings, "SPREADSHEETS_MAX_VALIDATION_ROWS", 700)
|
|
10
|
+
|
|
11
|
+
MAX_STYLED_ROWS = getattr(settings, "SPREADSHEETS_MAX_STYLED_ROWS", 700)
|
|
12
|
+
|
|
13
|
+
COMMENT_AUTHOR = getattr(settings, "SPREADSHEETS_COMMENT_AUTHOR", "Django Spreadsheet")
|
|
14
|
+
|
|
15
|
+
EXPORT_FILE_NAME = getattr(
|
|
16
|
+
settings, "SPREADSHEETS_EXPORT_FILE_NAME", _("export {config_name} {date}.xlsx")
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
TEMPLATE_FILE_NAME = getattr(
|
|
20
|
+
settings, "SPREADSHEETS_TEMPLATE_FILE_NAME", _("{config_name} template file.xlsx")
|
|
21
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Django
|
|
2
|
+
from django.utils.translation import gettext as _
|
|
3
|
+
|
|
4
|
+
XLSX_EMPTY_VALUES = (None, "", 0, "None")
|
|
5
|
+
|
|
6
|
+
# Bools conversion
|
|
7
|
+
TRUE_BOOL_XLSX_VALUE = _("Yes")
|
|
8
|
+
FALSE_BOOL_XLSX_VALUE = _("No")
|
|
9
|
+
|
|
10
|
+
YES_NO_CHOICES = [TRUE_BOOL_XLSX_VALUE, FALSE_BOOL_XLSX_VALUE]
|
|
11
|
+
|
|
12
|
+
BOOL_XLSX_MAPPING = {True: TRUE_BOOL_XLSX_VALUE, False: FALSE_BOOL_XLSX_VALUE}
|
|
13
|
+
|
|
14
|
+
XLSX_BOOL_MAPPING = {v: k for k, v in BOOL_XLSX_MAPPING.items()}
|
|
15
|
+
|
|
16
|
+
VERSION_CUSTOM_PROPERTY_NAME = "DjangoSpreadsheetConfigVersion"
|
|
17
|
+
|
|
18
|
+
DEFAULT_HEADER_STYLE_NAME = "DefaultHeaderStyle"
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Django
|
|
2
|
+
from django.utils.translation import gettext as _
|
|
3
|
+
|
|
4
|
+
# Third party
|
|
5
|
+
from openpyxl.utils.cell import get_column_letter
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ImporterError:
|
|
9
|
+
order = 0
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ImporterWarning:
|
|
13
|
+
order = 0
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ImporterSheetMixin:
|
|
17
|
+
def __init__(self, sheet_name, *args, **kwargs):
|
|
18
|
+
super().__init__(*args, **kwargs)
|
|
19
|
+
self.sheet_name = sheet_name
|
|
20
|
+
|
|
21
|
+
def __str__(self):
|
|
22
|
+
return _(f'Sheet "{self.sheet_name}"')
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ImporterCellMixin(ImporterSheetMixin):
|
|
26
|
+
def __init__(self, column, row, *args, **kwargs):
|
|
27
|
+
super().__init__(*args, **kwargs)
|
|
28
|
+
self.column = column
|
|
29
|
+
self.row = row
|
|
30
|
+
self.order = column
|
|
31
|
+
|
|
32
|
+
def __str__(self):
|
|
33
|
+
message = super().__str__()
|
|
34
|
+
return _(f"{message}, cell {get_column_letter(self.column)}{self.row}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ImporterSheetMissing(ImporterSheetMixin, ImporterError):
|
|
38
|
+
@property
|
|
39
|
+
def title(self):
|
|
40
|
+
return _(f'The sheet "{self.sheet_name}" is missing')
|
|
41
|
+
|
|
42
|
+
def __str__(self):
|
|
43
|
+
return _(
|
|
44
|
+
f'The sheet "{self.sheet_name}" is missing from the file you supplied. Check that the sheet has not been deleted or renamed.'
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ImporterSheetIgnored(ImporterSheetMixin, ImporterError):
|
|
49
|
+
def __init__(self, sheet_name, config_sheets_names, *args, **kwargs):
|
|
50
|
+
super().__init__(sheet_name, *args, **kwargs)
|
|
51
|
+
self.config_sheets_names = config_sheets_names
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def title(self):
|
|
55
|
+
return _(f'The sheet "{self.sheet_name}" will be ignored when importing')
|
|
56
|
+
|
|
57
|
+
def __str__(self):
|
|
58
|
+
return _(
|
|
59
|
+
f'The sheet "{self.sheet_name}" in the file you have supplied does not correspond to a sheet in the template ({", ".join(self.config_sheets_names)}). It will be ignored.'
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class ImporterColumnHeaderError(ImporterCellMixin, ImporterError):
|
|
64
|
+
def __init__(self, column_header, expected_column_header, *args, **kwargs):
|
|
65
|
+
super().__init__(*args, **kwargs)
|
|
66
|
+
self.column_header = column_header
|
|
67
|
+
self.expected_column_header = expected_column_header
|
|
68
|
+
self.order = -1
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def title(self):
|
|
72
|
+
return _("The column name does not match the model")
|
|
73
|
+
|
|
74
|
+
def __str__(self):
|
|
75
|
+
message = super().__str__()
|
|
76
|
+
return _(
|
|
77
|
+
f'{message} : the column name is "{self.column_header}" when it should be "{self.expected_column_header}"'
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ImporterTypeError(ImporterCellMixin, ImporterError):
|
|
82
|
+
def __init__(self, column_header, expected_type, error_message, *args, **kwargs):
|
|
83
|
+
super().__init__(*args, **kwargs)
|
|
84
|
+
self.column_header = column_header
|
|
85
|
+
self.error_message = error_message
|
|
86
|
+
self.expected_type = expected_type
|
|
87
|
+
|
|
88
|
+
def __str__(self):
|
|
89
|
+
message = super().__str__()
|
|
90
|
+
return _(
|
|
91
|
+
f'{message}, column "{self.column_header}", type is invalid. {self.error_message}'
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def title(self):
|
|
96
|
+
return _(
|
|
97
|
+
f'Column "{self.column_header}" must be of the type: {self.expected_type}'
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class ImporterRequiredDataError(ImporterCellMixin, ImporterError):
|
|
102
|
+
def __init__(self, column_header, *args, **kwargs):
|
|
103
|
+
super().__init__(*args, **kwargs)
|
|
104
|
+
self.column_header = column_header
|
|
105
|
+
|
|
106
|
+
def __str__(self):
|
|
107
|
+
message = super().__str__()
|
|
108
|
+
return _(f"{message} : cell must no be empty")
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def title(self):
|
|
112
|
+
return _(f'Column "{self.column_header}" is required')
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class ImporterRequiredUnlessDataError(ImporterRequiredDataError):
|
|
116
|
+
def __init__(self, unless_column_header, *args, **kwargs):
|
|
117
|
+
super().__init__(*args, **kwargs)
|
|
118
|
+
self.unless_column_header = unless_column_header
|
|
119
|
+
|
|
120
|
+
def __str__(self):
|
|
121
|
+
message = super(
|
|
122
|
+
ImporterRequiredDataError, self
|
|
123
|
+
).__str__() # Skip message from direct parent
|
|
124
|
+
return _(
|
|
125
|
+
f'{message} at least one of the columns "{self.column_header}" or "{self.unless_column_header}" must be completed'
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
@property
|
|
129
|
+
def title(self):
|
|
130
|
+
return _(
|
|
131
|
+
f'At least one of the columns "{self.column_header}" or "{self.unless_column_header}" must be completed'
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class ImporterRequiredIfDataError(ImporterRequiredDataError):
|
|
136
|
+
def __init__(self, required_if_column_header, *args, **kwargs):
|
|
137
|
+
super().__init__(*args, **kwargs)
|
|
138
|
+
self.required_if_column_header = required_if_column_header
|
|
139
|
+
|
|
140
|
+
def __str__(self):
|
|
141
|
+
message = super(
|
|
142
|
+
ImporterRequiredDataError, self
|
|
143
|
+
).__str__() # Skip message from direct parent
|
|
144
|
+
return _(
|
|
145
|
+
f'{message} column "{self.column_header}" must be completed if column "{self.required_if_column_header}" is completed'
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def title(self):
|
|
150
|
+
return _(
|
|
151
|
+
f'Columns "{self.column_header}" and "{self.required_if_column_header}" must be completed'
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class ImporterUniquenessError(ImporterSheetMixin, ImporterError):
|
|
156
|
+
def __init__(self, line_number, is_duplicate_of, *args, **kwargs):
|
|
157
|
+
super().__init__(*args, **kwargs)
|
|
158
|
+
self.line_number = line_number
|
|
159
|
+
self.is_duplicate_of = is_duplicate_of
|
|
160
|
+
|
|
161
|
+
def __str__(self):
|
|
162
|
+
return _(
|
|
163
|
+
f"Line {self.line_number} is a duplicate of line {self.is_duplicate_of}"
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def title(self):
|
|
168
|
+
return _("Duplicated lines")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class ImporterObjectRemovalWarning(ImporterSheetMixin, ImporterWarning):
|
|
172
|
+
def __init__(self, composition, *args, **kwargs):
|
|
173
|
+
super().__init__(*args, **kwargs)
|
|
174
|
+
self.object = object
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def title(self):
|
|
178
|
+
return _(f'The object "{self.object}" will be deleted during import')
|
|
179
|
+
|
|
180
|
+
def __str__(self):
|
|
181
|
+
return _(
|
|
182
|
+
f'The object "{self.object}" will be deleted during import because it does not exist in the imported file.'
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class ConfigVersionWarning(ImporterWarning):
|
|
187
|
+
def __init__(self, expected, found, *args, **kwargs):
|
|
188
|
+
super().__init__(*args, **kwargs)
|
|
189
|
+
self.expected = expected
|
|
190
|
+
self.found = found if found is not None else _("unknown")
|
|
191
|
+
|
|
192
|
+
@property
|
|
193
|
+
def title(self):
|
|
194
|
+
return _("The configuration version of the file is incorrect")
|
|
195
|
+
|
|
196
|
+
def __str__(self):
|
|
197
|
+
return _(
|
|
198
|
+
f"The configuration version of the file ({self.found}) is different from that expected ({self.expected})"
|
|
199
|
+
)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Django
|
|
2
|
+
from django.utils.translation import gettext_lazy as _
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ImportValueNotFoundException(Exception):
|
|
6
|
+
"""Raised when a value is invalid for a cell."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, model_name, field_name, value):
|
|
9
|
+
super().__init__(
|
|
10
|
+
_(f'`{model_name}.{field_name}` received an unknown value: "{value}".')
|
|
11
|
+
)
|
|
12
|
+
self.model_name = model_name
|
|
13
|
+
self.field_name = field_name
|
|
14
|
+
self.value = value
|