aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/recommend/models.py
blob: 9fe8b42f47649f03bc7ef3fbecb565fda30acc8f (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
# -*- coding: utf-8 -*-

"""
models for apps/recommend
"""


from django.db import models
from django.contrib import admin

from utils.tools import format_float

import re


class TreatResponse(models.Model):                           # {{{
    """
    治疗反应/结果的描述,以及结果的价值/权重
    """
    name = models.CharField(u"名称", max_length=100)
    description = models.TextField(u"详细描述", blank=True)
    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"治疗反应"

    def __unicode__(self):
        return u"< TreatResponse: %s >" % self.name

    def save(self, **kwargs):
        if self.is_valid():
            super(TreatResponse, self).save(**kwargs)
        else:
            return self

    def is_valid(self, **kwargs):
        # check weight range
        if (self.weight < 0.0) or (self.weight > 10.0):
            print u"Error: weight < 0.0 / weight > 10.0"
            raise ValueError(u"Error: weight<0.0 / weight>10.0")
            return False
        #
        return True

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


class ResearchIndicator(models.Model):                      # {{{
    """
    model to record the indicators which researched in the paper
    """
    MEAN_TYPE = 'MEAN'
    SCOPE_TYPE = 'SCOP'
    KIND_TYPE = 'KIND'
    DATA_TYPES = (
        (MEAN_TYPE, u'平均值'),
        (SCOPE_TYPE, u'范围'),
        (KIND_TYPE, u'种类'),
    )
    blog = models.ForeignKey("sciblog.SciBlog",
            related_name="research_indicators",
            verbose_name=u"待关联文章")
    indicator = models.ForeignKey("indicator.Indicator",
            related_name="research_indicators",
            verbose_name=u"待关联指标")
    dataType = models.CharField(u"指标值类型", max_length=4,
            choices=DATA_TYPES)
    # 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 = ['blog__id', 'indicator__id']

    def __unicode__(self):
        # cut down the length of title
        blog_title = self.blog.title[:10]
        return u"< ResearchIndicator: %s -> %s (type %s) >" % (
                blog_title, self.indicator.name, self.dataType)

    def save(self, **kwargs):
        if self.is_valid():
            super(ResearchIndicator, self).save(**kwargs)
        else:
            return self

    def is_valid(self, **kwargs):                           # {{{
        # check dataType, which should be consistent with the
        # dataType of the related indicator
        ind_obj = self.indicator
        ind_dataType = ind_obj.dataType
        if self.dataType == self.MEAN_TYPE:
            if ind_dataType not in [ind_obj.FLOAT_TYPE, ind_obj.FLOAT_RANGE_TYPE]:
                raise ValueError(u"Error: dataType与Indicator不符")
                return False
        elif self.dataType == self.SCOPE_TYPE:
            if ind_dataType not in [ind_obj.RANGE_TYPE, ind_obj.FLOAT_RANGE_TYPE, ind_obj.FLOAT_TYPE]:
                raise ValueError(u"Error: dataType与Indicator不符")
                return False
        elif self.dataType == self.KIND_TYPE:
            if ind_dataType in [ind_obj.FLOAT_TYPE,
                    ind_obj.RANGE_TYPE, ind_obj.FLOAT_RANGE_TYPE]:
                raise ValueError(u"Error: dataType与Indicator不符")
                return False
        else:
            raise ValueError(u"Error: unknown dataType")
            return False
        #
        return True
    # }}}

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'blog_id': self.blog.id,
            'indicator_id': self.indicator.id,
            'indicator_name': self.indicator.name,
            'dataType': self.dataType,
        }
        return dump_data
# }}}


