Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
from __future__ import division, unicode_literals
__copyright__ = "Copyright (C) 2018 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import six
from django.core.checks import Warning
from django.utils.functional import cached_property
from django.utils.module_loading import import_string
RELATE_USER_FULL_NAME_FORMAT_METHOD = "RELATE_USER_FULL_NAME_FORMAT_METHOD"
class RelateUserMethodSettingsInitializer(object):
"""
This is used to check (validate) settings.RELATE_CSV_SETTINGS (optional)
and initialize the settings for csv export for csv-related forms.
"""
def __init__(self):
self._custom_full_name_method = None
@cached_property
def get_custom_full_name_method(self):
self.check_custom_full_name_method()
return self._custom_full_name_method
def check_custom_full_name_method(self):
errors = []
from django.conf import settings
relate_user_full_name_format_method = getattr(
settings, RELATE_USER_FULL_NAME_FORMAT_METHOD, None)
self._custom_full_name_method = None
if relate_user_full_name_format_method is not None:
if isinstance(relate_user_full_name_format_method, six.string_types):
try:
relate_user_full_name_format_method = (
import_string(relate_user_full_name_format_method))
except ImportError:
errors = [Warning(
msg=(
"%(location)s: `%(method)s` failed to be imported, "
"default format method will be used."
% {"location": RELATE_USER_FULL_NAME_FORMAT_METHOD,
"method": relate_user_full_name_format_method
}
),
id="relate_user_full_name_format_method.W001"
)]
return errors
self._custom_full_name_method = relate_user_full_name_format_method
if not callable(relate_user_full_name_format_method):
errors.append(Warning(
msg=(
"%(location)s: `%(method)s` is not a callable, "
"default format method will be used."
% {"location": RELATE_USER_FULL_NAME_FORMAT_METHOD,
"method": relate_user_full_name_format_method
}
),
id="relate_user_full_name_format_method.W002"
))
else:
try:
returned_name = (
relate_user_full_name_format_method("first_name",
"last_name"))
except Exception as e:
from traceback import format_exc
errors.append(Warning(
msg=(
"%(location)s: `%(method)s` called with '"
"args 'first_name', 'last_name' failed with"
"exception below:\n"
"%(err_type)s: %(err_str)s\n"
"%(format_exc)s\n\n"
"Default format method will be used."
% {"location": RELATE_USER_FULL_NAME_FORMAT_METHOD,
"method": relate_user_full_name_format_method,
"err_type": type(e).__name__,
"err_str": str(e),
'format_exc': format_exc()}
),
id="relate_user_full_name_format_method.W003"
))
else:
unexpected_return_value = ""
if returned_name is None:
unexpected_return_value = "None"
if not isinstance(returned_name, six.string_types):
unexpected_return_value = type(returned_name).__name__
elif not returned_name.strip():
unexpected_return_value = "empty string %s" % returned_name
if unexpected_return_value:
errors.append(Warning(
msg=("%(location)s: `%(method)s` is expected to "
"return a non-empty string, got `%(result)s`, "
"default format method will be used."
% {
"location": RELATE_USER_FULL_NAME_FORMAT_METHOD,
"method": relate_user_full_name_format_method,
"result": unexpected_return_value,
}),
id="relate_user_full_name_format_method.W004"
))
else:
returned_name2 = (
relate_user_full_name_format_method("first_name2",
"last_name2"))
if returned_name == returned_name2:
errors.append(Warning(
msg=("%(location)s: `%(method)s` is expected to "
"return different value with different "
"input, default format method will be used."
% {
"location":
RELATE_USER_FULL_NAME_FORMAT_METHOD,
"method":
relate_user_full_name_format_method
}),
id="relate_user_full_name_format_method.W005"
))
if errors:
self._custom_full_name_method = None
return errors
relate_user_method_settings = RelateUserMethodSettingsInitializer()