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 import calendar
26 import datetime
27 import time
28
29 from django.conf import settings
30 from django.core.urlresolvers import reverse
31
32 from webclient.controller import BaseController
33
34
36
37 day = None
38 month = None
39 year = None
40 next_month = None
41 next_month_name = None
42 last_month = None
43 last_month_name = None
44 next_year = None
45 last_year = None
46
47 - def __init__(self, conn, year=None, month=None, day=None, eid=None, **kw):
48 BaseController.__init__(self, conn)
49 self.year = int(year)
50 self.month = int(month)
51 if eid is None:
52 self.eid = self.conn.getEventContext().userId
53 else:
54 self.eid = eid
55
56 if day:
57 self.day = int(day)
58
59
60 date = datetime.datetime(*(time.strptime(("%i-%i-%i" % (self.year, self.month, self.day)), "%Y-%m-%d")[0:6]))
61 self.eContext['breadcrumb'] = ['<a href="%s">History</a>' % reverse(viewname="load_history", args=[self.year, self.month, self.day]), '%s %s' % (date.strftime("%A, %d"), date.strftime("%B %Y"))]
62 self.nameday = date.strftime("%A")
63 else:
64
65
66 date = datetime.datetime(*(time.strptime(("%i-%i" % (self.year, self.month)), "%Y-%m")[0:6]))
67 self.eContext['breadcrumb'] = ["History"]
68
70 calendar.setfirstweekday(settings.FIRST_DAY_OF_WEEK)
71 now = datetime.datetime(self.year, self.month, 1)
72
73 if self.month == 12:
74 self.next_month = now.replace(year=now.year+1, month=1)
75 self.next_year = self.year+1
76 else:
77 self.next_month = now.replace(month=now.month+1)
78 self.next_year = self.year
79
80 if self.month == 1:
81 self.last_month = now.replace(year=self.year-1, month=12)
82 self.last_year = self.year-1
83 else:
84 self.last_month = now.replace(month=now.month-1)
85 self.last_year = self.year
86
87 next_month_name = self.next_month.strftime('%B')
88 last_month_name = self.last_month.strftime('%B')
89
90 self.week_day_labels = [x for x in calendar.weekheader(5).split(' ') if x != '']
91 self.current_month = datetime.datetime(self.year, self.month, 1)
92 self.month_name = calendar.month_name[self.month]
93
94 if self.month == 12:
95 self.next_month = self.current_month.replace(year=self.year+1, month=1)
96 else:
97 self.next_month = self.current_month.replace(month=self.current_month.month+1)
98
99 self.next_month_name = self.next_month.strftime('%B')
100
101 if self.month == 1:
102 self.last_month = self.current_month.replace(year=self.year-1, month=12)
103 else:
104 self.last_month = self.current_month.replace(month=self.current_month.month-1)
105
106 self.last_month_name = self.last_month.strftime('%B')
107
108 self.cal_weeks = calendar.monthcalendar(self.year, self.month)
109 self.monthrange = calendar.monthrange(self.year, self.month)[1]
110
111 self.cal_days = []
112
113 items = self.calendar_items(self.month, self.monthrange)
114
115 for week,day in [(week,day) for week in xrange(0,len(self.cal_weeks)) for day in xrange(0,7)]:
116 imgCounter = dict()
117 rdCounter = dict()
118 dsCounter = dict()
119 prCounter = dict()
120 imgCounter = 0
121 rdCounter = 0
122 dsCounter = 0
123 prCounter = 0
124 d = int(self.cal_weeks[week][day])
125 if d > 0:
126 t_items = {'image':[], 'dataset':[], 'project':[]}
127 for item in items.get(d):
128 if item.get('type') == 'ome.model.core.Image':
129 try:
130 t_items['image'].index(item.get('id'))
131 except:
132 imgCounter += 1
133 t_items['image'].append(item.get('id'))
134 elif item.get('type') == 'ome.model.containers.Dataset':
135 try:
136 t_items['dataset'].index(item.get('id'))
137 except:
138 dsCounter += 1
139 t_items['dataset'].append(item.get('id'))
140 elif item.get('type') == 'ome.model.containers.Project':
141 try:
142 t_items['project'].index(item.get('id'))
143 except:
144 prCounter += 1
145 t_items['project'].append(item.get('id'))
146 self.cal_days.append({'day':self.cal_weeks[week][day], 'counter': {'imgCounter':imgCounter, 'dsCounter':dsCounter, 'prCounter':prCounter }})
147 else:
148 self.cal_days.append({'day':self.cal_weeks[week][day], 'counter': {}})
149 self.cal_weeks[week][day] = {'cell': self.cal_days[-1]}
150
152 if month < 10:
153 mn = '0%i' % month
154 else:
155 mn = month
156
157
158
159 d1 = datetime.datetime(*(time.strptime(("%i-%s-01 00:00:00" % (self.year, mn)), "%Y-%m-%d %H:%M:%S")[0:6]))
160 d2 = datetime.datetime(*(time.strptime(("%i-%s-%i 23:59:59" % (self.year, mn, monthrange)), "%Y-%m-%d %H:%M:%S")[0:6]))
161
162 start = long(time.mktime(d1.timetuple())+1e-6*d1.microsecond)*1000
163 end = long(time.mktime(d2.timetuple())+1e-6*d2.microsecond)*1000
164 all_logs = self.conn.getEventsByPeriod(start, end, self.eid)
165
166 items = dict()
167 for d in xrange(1,monthrange+1):
168 items[d] = list()
169 for i in all_logs:
170 for d in items:
171 if time.gmtime(i.event.time.val / 1000).tm_mday == d:
172 items[d].append({'id':i.entityId.val, 'type': i.entityType.val, 'action': i.action.val})
173 return items
174
182
183 - def get_items(self, cal_type=None, page=None):
184
185 if self.month < 10:
186 mn = '0%i' % self.month
187 else:
188 mn = self.month
189 if self.day < 10:
190 dy = '0%i' % self.day
191 else:
192 dy = self.day
193
194
195
196
197 d1 = datetime.datetime(*(time.strptime(('%i-%s-%s 00:00:00' % (self.year, mn, dy)), "%Y-%m-%d %H:%M:%S")[0:6]))
198 d2 = datetime.datetime(*(time.strptime(('%i-%s-%s 23:59:59' % (self.year, mn, dy)), "%Y-%m-%d %H:%M:%S")[0:6]))
199
200 start = long(time.mktime(d1.timetuple())+1e-6*d1.microsecond)*1000
201 end = long(time.mktime(d2.timetuple())+1e-6*d2.microsecond)*1000
202
203 self.day_items = list()
204 self.day_items_size = 0
205 self.total_items_size = self.conn.countDataByPeriod(start, end, self.eid)
206
207 if cal_type is not None:
208 obj_logs = self.conn.getDataByPeriod(start=start, end=end, eid=self.eid, otype=cal_type, page=page)
209 obj_logs_counter = self.conn.countDataByPeriod(start, end, self.eid, cal_type)
210 if len(obj_logs[cal_type]) > 0 :
211
212 obj_ids = [ob.id for ob in obj_logs[cal_type]]
213 obj_annotation_counter = self.conn.getCollectionCount(cal_type.title(), "annotationLinks", obj_ids)
214
215 obj_list_with_counters = list()
216 for obj in obj_logs[cal_type]:
217 obj.annotation_counter = obj_annotation_counter.get(obj.id)
218 obj_list_with_counters.append(obj)
219
220 self.day_items.append({cal_type:obj_list_with_counters})
221 self.day_items_size = len(obj_list_with_counters)
222
223 self.paging = self.doPaging(page, len(obj_list_with_counters), obj_logs_counter)
224
225 else:
226 obj_logs = self.conn.getDataByPeriod(start, end, self.eid)
227 if len(obj_logs['image']) > 0 or len(obj_logs['dataset']) > 0 or len(obj_logs['project']) > 0:
228
229 pr_list_with_counters = list()
230 ds_list_with_counters = list()
231 im_list_with_counters = list()
232
233 pr_ids = [pr.id for pr in obj_logs['project']]
234 if len(pr_ids) > 0:
235 pr_annotation_counter = self.conn.getCollectionCount("Project", "annotationLinks", pr_ids)
236
237 for pr in obj_logs['project']:
238 pr.annotation_counter = pr_annotation_counter.get(pr.id)
239 pr_list_with_counters.append(pr)
240
241 ds_ids = [ds.id for ds in obj_logs['dataset']]
242 if len(ds_ids) > 0:
243 ds_annotation_counter = self.conn.getCollectionCount("Dataset", "annotationLinks", ds_ids)
244
245 for ds in obj_logs['dataset']:
246 ds.annotation_counter = ds_annotation_counter.get(ds.id)
247 ds_list_with_counters.append(ds)
248
249 im_ids = [im.id for im in obj_logs['image']]
250 if len(im_ids) > 0:
251 im_annotation_counter = self.conn.getCollectionCount("Image", "annotationLinks", im_ids)
252
253 for im in obj_logs['image']:
254 im.annotation_counter = im_annotation_counter.get(im.id)
255 im_list_with_counters.append(im)
256
257 self.day_items.append({'project':pr_list_with_counters, 'dataset':ds_list_with_counters, 'image':im_list_with_counters})
258 self.day_items_size = len(pr_list_with_counters)+len(ds_list_with_counters)+len(im_list_with_counters)
259
260 self.history_type = cal_type
261