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

"""
apps/indicator views

"""

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, Http404
from django.shortcuts import render, get_object_or_404
# CRSF
from django.template import RequestContext

# haystack search
from haystack.query import SearchQuerySet

from indicator import models as im
from indicator.forms import *
from indicator.tools import *

# apps/utils
from utils.search_tools import objects_of_sqs

import re
import datetime

## json
# Django 1.5 deprecates 'django.utils.simplejson'
# in favor of Python 2.6's bulti-in 'json' module
try:
    import json
except ImportError:
    from django.utils import simplejson as json



def get_indicator_view(request, **kwargs):
    idict = get_indicator(**kwargs)
    return HttpResponse("%s" % idict)


@login_required
def get_followed_indicator_view(request, **kwargs):
    idict = get_followed_indicator(request.user.id, **kwargs)
    return HttpResponse("%s" % idict)


@login_required
def get_unfollowed_indicator_view(request, **kwargs):
    idict = get_unfollowed_indicator(request.user.id, **kwargs)
    return HttpResponse("%s" % idict)


@login_required
def recommend_indicator_view(request, **kwargs):
    ilist = recommend_indicator(request.user.id, **kwargs)
    return HttpResponse("%s" % ilist)


# get_record_view {{{
@login_required
def get_record_view(request, indicator_id, date_range, **kwargs):
    """
    get IndicatorRecord record
    """
    indicator_id = int(indicator_id)
    # regex to match given 'date_range' (yyyymmdd-yyyymmdd)
    p = re.compile(r'^(?P<b_y>\d{4})(?P<b_m>\d{2})(?P<b_d>\d{2})-(?P<e_y>\d{4})(?P<e_m>\d{2})(?P<e_d>\d{2})$')
    m = p.match(date_range)
    # begin date
    begin_y = int(m.group('b_y'))
    # remove '^0'; avoid the '0???' octal number
    begin_m = int(re.sub(r'^0', '', m.group('b_m')))
    begin_d = int(re.sub(r'^0', '', m.group('b_d')))
    # end date
    end_y = int(m.group('b_y'))
    end_m = int(re.sub(r'^0', '', m.group('e_m')))
    end_d = int(re.sub(r'^0', '', m.group('e_d')))
    # date
    begin = datetime.date(begin_y, begin_m, begin_d)
    end = datetime.date(end_y, end_m, end_d)
    data = get_record(request.user.id, indicator_id=indicator_id,
            begin=begin, end=end, **kwargs)
    return HttpResponse("%s" % data)
# }}}



###########################################################
###### forms ######

## add_edit_category                                        # {{{
@login_required
def add_edit_category(request, category_id=None, template='indicator/simple.html'):
    """
    add/edit category: 'models.IndicatorCategory'
    for 'staff' or 'normal user'
    """
    # get or create model instance
    if category_id:
        category_id = int(category_id)
        category = get_object_or_404(im.IndicatorCategory,
                id=category_id)
        action = 'Edit'
        # check the user
        # 'staff' can edit all data;
        # normal users can only edit their own.
        if category.addByUser != request.user and (
                not request.user.is_staff):
            return HttpResponseForbidden()
    else:
        category = im.IndicatorCategory(addByUser=request.user)
        action = 'Add'

    if request.method == 'POST':
        form = IndicatorCategoryForm(request.POST, instance=category)
        if form.is_valid():
            # form posted and valid
            # save form to create/update the model instance
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with data of the specified instance
        form = IndicatorCategoryForm(instance=category)

    return render(request, template, {
        'object': 'IndicatorCategory',
        'action': action,
        'form': form,
    })
# }}}


# add_edit_indicator                                        # {{{
@login_required
def add_edit_indicator(request, indicator_id=None, template='indicator/simple.html'):
    """
    add/edit indicator: 'models.Indicator'
    for 'staff' or 'normal user'
    """
    if indicator_id:
        indicator_id = int(indicator_id)
        indicator = get_object_or_404(im.Indicator,
                id=indicator_id)
        action = 'Edit'
        # check the user
        # 'staff' can edit all data;
        # normal users can only edit their own.
        if indicator.addByUser != request.user and (
                not request.user.is_staff):
            return HttpResponseForbidden()
    else:
        indicator = im.Indicator(addByUser=request.user)
        action = 'Add'

    if request.method == 'POST':
        form = IndicatorForm(request.POST, instance=indicator)
        if form.is_valid():
            # form posted and valid
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with instance
        form = IndicatorForm(instance=indicator)

    return render(request, template, {
        'object': 'Indicator',
        'action': action,
        'form': form,
    })
