aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/indicator/models.py
blob: bd57d87a83e9820afab41c23faf8ecc22d807c28 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# -*- coding: utf-8 -*-
#
# Weitian Li <liweitianux@foxmail.com>
# updated: 2013/08/12
#

"""
apps/indicator models
"""

from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
# '@permalink' is no longer recommended
from django.core.urlresolvers import reverse

import re
import datetime

import sympy
from sympy.core.sympify import SympifyError

from utils.xpinyin import Pinyin


class IndicatorCategory(models.Model):                      # {{{
    """
    对 Indicator 进行分类,用于前端按分类显示和选择指标。
    """
    name = models.CharField(u"指标类别名称", max_length=100)
    pinyin = models.CharField(u"拼音", max_length=200,
            editable=False, blank=True)
    englishName = models.CharField(u"Indicator Category Name",
            max_length=200, blank=True)
    description = models.TextField(u"指标类别描述", blank=True)
    # 记录添加的用户,用户只能修改自己添加的对象
    addByUser = models.ForeignKey(User, verbose_name=u"添加的用户",
            related_name="indicator_categories")

    class Meta:
        verbose_name_plural = u"指标类别"
        ordering = ['pinyin', 'id']

    def __unicode__(self):
        return u"< IndicatorCategory: #%s, %s addBy %s >"\
                % (self.id, self.name, self.addByUser.username)

    def show(self):
        """
        used in 'search/search.html'
        to show search result
        """
        return self.__unicode__()

    def get_absolute_url(self):
        # need define url with name='show-category', 'pk' as parameter
        return reverse('show-category',
                kwargs={'pk': self.id})

    # auto generate `pinyin'
    def save(self, **kwargs):
        p = Pinyin()
        self.pinyin = p.get_pinyin(self.name)
        super(IndicatorCategory, self).save(**kwargs)

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'name': self.name,
            'pinyin': self.pinyin,
            'englishName': self.englishName,
            'description': self.description,
            'addByUser_id': self.addByUser.id,
        }
        return dump_data
# }}}


