Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: UTF-8 -*- 

# Copyright 2016 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

"""Model mixins for summaries 

 

""" 

 

from __future__ import unicode_literals 

from builtins import range 

from builtins import object 

 

from django.db import models 

 

from lino.api import dd, _ 

 

 

class ComputeResults(dd.Action): 

    label = _("Compute results") 

    icon_name = 'lightning' 

    readonly = False 

 

    def run_from_ui(self, ar, **kw): 

        for obj in ar.selected_rows: 

            obj.compute_summary_values() 

        ar.success(refresh=True) 

 

 

class UpdateSummary(dd.Action): 

    icon_name = 'bell' 

    label = _("Update summary") 

    sort_index = 90 

 

    def run_from_ui(self, ar): 

        # print(20150327, ar.selected_rows) 

        for obj in ar.selected_rows: 

            obj.update_ 

        ar.set_response(refresh_all=True) 

 

 

class UpdateSummariesByMaster(dd.Action): 

    """ 

    """ 

    icon_name = 'bell' 

    label = _("Update summary data for this object") 

 

    def __init__(self, master_model, summary_models): 

        self.master_model = master_model 

        self.summary_models = summary_models 

        super(UpdateSummariesByMaster, self).__init__() 

 

    def run_from_ui(self, ar, fix=None): 

        for master in ar.selected_rows: 

            assert isinstance(master, self.master_model) 

            for sm in self.summary_models: 

                sm.update_for_master(master) 

        ar.set_response(refresh=True) 

 

 

class Summary(dd.Model): 

 

    class Meta(object): 

        abstract = True 

 

    # summary_period = 'yearly' 

    summary_period = 'monthly' 

 

    year = models.IntegerField(_("Year")) 

    month = models.IntegerField(_("Month"), null=True, blank=True) 

 

    compute_results = ComputeResults() 

 

    @classmethod 

    def get_summary_periods(cls): 

        config = dd.plugins.summaries 

        for year in range(config.start_year, config.end_year): 

            if cls.summary_period == 'yearly': 

                yield year, None 

            else: 

                for month in range(1, 12): 

                    yield year, month 

 

    @classmethod 

    def get_for_period(cls, master, year, month): 

        qs = cls.objects.filter(master=master, year=year, month=month) 

        count = qs.count() 

        if count > 1: 

            qs.delete() 

            count = 0 

        if count == 0: 

            return cls(master=master, year=year, month=month) 

        return qs[0] 

 

    @classmethod 

    def get_summary_master_model(cls): 

        raise NotImplementedError() 

 

    @classmethod 

    def get_summary_masters(cls): 

        return cls.get_summary_master_model().objects.all() 

 

    @classmethod 

    def update_for_master(cls, master): 

        for year, month in cls.get_summary_periods(): 

            obj = cls.get_for_period(master, year, month) 

            obj.compute_summary_values() 

 

    def get_summary_querysets(self): 

        return [] 

 

    def compute_summary_values(self): 

        for collector, qs in self.get_summary_collectors(): 

            for obj in qs: 

                collector(obj) 

 

        self.full_clean() 

        self.save() 

 

    def add_date_filter(self, qs, fldname, **kwargs): 

        kwargs[fldname+'__year'] = self.year 

        if self.month is not None: 

            kwargs[fldname+'__month'] = self.month 

        return qs.filter(**kwargs)