# }}}


## add_edit_unit {{{
@login_required
def add_edit_unit(request, unit_id=None, template='indicator/simple.html'):
    """
    add unit for indicator
    """
    if unit_id:
        unit_id = int(unit_id)
        unit = get_object_or_404(im.Unit, id=unit_id)
        action = 'Edit'
        # check the user
        # 'staff' can edit all data;
        # normal users can only edit their own.
        if unit.addByUser != request.user and (
                not request.user.is_staff):
            return HttpResponseForbidden()
    else:
        unit = im.Unit(addByUser=request.user)
        action = 'Add'

    if request.method == 'POST':
        form = UnitForm(request.POST, instance=unit)
        if form.is_valid():
            # form posted and valid
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with instance
        form = UnitForm(instance=unit)

    return render(request, template, {
        'object': 'Unit',
        'action': action,
        'form': form,
    })
# }}}


## add_edit_confine {{{
@login_required
def add_edit_confine(request, confine_id=None, template='indicator/simple.html'):
    """
    InnateConfine
    add confines for indicator
    """
    if confine_id:
        confine_id = int(confine_id)
        confine = get_object_or_404(im.InnateConfine, id=confine_id)
        action = 'Edit'
        # check the user
        # 'staff' can edit all data;
        # normal users can only edit their own.
        if confine.addByUser != request.user and (
                not request.user.is_staff):
            return HttpResponseForbidden()
    else:
        confine = im.InnateConfine(addByUser=request.user)
        action = 'Add'

    if request.method == 'POST':
        form = InnateConfineForm(request.POST, instance=confine)
        if form.is_valid():
            # form posted and valid
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with instance
        form = InnateConfineForm(instance=confine)

    return render(request, template, {
        'object': 'InnateConfine',
        'action': action,
        'form': form,
    })
# }}}


## add_edit_record {{{
@login_required
def add_edit_record(request, record_id=None, template='indicator/simple.html'):
    """
    add/edit 'IndicatorRecord'

    staff 能自由地修改所有的记录,并且无需填写"修改原因";
    普通用户只能修改自己的记录,而且必须填写"修改原因" -> RecordHistory

    TODO:
    * 当用户选择好"indicator"后,重新筛选"unit",只提供与"indicator"
      对应的"unit"供选择;
    * 对"普通用户"增加限制,修改数据"记录"时必须同时提交"修改原因",
      对应模型"RecordHistory"。
    """
    if record_id:
        record_id = int(record_id)
        record = get_object_or_404(im.IndicatorRecord, id=record_id)
        action = 'Edit'
        # check the user
        if request.user.is_staff:
            # 'staff' can edit all data;
            pass
        elif request.user == record.user:
            # user modify the record
            return HttpResponse("Not finished yet ...")
            #return modify_record(request, record_id)
        else:
            return HttpResponseForbidden()
    else:
        record = im.IndicatorRecord(user=request.user)
        action = 'Add'

    if request.method == 'POST':
        form = IndicatorRecordForm(request.POST, instance=record)
        if form.is_valid():
            #raise ValueError
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with instance
        form = IndicatorRecordForm(instance=record)

    return render(request, template, {
        'object': 'IndicatorRecord',
        'action': action,
        'form': form,
    })
# }}}


## modify_record {{{
@login_required
def modify_record(request, record_id=None, template='indicator/simple.html'):
    """
    modify an existing IndicatorRecord

    TODO:
    a new 'RecordHistory' is added to record the modification reason
    and backup the original data
    """
    if record_id:
        record_id = int(record_id)
        record = get_object_or_404(im.IndicatorRecord, id=record_id)
        action = 'Edit'
        # check the user
        if request.user.is_staff:
            # 'staff' can edit all data;
            return add_edit_record(request, record_id)
        elif request.user == record.user:
            # user modify the record
            action = 'Modify'
            pass
        else:
            return HttpResponseForbidden()
    else:
        return add_edit_record(request)

    if request.method == 'POST':
        form = IndicatorRecordForm(request.POST, instance=record)
        if form.is_valid():
            # form posted and valid
            # TODO
            raise ValueError(u"该功能尚未完整实现")
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with instance
        form = IndicatorRecordForm(instance=record)

    return render(request, template, {
        'object': 'IndicatorRecord',
        'action': action,
        'form': form,
    })
## }}}