class Indicator(models.Model):                              # {{{
    """
    指标模型
    """
    name = models.CharField(u"指标名称", max_length=100)
    pinyin = models.CharField(u"拼音", max_length=200,
            editable=False, blank=True)
    englishName = models.CharField(u"Indicator Name",
            max_length=200, blank=True)
    description = models.TextField(u"指标描述", blank=True)
    # Indicator 接受数据类型/格式等说明/示例
    helpText = models.CharField(u"帮助", max_length=300, blank=True)
    # 记录添加指标的用户,用户只能修改自己添加的指标
    addByUser = models.ForeignKey(User, verbose_name=u"添加的用户",
            related_name="indicators")
    # Category
    categories = models.ManyToManyField(IndicatorCategory,
            verbose_name=u"所属类别", related_name="indicators")
    # DATA_TYPES for indicator
    INTEGER_TYPE = u'IN'        # 整数型
    FLOAT_TYPE = u'FL'          # 浮点型
    RANGE_TYPE = u'RG'          # 范围型(eg. 250-500)
    FLOAT_RANGE_TYPE = u'FR'    # 浮点型/范围型,接受定值或范围
    PM_TYPE = u'PM'             # +/- 型
    RADIO_TYPE = u'RD'          # 单选型
    CHECKBOX_TYPE = u'CB'       # 多选多
    DATA_TYPES = (
        (INTEGER_TYPE, u"整数型"),
        (FLOAT_TYPE, u"浮点定值型"),
        (RANGE_TYPE, u"浮点范围型"),
        (FLOAT_RANGE_TYPE, u"定值或范围型"),
        (PM_TYPE, u"阴阳型(+/-)"),
        #(RADIO_TYPE, u"单选型"),
        #(CHECKBOX_TYPE, u"多选型"),
    )
    dataType = models.CharField(u"数据类型", max_length=2,
            choices=DATA_TYPES)

    class Meta:
        verbose_name_plural = u"医学指标"
        ordering = ['pinyin', 'id']

    def __unicode__(self):
        return u"< Indicator: #%s, %s, dataType %s addBy %s >"\
                % (self.id, self.name, self.dataType,
                self.addByUser.username)

    def show(self):
        """
        used in 'search/search.html'
        to show search result
        """
        return self.__unicode__()

    def get_absolute_url(self):
        return reverse('show-indicator',
                kwargs={'pk': self.id})

    # auto generate `pinyin'
    def save(self, **kwargs):
        p = Pinyin()
        self.pinyin = p.get_pinyin(self.name)
        super(Indicator, self).save(**kwargs)

    def check_unit(self, **kwargs):
        """
        Check if the validity of the units specified for the indicator.
        A indicator must have one 'standard unit'.
        if indicator.dataType in [INTEGER_TYPE, PM_TYPE],
        then units are not needed.
        """
        if self.dataType in [self.FLOAT_TYPE, self.RANGE_TYPE,
                self.FLOAT_RANGE_TYPE]:
            std_unit = self.units.filter(standard=True)
            if std_unit:
                return True
            else:
                print u"Indicator id=%s 未指定标准单位" % self.id
                return False
        else:
            print u"dataType=%s 不需要单位" % self.dataType
            return True

    def _get_unit(self, type="standard"):
        if type == "standard":
            _units = self.units.filter(standard=True)
        elif type == "other":
            _units = self.units.filter(standard=False)
        else:
            _units = []
        return list(_units)

    def get_unit(self, type="standard"):
        """
        return a 'list' which contains the 'Unit's
        related to the indicator
            get_unit(type):
                type = standard(default), other, all
        this return the 'standard unit' by default
        if 'type="all"', the 'standard unit' comes first
        """
        if type == "all":
            # get all units
            # standard unit first
            _units = self._get_unit(type="standard")\
                    + self._get_unit(type="other")
            return _units
        else:
            return self._get_unit(type)

    def check_confine(self):
        """
        check the existence of the related InnateConfine
        """
        try:
            c = self.innate_confine
            return True
        except InnateConfine.DoesNotExist:
            print u'Indicator id=%s 未指定 InnateConfine' % self.id
            raise ValueError(u'Indicator id=%s 未指定 InnateConfine'
                    % self.id)
            return False

    def get_confine(self):
        """
        dump the confine data from the related InnateConfine
        """
        try:
            c = self.innate_confine
            return c.dump()
        except InnateConfine.DoesNotExist:
            print u'Indicator id=%s 未指定 InnateConfine' % self.id
            return {}

    def is_ready(self):
        """
        check the status of this indicator,
        if 'Unit's and 'InnateConfine' are correctly specified,
        then the Indicator is ready to use. returned 'True'
        """
        return (self.check_unit() and self.check_confine())

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'name': self.name,
            'pinyin': self.pinyin,
            'englishName': self.englishName,
            'description': self.description,
            'helpText': self.helpText,
            'addByUser_id': self.addByUser.id,
            'dataType': self.dataType,
            'categories_id': [c.id
                for c in self.categories.all()],
            'units_id': [u.id
                for u in self.get_unit(type="all")]
        }
        return dump_data
# }}}


class UserIndicator(models.Model):                          # {{{
    """
    记录某用户关注了哪些指标
    """
    user = models.OneToOneField(User, verbose_name=u"用户",
            related_name="user_indicator")
    followedIndicators = models.ManyToManyField(Indicator,
            verbose_name=u"关注的指标",
            related_name="user_indicators",
            null=True, blank=True)

    class Meta:
        verbose_name_plural = u"用户指标信息"

    def __unicode__(self):
        return u"< UserIndicator: for %s >" % self.user.username
# }}}


