Package omeroweb :: Package webclient :: Package controller :: Module history
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webclient.controller.history

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  #  
  4  #  
  5  #  
  6  # Copyright (c) 2008-2011 University of Dundee. 
  7  #  
  8  # This program is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU Affero General Public License as 
 10  # published by the Free Software Foundation, either version 3 of the 
 11  # License, or (at your option) any later version. 
 12  #  
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU Affero General Public License for more details. 
 17  #  
 18  # You should have received a copy of the GNU Affero General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20  #  
 21  # Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2008. 
 22  #  
 23  # Version: 1.0 
 24  # 
 25   
 26  import calendar 
 27  import datetime 
 28  import time 
 29   
 30  from django.conf import settings 
 31  from django.core.urlresolvers import reverse 
 32   
 33  from webclient.controller import BaseController 
 34   
 35   
36 -class BaseCalendar(BaseController):
37 38 day = None 39 month = None 40 year = None 41 next_month = None 42 next_month_name = None 43 last_month = None 44 last_month_name = None 45 next_year = None 46 last_year = None 47
48 - def __init__(self, conn, year=None, month=None, day=None, eid=None, **kw):
49 BaseController.__init__(self, conn) 50 self.year = int(year) 51 self.month = int(month) 52 if eid is None: 53 self.eid = self.conn.getEventContext().userId 54 else: 55 self.eid = eid 56 57 if day: 58 self.day = int(day) 59 # only for python 2.5 60 # date = datetime.datetime.strptime(("%i-%i-%i" % (self.year, self.month, self.day)), "%Y-%m-%d") 61 date = datetime.datetime(*(time.strptime(("%i-%i-%i" % (self.year, self.month, self.day)), "%Y-%m-%d")[0:6])) 62 self.displayDate = '%s %s' % (date.strftime("%A, %d"), date.strftime("%B %Y")) 63 self.nameday = date.strftime("%A") 64 else: 65 # only for python 2.5 66 # date = datetime.datetime.strptime(("%i-%i" % (self.year, self.month)), "%Y-%m") 67 date = datetime.datetime(*(time.strptime(("%i-%i" % (self.year, self.month)), "%Y-%m")[0:6]))
68
69 - def create_calendar(self):
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
151 - def calendar_items(self, month, monthrange):
152 if month < 10: 153 mn = '0%i' % month 154 else: 155 mn = month 156 # only for python 2.5 157 # d1 = datetime.datetime.strptime(("%i-%s-01 00:00:00" % (self.year, mn)), "%Y-%m-%d %H:%M:%S") 158 # d2 = datetime.datetime.strptime(("%i-%s-%i 23:59:59" % (self.year, mn, monthrange)), "%Y-%m-%d %H:%M:%S") 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
175 - def month_range(self, year, month):
176 if month == 12: 177 year += 1 178 month = 1 179 else: 180 month += 1 181 return (datetime.date(year, month, 1), datetime.date(year, month, 1)-datetime.timedelta(days=1))
182
183 - def get_items(self, 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 # only for python 2.5 194 # d1 = datetime.datetime.strptime(('%i-%s-%s 00:00:00' % (self.year, mn, dy)), "%Y-%m-%d %H:%M:%S") 195 # d2 = datetime.datetime.strptime(('%i-%s-%s 23:59:59' % (self.year, mn, dy)), "%Y-%m-%d %H:%M:%S") 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 obj_logs = self.conn.getDataByPeriod(start=start, end=end, eid=self.eid, page=page) 208 obj_logs_counter = self.conn.countDataByPeriod(start, end, self.eid) 209 if len(obj_logs['image']) > 0 or len(obj_logs['dataset']) > 0 or len(obj_logs['project']) > 0: 210 self.day_items.append({'project':obj_logs['project'], 'dataset':obj_logs['dataset'], 'image':obj_logs['image']}) 211 self.day_items_size = len(obj_logs['project'])+len(obj_logs['dataset'])+len(obj_logs['image']) 212 self.paging = self.doPaging(page, self.day_items_size, obj_logs_counter)
213