aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/indicator/tools.py
blob: 663ec4f0527dbfc76f2927f0cba856d570b7abc0 (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
# -*- coding: utf-8 -*-

"""
utils for apps/indicator
"""

from django.contrib.auth.models import User

from indicator import models as im
from sciblog import models as sciblogm

import datetime


# get_indicator {{{
def get_indicator(category_id="all", startswith="all"):
    """
    根据指定的 category_id 和 startswith 获取 indicator
    返回一个 dict
    Dict format:
    dict = {
        'a': [ {'pinyin': 'aa', ...}, {'pinyin': 'ab', ...}, ... ],
        'b': [ {'pinyin': 'ba', ...}, {'pinyin': 'bb', ...}, ... ],
        ...
    }
    """

    _idict = {}
    if category_id == 'all':
        iqueryset = im.Indicator.objects.all()
    else:
        try:
            cid = int(category_id)
            cate = im.IndicatorCategory.objects.get(id=cid)
            iqueryset = cate.indicators.all()
        except ValueError:
            raise ValueError(u'category_id 不是整数型')
            return _idict
        except im.IndicatorCategory.DoesNotExist:
            raise ValueError(u'id=%s 的 IndicatorCategory 不存在'
                    % cid)
            return _idict

    if startswith == 'all':
        starts = map(chr, range(ord('a'), ord('z')+1))
    else:
        starts = []
        _str = startswith.lower()
        for i in _str:
            if i >= 'a' and i <= 'z':
                starts.append(i)

    for l in starts:
        iq = iqueryset.filter(pinyin__istartswith=l).order_by('pinyin')
        _idict[l] = [ i.dump() for i in iq ]
    return _idict
# }}}


# get_followed_indicator {{{
def get_followed_indicator(user_id, category_id="all", startswith="all"):
    """
    获取已关注的指标
    返回 dict, 格式与 get_indicator() 一致
    """

    u = User.objects.get(id=user_id)
    ui, created = im.UserIndicator.objects.get_or_create(user=u)
    _idict = {}
    iqueryset = ui.followedIndicators.all()
    if not category_id == 'all':
        try:
            cid = int(category_id)
            iqueryset = iqueryset.filter(categories__id=cid)
        except ValueError:
            raise ValueError(u'category_id 不是整数型')
            return _idict

    if startswith == 'all':
        starts = map(chr, range(ord('a'), ord('z')+1))
    else:
        starts = []
        _str = startswith.lower()
        for i in _str:
            if i >= 'a' and i <= 'z':
                starts.append(i)

    for l in starts:
        iq = iqueryset.filter(pinyin__istartswith=l).order_by('pinyin')
        _idict[l] = [ i.dump() for i in iq ]
    return _idict
# }}}


# get_unfollowed_indicator {{{
def get_unfollowed_indicator(user_id, category_id="all", startswith="all"):
    """
    获取未关注的指标
    返回 dict, 格式与 get_indicator() 一致
    """

    u = User.objects.get(id=user_id)
    ui, created = im.UserIndicator.objects.get_or_create(user=u)
    _idict = {}
    iqueryset = im.Indicator.objects.exclude(user_indicators=ui)
    if not category_id == 'all':
        try:
            cid = int(category_id)
            iqueryset = iqueryset.filter(categories__id=cid)
        except ValueError:
            raise ValueError(u'category_id 不是整数型')
            return _idict

    if startswith == 'all':
        starts = map(chr, range(ord('a'), ord('z')+1))
    else:
        starts = []
        _str = startswith.lower()
        for i in _str:
            if i >= 'a' and i <= 'z':
                starts.append(i)

    for l in starts:
        iq = iqueryset.filter(pinyin__istartswith=l).order_by('pinyin')
        _idict[l] = [ i.dump() for i in iq ]
    return _idict
# }}}


# get_record {{{
def get_record(user_id, indicator_id, begin="", end="", std=False):
    """
    get_record(user_id, indicator_id, begin="", end="", std=False)

    return a dict with 'date' as key, and 'get_data()' as value.
    args 'begin' and 'end' to specify the date range.
    arg 'std=True' to get data in standard unit
    if 'begin=""', then the earliest date is given;
    if 'end=""', then the latest date is given.

    return dict format:
    rdata = {
        'date1': [d1r1.get_data(), d1r2.get_data(), ...],
        'date2': [d2r1.get_data(), d2r2.get_data(), ...],
        ...
    }
    """
    uid = int(user_id)
    indid = int(indicator_id)
    all_records = im.IndicatorRecord.objects.\
            filter(user__id=uid, indicator__id=indid).\
            order_by('date', 'created_at')
    # check if 'all_records' empty
    if not all_records:
        return {}
    # set 'begin' and 'end'
    if begin == '':
        begin = all_records[0].date
    if end == '':
        end = all_records.reverse()[0].date
    # check the validity of given 'begin' and 'end'
    if (isinstance(begin, datetime.date) and
            isinstance(end, datetime.date)):
        records = all_records.filter(date__range=(begin, end))
        _rdata = {}
        for r in records:
            _d = r.date.isoformat()
            # get data
            if std:
                _data = r.get_data_std()
            else:
                _data = r.get_data()
            #
            if _rdata.has_key(_d):
                # the date key already exist
                _rdata[_d] += [_data]
            else:
                # the date key not exist
                _rdata[_d] = [_data]
        # return
        return _rdata
    else:
        raise ValueError(u"begin='%s' or end='%s' 不是合法的日期" %
                (begin, end))
        return {}
# }}}


# get_record_std {{{
def get_record_std(**kwargs):
    return get_record(std=True, **kwargs)
# }}}


# calc_indicator_weight {{{
def calc_indicator_weight(user_id, indicator_id):
    """
    calculate the weight of given indicator
    used by 'recommend_indicator'
    """
    ### XXX: weight_type: how to store the weights into database ###
    weight_annotation = 4.0
    weight_blog_catched = 3.0
    weight_blog_collected = 2.0
    weight_other = 1.0
    ################################################################
    # weight = weight_type * relatedindicator.weight
    user = User.objects.get(id=user_id)
    ri_qs = im.RelatedIndicator.objects.filter(indicator__id=indicator_id)
    if not ri_qs:
        # queryset empty
        w = 0.0
        return w
    # queryset not empty
    annotation_ri_qs = ri_qs.filter(annotation__collected_by=user)
    blogcatch_ri_qs = ri_qs.filter(blog__catched_by=user)
    blogcollect_ri_qs = ri_qs.filter(blog__collected_by=user)
    weights = []
    if annotation_ri_qs:
        # related to annotations collected by user
        for ri in annotation_ri_qs:
            w = weight_annotation * ri.weight
            weights.append(w)
    elif blogcatch_ri_qs:
        # related to blogs catched by user
        for ri in blogcatch_ri_qs:
            w = weight_blog_catched * ri.weight
            weights.append(w)
    elif blogcollect_ri_qs:
        # related to blogs catched by user
        for ri in blogcollect_ri_qs:
            w = weight_blog_collected * ri.weight
            weights.append(w)
    else:
        # other type, use 'ri_qs' here
        for ri in ri_qs:
            w = weight_other * ri.weight
            weights.append(w)
    # return final result
    return max(weights)
# }}}


# recommend_indicator {{{
def recommend_indicator(user_id, number):
    """
    recommend unfollowed indicator for user,
    based on his/her readings and collections.

    return a list with the id's of recommended indicators

    TODO:
    performance test
    """
    user_id = int(user_id)
    number = int(number)
    # get unfollowed indicators
    u = User.objects.get(id=user_id)
    ui, created = im.UserIndicator.objects.get_or_create(user=u)
    uf_ind_qs = im.Indicator.objects.exclude(user_indicators=ui)
    # calc weight for each unfollowed indicator
    weights = []
    for ind in uf_ind_qs:
        w = calc_indicator_weight(user_id, ind.id)
        weights.append({'id': ind.id, 'weight': w})
    # sort 'weights' dict list by key 'weight'
    weights_sorted = sorted(weights, key=lambda item: item['weight'])
    weights_sorted.reverse()
    # return results with largest weights
    return weights_sorted[:number]
# }}}