Skip to content
This repository
Browse code

Refactoring: extract statistics model

Extract class WeeklyStatisticsModel from statistics module,
which do the actual data loading and caching.
  • Loading branch information...
commit 3f67dfbedea05214f87a0c155754b101f059cc87 1 parent e58dec3
Jerry authored March 09, 2012
34  tomate/statistics.py
@@ -29,44 +29,40 @@
29 29
 
30 30
 from datetime import datetime, date, timedelta, time
31 31
 
32  
-from tomate import model
  32
+from tomate.uimodel import WeeklyStatisticsModel
33 33
 
34 34
 class StatisticsView(gtk.VBox):
35 35
     """Statistics View"""
36 36
     def __init__(self, parent_window):
37 37
         super(StatisticsView, self).__init__(False, 0)
38 38
         self.parent_window = parent_window
39  
-        self.store = model.open_store()
40  
-        self.connect('destroy', lambda arg : self.store.close())
41  
-        self.graph = WeeklyGraph(self.store)
  39
+        self.stat_model = WeeklyStatisticsModel()
  40
+        self.graph = WeeklyGraph(self.stat_model)
42 41
         padding = 1
43 42
         hbox = gtk.HBox(False, 0)
44 43
         hbox.pack_start(self.graph, True, True, padding=padding)
45 44
         self.pack_start(hbox, True, True, padding=padding)
46 45
 
47 46
     def refresh(self):
48  
-        pass
  47
+        self.graph.refresh()
49 48
 
50 49
 
51 50
 class WeeklyGraph(gtk.DrawingArea):
52 51
 
53 52
     __gsignals__ = { "expose-event": "override" }
54 53
 
55  
-    def __init__(self, store):
  54
+    def __init__(self, model):
56 55
         super(WeeklyGraph, self).__init__()
57  
-        self.store = store
  56
+        self.model = model
58 57
         self.set_size_request(500, 400)
59  
-        self.load_data(date.today())
60  
-
61  
-    def load_data(self, day):
62  
-        weekday = day.weekday()
63  
-        start_day = day - timedelta(days=weekday)
64  
-        end_day = day + timedelta(days=6-weekday-1)
65  
-        start_time = datetime.combine(start_day, time(0, 0, 0))
66  
-        end_time = datetime.combine(end_day, time(23, 59, 59))
67  
-        data = self.store.statistics_tomato_count(start_time, end_time)
68  
-        self.data = [(datetime.fromtimestamp(t).strftime('%A'), d)
69  
-                for t, d in data]
  58
+        self.date = date.today()
  59
+
  60
+    def select_day(self, date):
  61
+        self.date = date
  62
+        self.refresh()
  63
+
  64
+    def refresh(self):
  65
+        self.model.reload_data(self.date)
70 66
 
71 67
     def do_expose_event(self, event):
72 68
         cr = self.window.cairo_create()
@@ -76,7 +72,7 @@ def do_expose_event(self, event):
76 72
         self.draw(cr, *self.window.get_size())
77 73
 
78 74
     def draw(self, cr, width, height):
79  
-        renderer = BarGraphRenderer(cr, width, height, self.data)
  75
+        renderer = BarGraphRenderer(cr, width, height, self.model.get_data())
80 76
         renderer.render()
81 77
 
82 78
 
41  tomate/uimodel.py
@@ -40,10 +40,12 @@ class ActivityStore(gtk.ListStore):
40 40
      INTERRUPT_COL,
41 41
      ACTIVITY_COL) = range(5)
42 42
 
43  
-    def __init__(self, priority=model.TODO):
  43
+    def __init__(self, priority=model.TODO, store=None):
44 44
         super(ActivityStore, self).__init__(str, bool, int, int, gobject.TYPE_PYOBJECT)
45 45
         self.priority = priority
46  
-        self.store = model.open_store()
  46
+        if not store:
  47
+            store = model.open_store()
  48
+        self.store = store
47 49
 
48 50
     def _append_activity(self, act):
49 51
         self.append([act.name,
@@ -112,14 +114,17 @@ def day_range(date):
112 114
     end_time = start_time + timedelta(days=1)
113 115
     return (start_time, end_time)
114 116
 
  117
+
115 118
 class FinishedTomatoModel(gtk.ListStore):
116 119
     (TIME_COL,
117 120
      ACTIVITY_COL,
118 121
      INTERRUPTED_COL) = range(3)
119 122
 
120  
-    def __init__(self):
  123
+    def __init__(self, store=None):
121 124
         super(FinishedTomatoModel, self).__init__(str, str, bool)
122  
-        self.store = model.open_store()
  125
+        if not store:
  126
+            store = model.open_store()
  127
+        self.store = store
123 128
 
124 129
     def load_finished_tomatoes(self, date):
125 130
         start_time, end_time = day_range(date)
@@ -138,9 +143,11 @@ def load_finished_tomatoes(self, date):
138 143
 
139 144
 class FinishedActivityModel(gtk.ListStore):
140 145
     """List model for Activity history"""
141  
-    def __init__(self):
  146
+    def __init__(self, store=None):
142 147
         super(FinishedActivityModel, self).__init__(bool, str)
143  
-        self.store = model.open_store()
  148
+        if not store:
  149
+            store = model.open_store()
  150
+        self.store = store
144 151
 
145 152
     def load_finished_activities(self, date):
146 153
         start_time, end_time = day_range(date)
@@ -151,3 +158,25 @@ def load_finished_activities(self, date):
151 158
             self.append((True, act.name))
152 159
         return len(act_histories) + len(acts)
153 160
 
  161
+
  162
+class WeeklyStatisticsModel(object):
  163
+    """Weekly statistics model"""
  164
+    def __init__(self, store=None):
  165
+        super(WeeklyStatisticsModel, self).__init__()
  166
+        if not store:
  167
+            store = model.open_store()
  168
+        self.store = store
  169
+        self.data = []
  170
+
  171
+    def get_data(self):
  172
+        return self.data
  173
+
  174
+    def reload_data(self, date):
  175
+        weekday = date.weekday()
  176
+        start_day = date - timedelta(days=weekday)
  177
+        end_day = date + timedelta(days=6-weekday-1)
  178
+        start_time = datetime.combine(start_day, time(0, 0, 0))
  179
+        end_time = datetime.combine(end_day, time(23, 59, 59))
  180
+        data = self.store.statistics_tomato_count(start_time, end_time)
  181
+        self.data = [(datetime.fromtimestamp(t).strftime('%A'), d)
  182
+                for t, d in data]

0 notes on commit 3f67dfb

Please sign in to comment.
Something went wrong with that request. Please try again.