# class ResearchCombination(models.Model):                    # {{{
#     """
#     记录文章研究的有效的指标组合
#     """
#     blog = models.ForeignKey("sciblog.SciBlog",
#             related_name="research_combinations",
#             verbose_name=u"待关联文章")
#     combination = models.ManyToManyField("indicator.Indicator",
#             related_name="research_combinations",
#             verbose_name=u"研究指标组合")
#     # 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 = ['blog__id']
# 
#     def __unicode__(self):
#         # cut down the length of title
#         blog_title = self.blog.title[:10]
#         combination = u''
#         for ind in self.combination:
#             combination = combination + ind.name + ", "
#         combination = re.sub(r',\ $', '', combination)
#         return u"< ResearchCombination: %s -> (%s) >" % (
#                 blog_title, combination)
# 
#     def save(self, **kwargs):
#         if self.is_valid():
#             super(ResearchCombination, self).save(**kwargs)
#         else:
#             return self
# 
#     def is_valid(self):
#         """
#         These M2M indicators must have related to the blog
#         """
#         related_indicators = [ri.indicator
#                 for ri in self.blog.research_indicators.all()]
#         for ind in self.combination:
#             if ind not in related_indicators:
#                 raise ValueError(u"Error: 选择了未关联到该文章的指标")
#                 return False
#         return True
# # }}}


# ResearchAtom {{{
class ResearchAtom(models.Model):
    """
    ???any good name???
    用于记录某篇文章中对某个指标所分的每一个小类的具体信息
    """
    blog = models.ForeignKey("sciblog.SciBlog",
            related_name="research_atoms",
            verbose_name=u"待关联文章")
    researchIndicator = models.ForeignKey("ResearchIndicator",
            related_name="research_atoms",
            verbose_name=u"文章研究指标")
    ## value
    # unit (XXX: only standard unit supported at the moment)
    unit = models.ForeignKey("indicator.Unit",
            null=True, blank=True,
            related_name="research_atoms", verbose_name=u"单位")
    # dataType: MEAN
    mean = models.FloatField(u"平均值", null=True, blank=True)
    sd = models.FloatField(u"标准值", null=True, blank=True)
    # dataType: SCOP
    scope_min = models.FloatField(u"范围下限", null=True, blank=True)
    scope_max = models.FloatField(u"范围上限", null=True, blank=True)
    # dataType: KIND
    kind = models.ForeignKey("indicator.ValueKind",
            null=True, blank=True,
            related_name="research_atoms", verbose_name=u"种类")
    ## 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 = ['blog__id', 'researchIndicator__id']

    def __unicode__(self):
        return u'< ResearchAtom: #%s %s >' % (self.id, self.display())

    def save(self, **kwargs):
        if self.is_valid():
            super(ResearchAtom, self).save(**kwargs)
        else:
            return self

    def is_valid(self, **kwargs):                           # {{{
        """
        The blog here must be consistent with the blog related to
        the researchIndicator
        """
        ri_obj = self.researchIndicator
        dataType = ri_obj.dataType
        # confine
        ind_obj = ri_obj.indicator
        if ind_obj.check_confine():
            confine = ind_obj.get_confine()
        else:
            raise ValueError(u"Error: 该指标未指定InnateConfine")
            return False
        # check blog id first
        if self.blog.id != ri_obj.blog.id:
            raise ValueError(u"Error: 关联blog错误")
            return False
        # check dataType and confine
        if dataType == ri_obj.MEAN_TYPE:
            if not (self.unit and self.unit.standard):
                raise ValueError(u"Error: 单位未填写/不是标准单位")
                return False
            if ((self.mean is None) or (self.sd is None)):
                raise ValueError(u"Error: 平均值/标准差未填写")
                return False
            # check with confine
            # XXX: only check 'mean' at the moment; 'sd' may also needed
            if (self.mean < confine['math_min']) or (
                    self.mean > confine['math_max']):
                raise ValueError(u"Error: 平均值超出允许范围")
                return False
        elif dataType == ri_obj.SCOPE_TYPE:
            if not (self.unit and self.unit.standard):
                raise ValueError(u"Error: 单位未填写/不是标准单位")
                return False
            if ((self.scope_min is None) or (self.scope_max is None)):
                raise ValueError(u"Error: 范围下限/上限未填写")
                return False
            if (self.scope_min >= self.scope_max):
                raise ValueError(u"Error: scope_min>=scope_max")
                return False
            # check confine
            if (self.scope_min < confine['math_min']) or (
                    self.scope_max > confine['math_max']):
                raise ValueError(u"Error: scope_min/scope_max 超出允许范围")
                return False
        elif dataType == ri_obj.KIND_TYPE:
            if not self.kind:
                raise ValueError(u"Error: 未选择种类")
                return False
        else:
            raise ValueError(u"Error: unknown dataType")
            return False
        #
        return True
    # }}}

    def get_value(self, **kwargs):                          # {{{
        ri_obj = self.researchIndicator
        dataType = ri_obj.dataType
        value = {
            'id': self.id,
            'dataType': dataType,
            'blog_id': self.blog.id,
        }
        if dataType == ri_obj.MEAN_TYPE:
            value['mean'] = self.mean
            value['sd'] = self.sd
            value['unit'] = self.unit.dump()
        elif dataType == ri_obj.SCOPE_TYPE:
            value['scope_min'] = self.scope_min
            value['scope_max'] = self.scope_max
            value['unit'] = self.unit.dump()
        elif dataType == ri_obj.KIND_TYPE:
            value['kind'] = self.kind.dump()
        else:
            value['error'] = True
        return value
    # }}}

    def display(self, **kwargs):
        """
        generate the display string for front page
        """
        ri_obj = self.researchIndicator
        ind_name = ri_obj.indicator.name
        dataType = ri_obj.dataType
        if dataType == ri_obj.MEAN_TYPE:
            value = '%sSD%s' % (format_float(self.mean), self.sd)
        elif dataType == ri_obj.SCOPE_TYPE:
            value = '%s~%s' % (format_float(self.scope_min),
                    format_float(self.scope_max))
        elif dataType == ri_obj.KIND_TYPE:
            value = '%s' % (self.kind.name)
        else:
            value = 'UNKNOWN'
        disp_str = u'%s(%s|%s)' % (ind_name, dataType, value)
        return disp_str