class IndicatorRecord(models.Model):                        # {{{
    """
    指标记录
    对应某指标某一次的数据记录
    """
    indicator = models.ForeignKey(Indicator, verbose_name=u"化验指标",
            related_name="indicator_records")
    user = models.ForeignKey(User, verbose_name=u"用户",
            related_name="indicator_records")
    # date
    created_at = models.DateTimeField(u"创建时间", auto_now_add=True)
    updated_at = models.DateTimeField(u"更新时间",
            auto_now_add=True, auto_now=True)
    # data
    date = models.DateField(u"化验日期")
    # TODO: limit_choices_to
    unit = models.ForeignKey("Unit", verbose_name=u"数据单位",
            related_name="indicator_records", null=True, blank=True)
    value = models.CharField(u"指标数据值", max_length=30,
            blank=True)
    val_max = models.FloatField(u"数据范围上限",
            null=True, blank=True)
    val_min = models.FloatField(u"数据范围下限",
            null=True, blank=True)
    notes = models.TextField(u"记录说明", blank=True)

    class Meta:
        verbose_name_plural = u"指标数据记录"
        ordering = ['id', 'date', 'created_at']

    def __unicode__(self):
        return u"< IndicatorRecord: #%s; %s, %s, %s >" % (self.id,
                self.user.username, self.indicator.name, self.date)

    def get_absolute_url(self):
        return reverse('show-record',
                kwargs={'pk': self.id})

    def save(self, **kwargs):
        if self.is_valid() and self.check_confine:
            super(IndicatorRecord, self).save(**kwargs)
        else:
            raise ValueError(u'您输入的数据不符合要求')

    def is_valid(self, **kwargs):                           # {{{
        """验证输入数据是否合法"""
        if self.indicator.dataType == self.indicator.INTEGER_TYPE:
            # 整数型
            try:
                value = int(self.value)
                return True
            except ValueError:
                raise ValueError(u'您提交的指标数据类型不正确')
                return False
        elif self.indicator.dataType == self.indicator.FLOAT_TYPE:
            # 浮点型
            if not self.unit:
                raise ValueError(u'未填写单位')
                return False
            try:
                value = float(self.value)
                return True
            except ValueError:
                raise ValueError(u'value 数据类型不正确')
                return False
        elif self.indicator.dataType == self.indicator.RANGE_TYPE:
            # 范围型
            if not self.unit:
                raise ValueError(u'未填写单位')
                return False
            if (self.val_max is None) or (self.val_min is None):
                raise ValueError(u'val_max 或 val_min 未填写')
                return False
            if (self.val_max <= self.val_min):
                raise ValueError(u'val_max <= val_min')
                return False
            return True
        elif self.indicator.dataType == self.indicator.FLOAT_RANGE_TYPE:
            # 定值/范围型 (浮点定值优先)
            if not self.unit:
                raise ValueError(u'未填写单位')
                return False
            if self.value:
                # 定值
                try:
                    value = float(self.value)
                    return True
                except ValueError:
                    raise ValueError(u'value 数据类型不正确')
                    return False
            elif (self.val_max is not None) and (self.val_min is not None):
                # 范围值
                if (self.val_max <= self.val_min):
                    raise ValueError(u'val_max <= val_min')
                    return False
                else:
                    return True
            else:
                raise ValueError(u'您提交的指标数据不符合要求')
                return False
        elif self.indicator.dataType == self.indicator.PM_TYPE:
            # +/- 型,无单位要求
            if (len(self.value) == 1) and (self.value in [u'+', u'-']):
                return True
            else:
                raise ValueError(u'value 只接受 "+" 或 "-"')
                return False
        ## TODO: RADIO_TYPE, CHECKBOX_TYPE
        elif self.indicator.dataType in [self.indicator.RADIO_TYPE,
                self.indicator.CHECKBOX_TYPE]:
            raise ValueError(u'RADIO_TYPE, CHECKBOX_TYPE 验证未实现')
            return False
        else:
            raise ValueError(u'指标数据类型不合法')
            return False
    # }}}

    def check_confine(self, **kwargs):                      # {{{
        """
        check if the record data within the related confine:
        math_min <= value <= math_max

        NOTE: convert record data to 'standard unit' before comparison
        """
        sind = self.indicator
        # unit relation
        unit_rel = self.unit.relation
        v = sympy.symbols('v')
        rel_sym = sympy.sympify(unit_rel)
        # error message
        errmsg = u"'%s' 求值错误,请检查只含有变量 'v'" % unit_rel
        # check
        if sind.dataType in [sind.FLOAT_TYPE, sind.RANGE_TYPE,
                sind.FLOAT_RANGE_TYPE]:
            if not sind.check_confine():
                return False
            # InnateConfine is ok
            sic = sind.innate_confine
            # value
            if self.value:
                try:
                    value = float(self.value)
                except ValueError:
                    print u'ERROR: value="%s" cannot convert to float'\
                            % self.value
                    return False
                # 'value' unit conversion
                try:
                    value_std = float(rel_sym.evalf(subs={v: value}))
                except ValueError:
                    print errmsg
                    raise ValueError(errmsg)
                if (value_std < sic.math_min) or (
                        value_std > sic.math_max):
                    print u'ERROR: value(std) < math_min or value(std) > math_max'
                    return False
            # val_max
            if self.val_max is not None:
                # unit conversion
                try:
                    val_max_std = float(rel_sym.evalf(
                        subs={v: self.val_max}))
                except ValueError:
                    print errmsg
                    raise ValueError(errmsg)
                if (val_max_std <= sic.math_min) or (
                        val_max_std > sic.math_max):
                    print u'ERROR: val_max(std) <= math_min or val_max(std) > math_max'
                    return False
            # val_min
            if self.val_min is not None:
                try:
                    val_min_std = float(rel_sym.evalf(
                        subs={v: self.val_min}))
                except ValueError:
                    print errmsg
                    raise ValueError(errmsg)
                if (val_min_std < sic.math_min) or (
                        val_min_std >= sic.math_max):
                    print u'ERROR: val_min(std) < math_min or val_min(std) >= math_max'
                    return False
            # check finished
            return True
        else:
            # INTEGER_TYPE or PM_TYPE
            return True

    # }}}

    def get_data(self, **kwargs):                           # {{{
        """
        get the record data
        in unit originally filled by the user
        """
        # check the indicator.dataType
        sind = self.indicator
        if sind.dataType in [sind.FLOAT_TYPE, sind.RANGE_TYPE,
                sind.FLOAT_RANGE_TYPE]:
            # self.value
            if self.value:
                value = float(self.value)
            else:
                value = None
            # self.val_max
            if self.val_max:
                val_max = self.val_max
            else:
                val_max = None
            # self.val_min
            if self.val_min:
                val_min = self.val_min
            else:
                val_min = None
            # output data
            data = {
                'date': self.date.isoformat(),
                'value': value,
                'val_max': val_max,
                'val_min': val_min,
                'unit': self.unit.dump(),
                'notes': self.notes,
                'record_histories_id': [rh.id
                    for rh in self.record_histories.all()],
            }
        else:
            data = {
                'date': self.date.isoformat(),
                'value': self.value,
                'val_max': self.val_max,
                'val_min': self.val_min,
                'unit': {},
                'notes': self.notes,
                'record_histories_id': [rh.id
                    for rh in self.record_histories.all()],
            }
        return data
    # }}}

    def get_data_std(self, **kwargs):                       # {{{
        """
        get the record data in 'standard unit'
        """
        # check the indicator.dataType
        sind = self.indicator
        if sind.dataType in [sind.FLOAT_TYPE, sind.RANGE_TYPE,
                sind.FLOAT_RANGE_TYPE]:
            # check if self.unit standard
            if self.unit.standard:
                return self.get_data(**kwargs)
            # check if specified 'standard unit' for this indicator
            elif sind.check_unit():
                # unit relation
                std_unit = sind.get_unit(type="standard")[0]
                unit_rel = self.unit.relation
                v = sympy.symbols('v')
                rel_sym = sympy.sympify(unit_rel)
                # error message
                errmsg = u"'%s' 求值错误,请检查只含有变量 'v'" % unit_rel
                # self.value
                if self.value:
                    value = float(self.value)
                    try:
                        value_std = float(rel_sym.evalf(
                            subs={v: value}))
                    except ValueError:
                        print errmsg
                        raise ValueError(errmsg)
                else:
                    value_std = None
                # self.val_max
                if self.val_max:
                    val_max = self.val_max
                    try:
                        val_max_std = float(rel_sym.evalf(
                            subs={v: val_max}))
                    except ValueError:
                        print errmsg
                        raise ValueError(errmsg)
                else:
                    val_max_std = None
                # self.val_min
                if self.val_min:
                    val_min = self.val_min
                    try:
                        val_min_std = float(rel_sym.evalf(
                            subs={v: val_min}))
                    except ValueError:
                        print errmsg
                        raise ValueError(errmsg)
                else:
                    val_min_std = None
                # output data
                data_std = {
                    'date': self.date.isoformat(),
                    'value': value_std,
                    'val_max': val_max_std,
                    'val_min': val_min_std,
                    'unit': std_unit.dump(),
                    'notes': self.notes,
                    'record_histories_id': [rh.id
                        for rh in self.record_histories.all()],
                }
                return data_std
            else:
                print u"id=%s Indicator 尚未指定标准单位" % sind.id
                return {}
        else:
            return self.get_data(**kwargs)
    # }}}

    def is_normal(self, **kwargs):                          # {{{
        """
        compare the given data with the indicator confines.

        if the data within the confines, then return 'True',
        which suggests the indicator is normal.
        if the data out of the confines, then return 'False'.

        * return 'None' if there are other problems.
        """
        sind = self.indicator
        # 先检查 Unit 和 InnateConfine 是否已经正确指定
        if not sind.is_ready():
            print u"ERROR: Indicator id=%s NOT ready yet" % sind.id
            return None
        sic = sind.innate_confine
        # 获取以标准单位为单位的数据
        data_std = self.get_data_std()
        # 根据数据类型判断是否处于正常情况
        if sind.dataType == sind.INTEGER_TYPE:
            # 整数型
            value = int(data_std['value'])
            val_norm = int(sic.val_norm)
            # XXX: modify accordingly
            if value == val_norm:
                return True
            else:
                return False
        elif sind.dataType == sind.FLOAT_TYPE:
            # 浮点型
            value = data_std['value']
            human_max = sic.human_max
            human_min = sic.human_min
            if (value <= human_max) and (value >= human_min):
                return True
            else:
                return False
        elif sind.dataType == sind.RANGE_TYPE:
            # 范围型
            val_max = data_std['val_max']
            val_min = data_std['val_min']
            human_max = sic.human_max
            human_min = sic.human_min
            if (val_max <= human_max) and (val_min >= human_min):
                return True
            else:
                return False
        elif sind.dataType == sind.FLOAT_RANGE_TYPE:
            # 浮点型/范围型
            if self.value:
                value = float(data_std['value'])
                human_max = sic.human_max
                human_min = sic.human_min
                if (value <= human_max) and (value >= human_min):
                    return True
                else:
                    return False
            elif self.val_max and self.val_min:
                # 范围值
                val_max = data_std['val_max']
                val_min = data_std['val_min']
                human_max = sic.human_max
                human_min = sic.human_min
                if (val_max <= human_max) and (val_min >= human_min):
                    return True
                else:
                    return False
            else:
                print u'数据类型错误'
                raise ValueError(u'数据类型错误')
                return None
        elif sind.dataType == sind.PM_TYPE:
            # 阴阳(+/-)型
            value = data_std['value']
            val_norm = sic.val_norm
            if value == val_norm:
                return True
            else:
                return False
        elif sind.dataType in [sind.RADIO_TYPE, sind.CHECKBOX_TYPE]:
            print u'RADIO_TYPE, CHECKBOX_TYPE 验证未实现'
            raise ValueError(u'RADIO_TYPE, CHECKBOX_TYPE 验证未实现')
            return None
        else:
            print u'数据类型不合法'
            raise ValueError(u'数据类型不合法')
            return None
    # }}}

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'indicator_id': self.indicator.id,
            'user_id': self.user.id,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'date': self.date.isoformat(),
            'unit_id': self.unit.id,
            'value': self.value,
            'val_max': self.val_max,
            'val_min': self.val_min,
            'notes': self.notes,
            'record_histories_id': [rh.id
                for rh in self.record_histories.all()],
        }
        return dump_data
