import calendar calendar.setfirstweekday(0) #start day Mon(0)-Sun(6) from Products.CMFPlone.CalendarTool import CalendarTool as PloneCalTool from Products.CMFPlone import ToolNames from AccessControl import ClassSecurityInfo from Globals import InitializeClass from Products.CMFPlone.PloneBaseTool import PloneBaseTool from AccessControl import getSecurityManager import traceback class CalendarTool(PloneCalTool): #meta_type = ToolNames.CalendarTool meta_type = 'CalendarTool' security = ClassSecurityInfo() toolicon = 'skins/plone_images/event_icon.gif' security.declarePublic('catalog_getevents2') def catalog_getdays_withevents(self, year, month): """ given a year and month return a list of days that have events """ import calendar from AccessControl import getSecurityManager user = getSecurityManager().getUser() year=int(year) month=int(month) last_day=calendar.monthrange(year, month)[1] first_date=self.getBeginAndEndTimes(1, month, year)[0] last_date=self.getBeginAndEndTimes(last_day, month, year)[1] query = self.portal_catalog( #portal_type=self.getCalendarTypes(), #review_state=self.getCalendarStates(), portal_type=('BlogEntry',), start={'query': last_date, 'range': 'max'}, end={'query': first_date, 'range': 'min'}, sort_on='start' ) # compile a list of the days that have events eventDays={} for daynumber in range(1, 32): # 1 to 31 eventDays[daynumber] = {'eventslist':[], 'event':0, 'day':daynumber} includedevents = [] for result in query: if result.getRID() in includedevents: break else: includedevents.append(result.getRID()) event={} # we need to deal with events that end next month if result.end.month() != month: # doesn't work for events that last ~12 months # fix it if it's a problem, otherwise ignore eventEndDay = last_day event['end'] = None else: eventEndDay = result.end.day() event['end'] = result.end.Time() # and events that started last month if result.start.month() != month: # same as above re: 12 month thing eventStartDay = 1 event['start'] = None else: eventStartDay = result.start.day() event['start'] = result.start.Time() event['title'] = result.Title or result.getId if eventStartDay != eventEndDay: allEventDays = range(eventStartDay, eventEndDay+1) eventDays[eventStartDay]['eventslist'].append( {'end': None, 'start': result.start.Time(), 'title': event['title']} ) eventDays[eventStartDay]['event'] = 1 for eventday in allEventDays[1:-1]: eventDays[eventday]['eventslist'].append( {'end': None, 'start': None, 'title': event['title']} ) eventDays[eventday]['event'] = 1 if result.end == result.end.earliestTime(): last_day_data = eventDays[allEventDays[-2]] last_days_event = last_day_data['eventslist'][-1] last_days_event['end'] = (result.end-1).latestTime().Time() else: eventDays[eventEndDay]['eventslist'].append( {'end': result.end.Time(), 'start': None, 'title': event['title']} ) eventDays[eventEndDay]['event'] = 1 else: eventDays[eventStartDay]['eventslist'].append(event) eventDays[eventStartDay]['event'] = 1 DaysWithEvent=[] for day in eventDays: dEvent = eventDays[day]['event'] if dEvent == 1: DaysWithEvent.append(day) return DaysWithEvent security.declarePublic('catalog_getevents') def catalog_getevents(self, year, month): """ given a year and month return a list of days that have events """ year=int(year) month=int(month) last_day=calendar.monthrange(year, month)[1] first_date=self.getBeginAndEndTimes(1, month, year)[0] last_date=self.getBeginAndEndTimes(last_day, month, year)[1] query = [] user = getSecurityManager().getUser() if str(user) != 'Anonymous User': query = self.portal_catalog( #portal_type=self.getCalendarTypes(), #review_state=self.getCalendarStates(), portal_type=('BlogEntry',), start={'query': last_date, 'range': 'max'}, end={'query': first_date, 'range': 'min'}, Creator=(str(getSecurityManager().getUser()),), sort_on='start' ) groups = user.getGroups() groups.append(str(user)) print "Groups", groups #traceback.print_stack() query = query + self.portal_catalog( #portal_type=self.getCalendarTypes(), #review_state=self.getCalendarStates(), portal_type=('StructuredTask', 'ReflectionTask',), start={'query': last_date, 'range': 'max'}, end={'query': first_date, 'range': 'min'}, getTarget=groups, sort_on='start' ) # compile a list of the days that have events eventDays={} for daynumber in range(1, 32): # 1 to 31 eventDays[daynumber] = {'eventslist':[], 'event':0, 'day':daynumber} includedevents = [] for result in query: if result.getRID() in includedevents: break else: includedevents.append(result.getRID()) event={} # we need to deal with events that end next month if result.end.month() != month: # doesn't work for events that last ~12 months # fix it if it's a problem, otherwise ignore eventEndDay = last_day event['end'] = None else: eventEndDay = result.end.day() event['end'] = result.end.Time() # and events that started last month if result.start.month() != month: # same as above re: 12 month thing eventStartDay = 1 event['start'] = None else: eventStartDay = result.start.day() event['start'] = result.start.Time() event['title'] = result.Title or result.getId if eventStartDay != eventEndDay: allEventDays = range(eventStartDay, eventEndDay+1) eventDays[eventStartDay]['eventslist'].append( {'end': None, 'start': result.start.Time(), 'title': event['title']} ) eventDays[eventStartDay]['event'] = 1 for eventday in allEventDays[1:-1]: eventDays[eventday]['eventslist'].append( {'end': None, 'start': None, 'title': event['title']} ) eventDays[eventday]['event'] = 1 if result.end == result.end.earliestTime(): last_day_data = eventDays[allEventDays[-2]] last_days_event = last_day_data['eventslist'][-1] last_days_event['end'] = (result.end-1).latestTime().Time() else: eventDays[eventEndDay]['eventslist'].append( {'end': result.end.Time(), 'start': None, 'title': event['title']} ) eventDays[eventEndDay]['event'] = 1 else: eventDays[eventStartDay]['eventslist'].append(event) eventDays[eventStartDay]['event'] = 1 # This list is not uniqued and isn't sorted # uniquing and sorting only wastes time # and in this example we don't need to because # later we are going to do an 'if 2 in eventDays' # so the order is not important. # example: [23, 28, 29, 30, 31, 23] return eventDays CalendarTool.__doc__ = PloneCalTool.__doc__ InitializeClass(CalendarTool)