## add_recordhistory {{{
@login_required
def add_recordhistory(request, record_id, template='indicator/simple.html'):
    """
    add 'RecordHistory' for a record by given

    'staff' should use the 'admin' interface.
    """
    record_id = int(record_id)
    record = get_object_or_404(im.IndicatorRecord, id=record_id)
    recordhistory = im.RecordHistory(indicatorRecord=record)
    action = 'Add'
    # check the user
    if request.user != record.user:
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = RecordHistoryForm(request.POST, instance=recordhistory)
        if form.is_valid():
            # form posted and valid
            form.save()
            # redirect url, avoid page reload/refresh
            return HttpResponseRedirect('/indicator/done/')
    else:
        # form with instance
        form = RecordHistoryForm(instance=recordhistory)

    return render(request, template, {
        'object': 'RecordHistory',
        'action': action,
        'form': form,
    })
# }}}


###########################################################
###### indicator UI pages ######
# indicator/index.html {{{
@login_required
def indicator_index(request):
    """
    index page for indicator
    """
    template = 'indicator/index.html'
    return render(request, template)
# }}}


# indicator/SideBar.html {{{
@login_required
def indicator_sidebar(request):
    """
    sidebar page for indicator
    """
    template = 'indicator/SideBar.html'
    return render(request, template)
# }}}


# indicator/SheetDefault.html {{{
@login_required
def indicator_status(request):
    """
    status page for indicator
    add/edit/view indicator data

    TODO:
    * when to recommend indicators
    * how to deal with non-standard units
    """
    template = 'indicator/SheetDefault.html'
    letters = map(chr, range(ord('a'), ord('z')+1))
    # indicators
    indicators = []
    followed_indicators = []
    r_indicators = []

    # get followed indicator, P[inyin] dict format
    followed_indicators_pdict = get_followed_indicator(request.user.id)
    # convert to list
    for l in letters:
        followed_indicators += followed_indicators_pdict[l]

    ## TODO: when to recommend indicators for user ??
    if not followed_indicators:
        # if no followed indicators yet, then recommend 2 indicators
        r_indicators_unsort = [
                im.Indicator.objects.get(id=ri['id']).dump()
                for ri in recommend_indicator(request.user.id, 2)
        ]
        r_indicators= sorted(r_indicators_unsort,
                key = lambda item: item['pinyin'])

    # recommended indicators behind followed ones
    indicators = followed_indicators + r_indicators

    # process 'indicators' list, to add following keys:         # {{{
    #   ref_text:
    #   ref_value:
    #   std_unit_symbol:
    #   record_empty: bool, if has no record, then True
    for ind in indicators:
        ind_obj = get_object_or_404(im.Indicator, id=ind['id'])
        # check if 'indicator.is_ready()'
        if not ind_obj.is_ready():
            raise ValueError(u"Indicator id=%s is NOT ready yet!"
                    % ind_obj.id)
        # the indicator is ready
        dataType = ind_obj.dataType
        confine = ind_obj.get_confine()
        ## set 'ref_text', 'ref_value', 'std_unit_*'
        if dataType in [ind_obj.FLOAT_TYPE, ind_obj.RANGE_TYPE,
                ind_obj.FLOAT_RANGE_TYPE]:
            ind['ref_text'] = u"参考范围"
            # ref_value
            human_max = confine.get('human_max')
            human_min = confine.get('human_min')
            ind['ref_value'] = format_data(ind_obj,
                    val_max=human_max, val_min=human_min)
            # set 'std_unit_*'
            ind['std_unit_name'] = confine.get('unit').get('name')
            ind['std_unit_symbol'] = confine.get('unit').get('symbol')
        elif dataType in [ind_obj.INTEGER_TYPE, ind_obj.PM_TYPE]:
            ind['ref_text'] = u"参考值"
            # ref_value
            val_norm = confine.get('val_norm')
            ind['ref_value'] = format_data(ind_obj, value=val_norm)
            # std_unit
            ind['std_unit_name'] = u""
            ind['std_unit_symbol'] = u""
        else:
            ind['ref_text'] = u"参考"
            ind['ref_value'] = None
            ind['std_unit_name'] = None
            ind['std_unit_symbol'] = None
        ## check record of indicator
        records = ind_obj.indicator_records.filter(user=request.user).\
                order_by('-date', '-updated_at')
        if records:
            ind['record_empty'] = False
            # last record of the indicator
            last_record = records[0]
            if dataType in [ind_obj.INTEGER_TYPE, ind_obj.PM_TYPE,
                    ind_obj.FLOAT_TYPE]:
                value_str = format_data(ind_obj, value=last_record.value)
            elif dataType == ind_obj.RANGE_TYPE:
                value_str = format_data(ind_obj,
                        val_max=last_record.val_max,
                        val_min=last_record.val_min)
            elif dataType == ind_obj.FLOAT_RANGE_TYPE:
                value = last_record.value
                val_max = last_record.val_max
                val_min = last_record.val_min
                if value is not None:
                    value_str = format_data(ind_obj, value=value)
                elif (val_max is not None) and (val_min is not None):
                    value_str = format_data(ind_obj,
                            val_max=val_max, val_min=val_min)
                else:
                    value_str = u''
            else:
                # unknow
                value_str = u''
            # save to dict
            ind['last_record'] = {
                'date': last_record.date.isoformat(),
                'value_str': value_str,
            }
        else:
            ind['record_empty'] = True
            ind['last_record'] = {}
    # }}}

    data = {
        'indicators': indicators,
    }
    # render template
    #raise ValueError
    return render(request, template, data)