# }}}


class RecordHistory(models.Model):                          # {{{
    """
    指标记录 IndicatorRecord 的历史数据和对应的修改原因
    """
    indicatorRecord = models.ForeignKey("IndicatorRecord",
            verbose_name=u"指标数据记录",
            related_name="record_histories")
    # modification datetime
    created_at = models.DateTimeField(u"创建时间", auto_now_add=True)
    # modification reason
    reason = models.TextField(u"修改原因")
    # original data before modification
    date_bak = models.DateField(u"原化验日期", blank=True,
            editable=False)
    unit_bak = models.ForeignKey("Unit", verbose_name=u"原数据单位",
            related_name="record_histories",
            null=True, blank=True, editable=False)
    value_bak = models.CharField(u"原指标数据值", max_length=30,
            blank=True, editable=False)
    val_max_bak = models.FloatField(u"原数据范围上限",
            null=True, blank=True, editable=False)
    val_min_bak = models.FloatField(u"原数据范围下限",
            null=True, blank=True, editable=False)
    notes_bak = models.TextField(u"原记录说明", blank=True,
            editable=False)

    class Meta:
        verbose_name_plural = u"记录修改历史"
        ordering = ['indicatorRecord__id', 'created_at']

    def __unicode__(self):
        return u"< RecordHistory: for Record #%s, %s >"\
                % (self.indicatorRecord.id, self.created_at)

    def save(self, **kwargs):
        sr = self.indicatorRecord
        # get history data from *not-saved* IndicatorRecord
        self.date_bak = sr.date
        self.unit_bak = sr.unit
        self.value_bak = sr.value
        self.val_max_bak = sr.val_max
        self.val_min_bak = sr.val_min
        self.notes_bak = sr.notes
        # save
        super(RecordHistory, self).save(**kwargs)

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'indicatorRecord_id': self.indicatorRecord.id,
            'created_at': self.created_at.isoformat(),
            'reason': self.reason,
            'date_bak': self.date_bak.isoformat(),
            'unit_bak_id': self.unit_bak.id,
            'value_bak': self.value_bak,
            'val_max_bak': self.val_max_bak,
            'val_min_bak': self.val_min_bak,
            'notes_bak': self.notes_bak,
        }
        return dump_data