# }}}


# ResearchConfig {{{
class ResearchConfig(models.Model):
    """
    记录某篇文章所研究的某一个具体的组合(哪几个指标的具体值)
    的治疗结果等信息
    """
    blog = models.ForeignKey("sciblog.SciBlog",
            related_name="research_configs",
            verbose_name=u"待关联文章")
    researchAtoms = models.ManyToManyField("ResearchAtom",
            related_name="research_configs",
            verbose_name=u"研究指标值组合")
    treatResponse = models.ForeignKey("TreatResponse",
            related_name="research_configs",
            verbose_name=u"治疗反应")
    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 = ['blog__id']

    def __unicode__(self):
        # XXX
        info = ''
        for atom in self.researchAtoms.all():
            info = '%s%s,' % (info, atom.id)
        info = re.sub(r',\s*$', '', info)
        return u'< ResearchConfig: #%s (Atoms: %s) -> %s >' % (
                self.id, info, self.blog.title[:10])

    def save(self, **kwargs):
        if self.is_valid():
            super(ResearchConfig, self).save(**kwargs)
        else:
            return self

    def is_valid(self):
        """
        The blog here must be consistent with the blog related to
        the researchAtoms
        """
        # 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
        #### check blog ####
        ## Error: needs to have a value for field "researchconfig" before this many-to-many relationship can be used.
        #for atom in self.researchAtoms.all():
        #    if atom.blog.id != self.blog.id:
        #        raise ValueError(u"Error: 关联blog错误")
        #        return False
        #### end ####
        return True

    def is_ok(self, **kwargs):
        """
        check this config whether ok or not?
        i.e.: whether the data fields are valid?
        """
        # check atoms
        if not self.researchAtoms.all():
            return False
        # check blog id
        for atom in self.researchAtoms.all():
            if atom.blog.id != self.blog.id:
                raise ValueError(u"Error: 关联blog错误")
                return False
        #
        return True

    def dump(self, **kwargs):
        dump_data = {
            'id': self.id,
            'blog_id': self.blog.id,
            'researchAtoms_id': [atom.id
                for atom in self.researchAtoms.all()],
            'treatResponse_id': self.treatResponse.id,
            'weight': self.weight,
        }
        return dump_data
# }}}


# admin
admin.site.register([
    TreatResponse,
    ResearchIndicator,
    #ResearchCombination,
    ResearchAtom,
    ResearchConfig,
])