aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/indicator/forms.py
blob: 2e0b7090a742aa3e03a9508102941488f1f226be (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# -*- coding: utf-8 -*-

"""
forms for apps/indicator
"""

from django import forms
from django.utils.translation import ugettext as _

from indicator import models as im

import sympy
from sympy.core.sympify import SympifyError


class IndicatorCategoryForm(forms.ModelForm):               # {{{
    """
    form for 'models.IndicatorCategory'
    """
    class Meta:
        model = im.IndicatorCategory
        exclude = ('addByUser',)
# }}}


class IndicatorForm(forms.ModelForm):                       # {{{
    """
    form for 'models.Indicator'
    """
    class Meta:
        model = im.Indicator
        exclude = ('addByUser',)
# }}}


class UnitForm(forms.ModelForm):                            # {{{
    """
    form for 'models.Unit'
    """
    class Meta:
        model = im.Unit
        exclude = ('addByUser',)

    def __init__(self, *args, **kwargs):
        super(UnitForm, self).__init__(*args, **kwargs)
        # store 'instance_id', for edting instance
        self.instance_id = self.instance.id

    # 'clean_standard()' cannot raise Vali dationError correctly??
    # TODO: clean each field and generate errors accordingly.

    def clean(self):
        cleaned_data = super(UnitForm, self).clean()
        instance_id = self.instance_id
        standard = cleaned_data['standard']
        indicator = cleaned_data['indicator']
        std_unit_list = indicator.get_unit(type="standard")
        relation = cleaned_data.get('relation', u'')
        if standard:
            if std_unit_list and (instance_id != std_unit_list[0].id):
                raise forms.ValidationError(_(u'标准单位已存在'),
                        code='standard')
            cleaned_data['relation'] = u'v'
        else:
            try:
                fsym = sympy.sympify(relation)
            except SympifyError:
                raise forms.ValidationError(_(u'"%(relation)s" 不是合法的表达式'),
                    code='relation_invalid',
                    params={'relation': relation})
        # always return the full collection of cleaned data
        return cleaned_data
# }}}


class InnateConfineForm(forms.ModelForm):                   # {{{
    """
    form for 'models.InnateConfine'
    """
    unit = forms.ModelChoiceField(label=u"标准单位",
            queryset=im.Unit.objects.filter(standard=True))

    class Meta:
        model = im.InnateConfine
        exclude = ('addByUser',)

    def clean(self):                                        # {{{
        """
        check the validity of data
        """
        cleaned_data = super(InnateConfineForm, self).clean()
        indicator = cleaned_data['indicator']
        unit = cleaned_data.get('unit')
        val_norm = cleaned_data.get('val_norm')
        human_max = cleaned_data.get('human_max')
        human_min = cleaned_data.get('human_min')
        math_max = cleaned_data.get('math_max')
        math_min = cleaned_data.get('math_min')
        # check data
        if indicator.dataType in [indicator.FLOAT_TYPE,
                indicator.RANGE_TYPE, indicator.FLOAT_RANGE_TYPE]:
            # check unit
            if not (unit and unit.standard):
                raise forms.ValidationError(_(u'unit 未填写/不是标准单位'),
                        code='unit')
            if (human_max is None) or (human_min is None):
                raise forms.ValidationError(_(u'human_max/human_min 未填写'),
                        code='human_empty')
            if (human_max <= human_min):
                raise forms.ValidationError(_(u'human_max <= human_min'),
                        code='human_relation')
            # check 'math_max' and 'math_min'
            if (math_max is None) or (math_min is None):
                raise forms.ValidationError(_(u'math_max/math_min 未填写'),
                        code='math_empty')
            if (math_max <= math_min):
                raise forms.ValidationError(_(u'math_max <= math_min'),
                        code='math_relation')
            # compare 'human*' and 'math*'
            if (human_max > math_max) or (human_min < math_min):
                raise forms.ValidationError(_(u'Error: human_max>math_max / human_min<math_min'),
                        code='human_math_relation')
            # check finished
        elif indicator.dataType == indicator.INTEGER_TYPE:
            # 整数型
            try:
                val_norm = int(val_norm)
            except ValueError:
                raise ValidationError(_(u'val_norm="%(val_norm)s" 不是整数型值'),
                        code='val_norm_int',
                        params={'val_norm': val_norm})
        elif indicator.dataType == indicator.PM_TYPE:
            # 阴阳(+/-)型
            if (len(val_norm) == 1) and (val_norm in [u'+', u'-']):
                pass
            else:
                raise forms.ValidationError(_(u'val_norm 只接受 "+"/"-"'),
                        code='val_norm_pm')
        ## TODO: RADIO_TYPE, CHECKBOX_TYPE
        elif indicator.dataType in [indicator.RADIO_TYPE,
                indicator.CHECKBOX_TYPE]:
            raise forms.ValidationError(_(u'RADIO_TYPE, CHECKBOX_TYPE 验证未实现'),
                    code='radio_checkbox')
        else:
            raise forms.ValidationError(_(u'数据不符合要求'),
                    code='data_type_invalid')
        # all checks finished
        return cleaned_data
    # }}}
# }}}


class IndicatorRecordForm(forms.ModelForm):                 # {{{
    """
    form for 'models.IndicatorRecord'
    """

    class Meta:
        model = im.IndicatorRecord
        exclude = ('user',)

    def clean(self):                                        # {{{
        cleaned_data = super(IndicatorRecordForm, self).clean()
        # get data
        indicator = cleaned_data['indicator']
        unit = cleaned_data.get('unit')
        _value = cleaned_data.get('value')
        _val_max = cleaned_data.get('val_max')
        _val_min = cleaned_data.get('val_min')
        # check data                                        # {{{
        if indicator.dataType == indicator.INTEGER_TYPE:
            # integer
            try:
                value = int(_value)
            except ValueError:
                raise forms.ValidationError(_(u'value 不是整数类型'),
                        code='value_integer')
        elif indicator.dataType == indicator.FLOAT_TYPE:
            # float
            if not unit:
                raise forms.ValidationError(_(u'unit 未填写'),
                        code='unit_empty')
            try:
                value = float(_value)
            except ValueError:
                raise forms.ValidationError(_(u'value 不是浮点数类型'),
                        code='value_float')
        elif indicator.dataType == indicator.RANGE_TYPE:
            # range
            val_max = _val_max
            val_min = _val_min
            if not unit:
                raise forms.ValidationError(_(u'unit 未填写'),
                        code='unit_empty')
            if (val_max is None) or (val_min is None):
                raise forms.ValidationError(_(u'val_max/val_min 未填写'),
                        code='val_empty')
            if (val_max <= val_min):
                raise forms.ValidationError(_(u'val_max <= val_min'),
                        code='val_relation')
        elif indicator.dataType == indicator.FLOAT_RANGE_TYPE:
            # float/range
            if not unit:
                raise forms.ValidationError(_(u'unit 未填写'),
                        code='unit_empty')
            if value:
                # float (first)
                try:
                    value = float(_value)
                except ValueError:
                    raise forms.ValidationError(_(u'value 不是浮点数类型'),
                            code='value_float')
            elif (val_max is not None) or (val_min is not None):
                # range
                val_max = _val_max
                val_min = _val_min
                if (val_max <= val_min):
                    raise forms.ValidationError(_(u'val_max <= val_min'),
                            code='val_relation')
            else:
                raise forms.ValidationError(_(u'请填写 value 或者 "val_max + val_min"'),
                        code='value_val')
        elif indicator.dataType == indicator.PM_TYPE:
            # +/-
            value = _value
            if (len(value) == 1) and (value in [u'+', u'-']):
                pass
            else:
                raise forms.ValidationError(_(u'value 只接受 "+"/"-"'),
                        code='value_pm')
        elif indicator.dataType in [indicator.RADIO_TYPE,
                indicator.CHECKBOX_TYPE]:
            ## TODO: RADIO_TYPE, CHECKBOX_TYPE
            raise forms.ValidationError(_(u'RADIO_TYPE, CHECKBOX_TYPE 验证未实现'),
                    code='radio_checkbox')
        else:
            raise forms.ValidationError(_(u'数据不符合要求'),
                    code='data_type_invalid')
        # }}}
        # check confine                                     # {{{
        # for [FLOAT_TYPE, RANGE_TYPE, FLOAT_RANGE_TYPE]
        # [INTEGER_TYPE, PM_TYPE] already validated above
        if indicator.dataType in [indicator.FLOAT_TYPE,
                indicator.RANGE_TYPE, indicator.FLOAT_RANGE_TYPE]:
            # check confine if specified for the indicator
            if not indicator.check_confine():
                raise forms.ValidationError(_(u'该指标未指定 InnateConfine'),
                        code='innateconfine')
            # innateconfine ok
            confine = indicator.innate_confine
            human_max = confine.human_max
            human_min = confine.human_min
            math_max = confine.math_max
            math_min = confine.math_min
            # unit conversion
            unit_rel = unit.relation
            v = sympy.symbols('v')
            rel_sym = sympy.sympify(unit_rel)
            # data
            value = _value
            val_max = _val_max
            val_min = _val_min
            # value
            if value:
                try:
                    value = float(value)
                except ValueError:
                    raise forms.ValidationError(_(u'value 不是浮点数类型'),
                            code='value_float')
                # 'value' unit conversion
                try:
                    value_std = float(rel_sym.evalf(subs={v: value}))
                except ValueError:
                    raise forms.ValidationError(_(u'"%s" 求值错误,请检查只有变量"v"' % unit_rel),
                            code='value_evalf')
                if (value_std < math_min) or (value_std > math_max):
                    raise forms.ValidationError(_(u'value(std) < math_min or value(std) > math_max'),
                            code='value_std_relation')
            # val_max
            if val_max is not None:
                # unit conversion
                try:
                    val_max_std = float(rel_sym.evalf(
                        subs={v: val_max}))
                except ValueError:
                    raise forms.ValidationError(_(u'"%s" 求值错误,请检查只有变量"v"' % unit_rel),
                            code='val_max_evalf')
                if (val_max_std <= math_min) or (
                        val_max_std > math_max):
                    raise forms.ValidationError(_(u'val_max(std) <= math_min or val_max(std) > math_max'),
                            code='val_max_std_relation')
            # val_min
            if val_min is not None:
                try:
                    val_min_std = float(rel_sym.evalf(
                        subs={v: val_min}))
                except ValueError:
                    raise forms.ValidationError(_(u'"%s" 求值错误,请检查只有变量"v"' % unit_rel),
                            code='val_min_evalf')
                if (val_min_std < math_min) or (
                        val_min_std >= math_max):
                    raise forms.ValidationError(_(u'val_min(std) < math_min or val_min(std) >= math_max'),
                            code='val_min_std_relation')
        # }}}
        # return cleaned data
        return cleaned_data
    # }}}
# }}}


class RecordHistoryForm(forms.ModelForm):                   # {{{
    """
    form for 'models.RecordHistory'
    """
    class Meta:
        model = im.RecordHistory
        exclude = ('indicatorRecord',)
# }}}


# vim: set ts=4 sw=4 tw=0 fenc=utf-8 ft=python.django: #