# }}}


class Unit(models.Model):                                   # {{{
    """
    指标单位
    是否为标准单位,其他单位与标准单位的换算关系
    """
    # related to the `indicator'
    indicator = models.ForeignKey(Indicator, verbose_name=u"指标",
            related_name="units")
    name = models.CharField(u"单位名称", max_length=50)
    symbol = models.CharField(u"单位符号", max_length=50)
    standard = models.BooleanField(u"是否标准单位", default=False)
    # conversion relation
    relation = models.CharField(u"与标准单位的映射",
            help_text=u"value (std_unit) = f(v)",
            max_length=100, blank=True)
    description = models.TextField(u"单位描述", blank=True)
    # 记录添加的用户,用户只能修改自己添加的对象
    addByUser = models.ForeignKey(User, verbose_name=u"添加的用户",
            related_name="units")

    class Meta:
        verbose_name_plural = u"单位"

    def __unicode__(self):
        if self.standard:
            _std = ' (*)'
        else:
            _std = ''
        return u"< Unit: %s%s for %s, addBy %s >" % (self.name,
                _std, self.indicator.name, self.addByUser.username)

    def is_valid(self):
        if self.standard:
            std_unit_list = self.indicator.get_unit(type="standard")
            if std_unit_list:
                std_unit = std_unit_list[0]
                if self.id == std_unit.id:
                    return True
                else:
                    print u"该指标已经指定了标准单位"
                    raise ValueError(u"该指标已经指定了标准单位")
                    return False
            else:
                return True
        else:
            if (not self.relation):
                print u"单位映射关系未填写"
                raise ValueError(u"单位映射关系未填写")
                return False
            else:
                try:
                    fsym = sympy.sympify(self.relation)
                    return True
                except SympifyError:
                    print u"'%s' 不是合法的算术表达式" % self.relation
                    raise ValueError(u"'%s' 不是合法的算术表达式"\
                            % self.relation)
                    return False

    def save(self, **kwargs):
        if self.standard:
            self.relation = "v"
        if self.is_valid():
            super(Unit, self).save(**kwargs)

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'indicator_id': self.indicator.id,
            'name': self.name,
            'symbol': self.symbol,
            'standard': self.standard,
            'relation': self.relation,
            'addByUser_id': self.addByUser.id,
        }
        return dump_data