# }}}


# indicator/NewDeleteIndex.html {{{
@login_required
def indicator_fanduf(request):
    """
    follow/unfollow indicator

    Note:
    * when 'page_condition == all',
      indicators -> indicators_pdict, in 'P[inyin] dict' format
    * other 'page_condition', 'indicators' a object list
    """
    template = 'indicator/NewDeleteIndex.html'
    letters = map(chr, range(ord('a'), ord('z')+1))

    # get 7 categories (page can only contains 1+7 categories)
    categories = im.IndicatorCategory.objects.all().\
            order_by('id')[:7]
    # set default value for 'selected_cat*'
    selected_catid = None
    selected_category = None
    # default parameters
    indicators = None
    search_kw_empty = False
    search_result_empty = False

    # get/post views
    if request.method == 'GET':
        # default page_condition: "all"
        selected_catid = "all"
        page_condition = "all"
        # page_condition: "category"
        # select category / search indicator
        if 'tab' in request.GET:
            # tab: selected category, default "all"
            selected_catid = request.GET.get('tab')
            if selected_catid == "all" or selected_catid == "":
                page_condition = "all"
            else:
                selected_catid = int(selected_catid)
                selected_category = get_object_or_404(
                        im.IndicatorCategory, id=selected_catid)
                page_condition = "category"
                # get indicators of the category
                indicators = selected_category.indicators.\
                        all().order_by('pinyin')
        # page_condition: "search"
        # can override the above 'category' if 'tab' & 'kw' both exist
        if 'kw' in request.GET:
            # kw: search keyword to find indicator
            search_kw = request.GET.get('kw')
            page_condition = "search"
            selected_catid = None
            # check search keyword
            if search_kw == "":
                search_kw_empty = True
            else:
                # search
                # TODO: howto order_by() by 'pinyin'
                sqs = SearchQuerySet().models(im.Indicator).\
                        filter(content=search_kw)
                if sqs:
                    # search result not empty
                    inds_unsort = [ind.dump()
                            for ind in objects_of_sqs(sqs)]
                    indicators = sorted(inds_unsort,
                            key = lambda item: item['pinyin'])
                else:
                    search_result_empty = True
    elif request.method == 'POST':
        # posted data of followed indicators
        # TODO
        post = request.POST
        raise ValueError(u"TODO")
    else:
        # XXX
        raise Http404

    # all indicators
    if page_condition == "all":
        # get indicators, P[inyin] dict format
        indicators = get_indicator()

    # get followed indicator, P[inyin] dict format
    followed_indicators_pdict = get_followed_indicator(request.user.id)
    # convert to list
    followed_indicators = []
    for l in letters:
        followed_indicators += followed_indicators_pdict[l]

    data = {
        'page_condition': page_condition,
        'categories': categories,
        'selected_category': selected_category,
        'selected_catid': selected_catid,
        'letters': letters,
        'indicators': indicators,
        'followed_indicators': followed_indicators,
        'search_kw_empty': search_kw_empty,
        'search_result_empty': search_result_empty,
    }
    # render page
    return render(request, template, data)
# }}}


## popup pages
# indicator/popup/DeleteCardTip.html {{{
@login_required
def indicator_deletecardtip(request):
    """
    prompted tip for deleting a card
    """
    template = 'indicator/popup/DeleteCardTip.html'
    return render(request, template)
# }}}


# indicator/popup/EditHistoryData.html {{{
@login_required
def indicator_edithistorydata(request):
    """
    popup page to edit history data for an indicator
    """
    template = 'indicator/popup/EditHistoryData.html'
    return render(request, template)
# }}}


# indicator/popup/IndexDesc.html {{{
@login_required
def indicator_indexdesc(request):
    """
    description for an indicator
    """
    template = 'indicator/popup/IndexDesc.html'
    return render(request, template)
# }}}


