Ejemplo n.º 1
0
    def eventsByDay(self, start, end):
        """
        Returns a day-keyed dictionary of days between start and end with events.
        Each day in the sequence is returned as a event list sorted by startTime.
        Also, optionally, selects by several criteria
        """
        days = {}
        query = self.eventsByDateRange(start, end)
        # get the dates, taking recurrence into account
        for result in query:
            recurs = result.get('recurs', 'daily')
            estart = result['start']
            ldate = min(end, result['end'])
            if recurs == 'weekly':
                dates = caldate.weekly(start, ldate, estart)
            elif recurs == 'biweekly':
                dates = caldate.biweekly(start, ldate, estart)
            elif recurs == 'monthly':
                dates = caldate.monthly(start, ldate, estart)
            else:  # daily
                dates = caldate.daily(start, ldate, estart)
            for d in dates:
                days.setdefault(d, []).append(result)

        return days
Ejemplo n.º 2
0
    def getevents(self, first, last, **kwa):
        """ given start and end dates, return a list of days that have events
        """

        ctool = self.calendar
        catalog = getToolByName(self.context, 'portal_catalog')
        site_properties = getToolByName(self.context, 'portal_properties').site_properties

        ampm = site_properties.getProperty('localLongTimeFormat').find('%p') >= 0

        first_date = first.earliestTime()
        last_date = last.latestTime()

        query = catalog(
            portal_type=ctool.getCalendarTypes(),
            review_state=ctool.getCalendarStates(),
            start={'query': last_date, 'range': 'max'},
            end={'query': first_date, 'range': 'min'},
            sort_on='start',
            **kwa)

        # compile a list of the days that have events
        events = []

        for result in query:

            # get the dates, taking recurrence into account
            recurs = getattr(result, 'recurs', 'daily')
            start = result.start
            ldate = min(last_date, result.end.latestTime())
            if recurs == 'weekly':
                dates = caldate.weekly(first_date, ldate, start)
            elif recurs == 'biweekly':
                dates = caldate.biweekly(first_date, ldate, start)
            elif recurs == 'monthly':
                dates = caldate.monthly(first_date, ldate, start)
            else:  # daily
                dates = caldate.daily(first_date, ldate, start)

            if dates:
                if ampm:
                    st = result.start.AMPMMinutes().lstrip('0')
                else:
                    st = result.start.TimeMinutes()

                time = list(result.start.parts()[3:5])
                # put event in list
                for day in dates:
                    startdt = DateTime(*(list(day.parts()[0:3]) + time))
                    events.append((startdt, st, result))

        # sort without considering result part
        events.sort(
            lambda x, y: cmp(x[:2], y[:2])
            )
        return events
Ejemplo n.º 3
0
    def catalog_getevents(self, year, month, **kw):
        """ given a year and month return a list of days that have events
        """
        # XXX: this method violates the rules for tools/utilities:
        # it depends on a non-utility tool
        year = int(year)
        month = int(month)
        last_day = self.calendar._getCalendar().monthrange(year, month)[1]
        first_date = self.calendar.getBeginAndEndTimes(1, month, year)[0]
        last_date = self.calendar.getBeginAndEndTimes(last_day, month, year)[1]

        query_args = {
            'portal_type': self.calendar.getCalendarTypes(),
            'review_state': self.calendar.getCalendarStates(),
            'start': {'query': last_date, 'range': 'max'},
            'end': {'query': first_date, 'range': 'min'},
            'sort_on': 'start'
        }
        query_args.update(kw)

        ctool = getToolByName(self, 'portal_catalog')
        query = ctool(**query_args)

        # 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())

            # we need to deal with events that end after this month
            if (result.end.year(), result.end.month()) != (year, month):
                eventEndDay = last_day
                eventEndDate = last_date
            else:
                eventEndDay = result.end.day()
                eventEndDate = result.end

            # and events that started last month
            if (result.start.year(), result.start.month()) != (year, month):
                eventStartDay = 1
                eventStartDate = first_date
            else:
                eventStartDay = result.start.day()
                eventStartDate = result.start

            # and recurrence
            recurs = result.recurs
            start = result.start.earliestTime()
            if recurs == 'weekly':
                dates = caldate.weekly(eventStartDate, eventEndDate, start)
                allEventDays = [e.day() for e in dates]
            elif recurs == 'biweekly':
                dates = caldate.biweekly(eventStartDate, eventEndDate, start)
                allEventDays = [e.day() for e in dates]
            elif recurs == 'monthly':
                dates = caldate.monthly(eventStartDate, eventEndDate, start)
                allEventDays = [e.day() for e in dates]
            else:  # daily
                allEventDays = range(eventStartDay, eventEndDay + 1)

            # construct subject class selectors
            exclasses = ' '.join(["subject-%s" % url_quote_plus(s).replace('+', '-') for s in result.Subject])

            # construct dictionary to return for event
            st = result.start.AMPMMinutes().lstrip('0')
            event = {'end': None,
                     'start': st,
                     'title': result.Title or result.getId,
                     'desc': result.Description,
                     'url': result.getURL(),
                     'exclasses': exclasses,
                    }

            # put event in list
            for eventday in allEventDays:
                eventDays[eventday]['eventslist'].append(event)
                eventDays[eventday]['event'] = 1

        return eventDays