# }}}


class InnateConfine(models.Model):                          # {{{
    """
    指标数据范围
    数学可能值范围,人体正常值范围

    注意:
    如果数据类型需要单位,则必须使用"标准单位";
    IndicatorRecord.is_normal() 方法需要如此;
    因为 标准单位 到 其他单位 的换算没有实现。
    """
    # indicator
    indicator = models.OneToOneField("Indicator",
            verbose_name=u"指标", related_name="innate_confine")
    # unit
    # TODO: limit_choices_to
    unit = models.ForeignKey("Unit", related_name="innate_confines",
            verbose_name=u"单位", null=True, blank=True)
    # normal value (for INTEGER_TYPE, PM_TYPE)
    val_norm = models.CharField(u"正常值", max_length=30, blank=True,
            help_text=u'填写"整数型","阴阳(+/-)型数据"')
    # normal range
    human_max = models.FloatField(u"人体正常值上限",
            null=True, blank=True)
    human_min = models.FloatField(u"人体正常值下限",
            null=True, blank=True)
    # possbile range
    math_max = models.FloatField(u"数学可能值上限",
            null=True, blank=True)
    math_min = models.FloatField(u"数学可能值下限",
            null=True, blank=True)
    # description or notes
    description = models.TextField(u"描述", blank=True)
    # 记录添加的用户,用户只能修改自己添加的对象
    addByUser = models.ForeignKey(User, verbose_name=u"添加的用户",
            related_name="innate_confines")

    class Meta:
        verbose_name_plural = u"固有数值范围"

    def __unicode__(self):
        return u"< InnateConfine: for %s, addBy %s >"\
                % (self.indicator.name, self.addByUser.username)

    def save(self, **kwargs):
        """
        check the data before save
        """
        if self.is_valid():
            super(InnateConfine, self).save(**kwargs)
        else:
            print u"您填写的数据不符合要求,请检查"
            return self

    def is_valid(self):                                     # {{{
        """
        check the validity of data
        """
        sind = self.indicator
        if sind.dataType in [sind.FLOAT_TYPE, sind.RANGE_TYPE,
                sind.FLOAT_RANGE_TYPE]:
            # check unit
            if not (self.unit and self.unit.standard):
                raise ValueError(u'单位未填写/不是标准单位')
                return False
            if (self.human_max is None) or (self.human_min is None):
                raise ValueError(u'Error: human_max 或 human_min 未填写')
                return False
            if not (self.human_max > self.human_min):
                raise ValueError(u'Error: human_max <= human_min')
                return False
            # check 'math_max' and 'math_min'
            if (self.math_max is None) or (self.math_min is None):
                raise ValueError(u'Error: math_max 或 math_min 未填写')
                return False
            if not (self.math_max > self.math_min):
                raise ValueError(u'Error: math_max <= math_min')
                return False
            # compare 'human*' and 'math*'
            if (self.human_max > self.math_max) or (
                    self.human_min < self.math_min):
                raise ValueError(u'Error: human_max>math_max / human_min<math_min')
                return False
            # check finished
            return True
        elif sind.dataType == sind.INTEGER_TYPE:
            # 整数型
            try:
                val_norm = int(self.val_norm)
                return True
            except ValueError:
                raise ValueError(u'val_norm="%s" 不是整数型值'
                        % self.val_norm)
                return False
        elif sind.dataType == sind.PM_TYPE:
            # 阴阳(+/-)型
            if (len(self.val_norm) == 1) and (
                    self.val_norm in [u'+', u'-']):
                return True
            else:
                raise ValueError(u'value 只接受 "+" 或 "-"')
                return False
        ## TODO: RADIO_TYPE, CHECKBOX_TYPE
        elif sind.dataType in [sind.RADIO_TYPE, sind.CHECKBOX_TYPE]:
            raise ValueError(u'RADIO_TYPE, CHECKBOX_TYPE 验证未实现')
            return False
        else:
            raise ValueError(u'数据不符合要求')
            return False
    # }}}

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'indicator_id': self.indicator.id,
            'unit': self.unit.dump(),
            'val_norm': self.val_norm,
            'human_max': self.human_max,
            'human_min': self.human_min,
            'math_max': self.math_max,
            'math_min': self.math_min,
            'addByUser_id': self.addByUser.id,
        }
        return dump_data