###########################################################
###### ajax ######
# ajax_act_index {{{
@login_required
def ajax_act_index(request):
    """
    index action (add/minus)
    follow/unfollow indicator
    """
    # default 'fail'
    result = 'fail'
    #if request.is_ajax():
    if True:
        # check index_id -> indicator_id
        if request.GET.get('index_id') is not None:
            index_id = request.GET.get('index_id')
            try:
                indicator_id = int(index_id)
            except ValueError:
                print u'Error: Given index_id="%s" cannot convert to integer' % indicator_id
                result = 'fail'
                return HttpResponse(result)
        # check 'act': add/minus -> action: follow/unfollow
        if request.GET.get('act') is not None:
            action = request.GET.get('act')
            if action == "add":
                # follow
                if follow_indicator(request.user.id, indicator_id):
                    result = 'success'
            elif action == "minus":
                # unfollow
                if unfollow_indicator(request.user.id, indicator_id):
                    result = 'success'
            else:
                raise ValueError(u'Error: Given act="%s" unknown' % action)
                result = 'fail'

    return HttpResponse(result)
# }}}


# ajax_close_sub_title {{{
def ajax_close_sub_title(request):
    """
    close the small prompt banner above the indicator cards

    'indicator/static/javascripts/sheetdefault.js'
    """
    if request.is_ajax():
        result = 'success'
    else:
        result = 'fail'
        #raise Http404
    return HttpResponse(result)
# }}}


# ajax_edit_history_data {{{
@login_required
def ajax_edit_history_data(request):
    """
    edit history data
    used in 'detail history' view card
    """
    if request.is_ajax():
        result = 'success'
    else:
        result = 'fail'
        #raise Http404
    return HttpResponse(result)
# }}}


# ajax_get_card_data_chart {{{
@login_required
def ajax_get_card_data_chart(request):
    """
    'indicator/static/javascripts/load_card.js'
    get card data
    for the 'chart' within the card
    format: [v1, v2, v3, ...]

    NB.
    每一天都要有数据,否则时间轴对不上 (load_card.js: redraw_chard())
    TODO:
    workaround for the above problem!
    """
    # TODO
    if request.is_ajax():
        result = [6.0, 5.9, 5.5, 4.5, 6.2, 6.5, 5.2, 6.0,
                  5.9, 5.5, 4.5, 6.2, 6.5, 5.2, 6.0, 5.9,
                  5.5, 4.5, 6.2, 6.5]
    else:
        result = ''
        #raise Http404
    return HttpResponse(json.dumps(result),
            mimetype='application/json')
# }}}


# ajax_get_card_data_table {{{
@login_required
def ajax_get_card_data_table(request):
    """
    get card data
    for used in 'detail data card'
    format:
    <tr><td>yyyy-mm-dd</td><td>hh:mm</td><td>value unit</td></tr>
    """
    # TODO
    if request.is_ajax():
        result = """
            <tr><td>2013-08-10</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-09</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-08</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-08</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-07</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-06</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-05</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-04</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-03</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-02</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-08-01</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-07-31</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            <tr><td>2013-07-30</td><td>11:20</td><td>100x10^4拷贝/mL</td></tr>
            """
    else:
        result = ''
        #raise Http404
    return HttpResponse(result)
# }}}


# ajax_unfollow_indicator {{{
@login_required
def ajax_unfollow_indicator(request):
    """
    respone to the ajax request from 'delete_card_tip.js'
    unfollow the specified indicator: GET.indicator_id
    """
    # default 'fail'
    result = 'fail'
    if request.is_ajax():
        if request.GET.get('indicator_id') is not None:
            indicator_id = request.GET.get('indicator_id')
            try:
                indicator_id = int(indicator_id)
            except ValueError:
                print u'Error: Given indicator_id="%s" cannot convert to integer' % indicator_id
                result = 'fail'
            if unfollow_indicator(request.user.id, indicator_id):
                result = 'success'

    # return result
    return HttpResponse(result)
# }}}


###########################################################

### test_view ###
def test_view(request, **kwargs):
    """
    test view
    'indicator/templates/indicator/test.html'
    """
    template = 'indicator/test.html'
    all_letters = map(chr, range(ord('a'), ord('z')+1))
    all_indicators = get_indicator()
    followed_indicators_pdict = get_followed_indicator(request.user.id)
    # convert to list
    followed_indicators = []
    for l in all_letters:
        followed_indicators += followed_indicators_pdict[l]

    list = []

    data = {
        'all_letters': all_letters,
        'all_indicators': all_indicators,
        'followed_indicators': followed_indicators,
        'list': list,
    }
    return render(request, template, data)