# }}}


class StatisticalConfine(models.Model):                     # {{{

    deviation_ceiling = models.FloatField(u"统计偏差范围上限", null=True, blank=True)
    deviation_floor   = models.FloatField(u"统计偏差范围限", null=True, blank=True)

    class Meta:
        verbose_name_plural = u"统计数值范围"

    def __unicode__(self):
        return "< StatisticalConfine: %s >" % self.id
# }}}


#class UnitLabSheet(models.Model):                           # {{{
#
#    equipment = models.CharField(u"化验设备", max_length=100, null=True, blank= True)
#    #figure = models.OneToOneField("figure.Figure", verbose_name=u"图片", related_name="unitlabsheet")
#    unit_standard = models.ForeignKey("UnitStandard", verbose_name = u"单位标准", related_name="unit_lab_sheets", null=True, blank=True)
#
#    class Meta:
#        verbose_name_plural = u"标准化验单"
#
#    def __unicode__(self):
#        return "< UnitLabSheet: %s >" % self.id
## }}}


#class ReviseReason(models.Model):                           # {{{
#    """
#    记录 IndicatorRecord 数据修改原因
#    医学数据重要且要求准确,不可随意修改
#    ReviseReason 添加后不允许再修改?
#    """
#    # TODO: 中文支持
#    content = models.TextField(u"内容")
#    created_at = models.DateTimeField(u"创建时间",
#            editable=False, auto_now_add=True)
#    user = models.ForeignKey(User, verbose_name=u"用户")
#
#    class Meta:
#        verbose_name_plural=u"指标记录修改原因"
#
#    def __unicode__(self):
#        return u"< ReviseReason: %s, %s >" % (self.id,
#                self.user.username)
#
#    def dump(self, **kwargs):
#        dump_data = {
#            'id': self.id,
#            'content': self.content,
#            'created_at': self.created_at.isoformat(),
#            'user_id': self.user.id,
#        }
#        return dump_data
## }}}


class RelatedIndicator(models.Model):                       # {{{
    """
    记录 blog/annotation 与哪些 indicator 关联,
    以及关联的权重。

    用于为用户推荐可以关注的指标。
    """
    # indicator
    indicator = models.ForeignKey("Indicator",
            related_name="related_indicators",
            verbose_name=u"待关联指标")
    # type of related object
    ANNOTATION_TYPE = 'AN'
    BLOG_TYPE = 'BL'
    OBJECT_TYPES = (
        (ANNOTATION_TYPE, '文章注释'),
        (BLOG_TYPE, '文章'),
    )
    objectType = models.CharField(u"待关联目标类型", max_length=2,
            choices=OBJECT_TYPES)
    # objects
    annotation = models.ForeignKey("sciblog.BlogAnnotation",
            related_name="related_indicators",
            verbose_name=u"待关联文章注释", null=True, blank=True)
    blog = models.ForeignKey("sciblog.SciBlog",
            related_name="related_indicators",
            verbose_name=u"待关联文章", null=True, blank=True)
    # weight
    weight = models.FloatField(u"权重", help_text=u"范围: 0-10")
    # datetime
    created_at = models.DateTimeField(u"创建时间", auto_now_add=True)
    updated_at = models.DateTimeField(u"更新时间",
            auto_now_add=True, auto_now=True)

    class Meta:
        verbose_name_plural = u"指标关联信息"
        ordering = ['objectType']

    def __unicode__(self):
        if self.objectType == self.ANNOTATION_TYPE:
            info = 'Annotation #%s' % self.annotation.id
        elif self.objectType == self.BLOG_TYPE:
            info = 'Blog #%s' % self.blog.id
        else:
            info = '%s' % self.objectType
        return u"< RelatedIndicator: %s -> %s >"\
                % (info, self.indicator.name)

    def save(self, **kwargs):
        if self.is_valid():
            if self.objectType == self.ANNOTATION_TYPE:
                self.blog = None
            if self.objectType == self.BLOG_TYPE:
                self.annotation = None
            # save
            super(RelatedIndicator, self).save(**kwargs)
        else:
            return self

    def is_valid(self, **kwargs):                           # {{{
        """
        annotation/blog must be consistent with objectType
        """
        # check objectType
        if self.objectType == self.ANNOTATION_TYPE:
            if not self.annotation:
                raise ValueError(u"Error: annotation 未填写")
                return False
        elif self.objectType == self.BLOG_TYPE:
            if not self.blog:
                raise ValueError(u"Error: blog 未填写")
                return False
        else:
            raise ValueError(u"Error: objectType 不合法")
            return False
        # check weight range
        if (self.weight < 0.0) or (self.weight > 10.0):
            raise ValueError(u"Error: weight < 0.0 / weight > 10.0")
            return False
        # finished
        return True
    # }}}

    def dump(self, **kwargs):
        # annotation_id
        if self.annotation:
            annotation_id = self.annotation.id
        else:
            annotation_id = None
        # blog_id
        if self.blog:
            blog_id = self.blog.id
        else:
            blog_id = None
        # dump data
        dump_data = {
            'id': self.id,
            'indicator_id': self.indicator.id,
            'objectType': self.objectType,
            'annotation_id': annotation_id,
            'blog_id': blog_id,
            'weight': self.weight,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
        }
        return dump_data
# }}}



admin.site.register([
                     IndicatorCategory,
                     Indicator,
                     UserIndicator,
                     IndicatorRecord,
                     RecordHistory,
                     Unit,
                     InnateConfine,
                     StatisticalConfine,
                     #UnitLabSheet,
                     #ReviseReason,
                     RelatedIndicator,
                    ])

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