def test_construct_calendar(self): res = get_events(self.portal, ret_mode=RET_MODE_OBJECTS, expand=True) cal = construct_calendar(res) # keys are date-strings. def _num_events(values): num = 0 for val in values: num += len(val) return num # The long_event occurs on every day in the resulting calendar data # structure. self.assertEqual(_num_events(cal.values()), 48) # Test with range # # Completly outside range and start, end given as datetime cal = construct_calendar( res, start=datetime(2000, 1, 1, 10, 0), end=datetime(2000, 1, 2, 10, 0) ) self.assertEqual(_num_events(cal.values()), 0) # Within range cal = construct_calendar( res, start=date(2013, 5, 1), end=date(2013, 5, 31) ) self.assertEqual(_num_events(cal.values()), 35) # First day must also be set in the calendar self.assertTrue('2013-05-01' in cal.keys()) # invalid start def _invalid_start(): return construct_calendar( res, start='invalid', end=datetime(2000, 1, 2, 10, 0) ) self.assertRaises(AssertionError, _invalid_start) # invalid end def _invalid_end(): return construct_calendar( res, start=datetime(2000, 1, 1, 10, 0), end='invalid' ) self.assertRaises(AssertionError, _invalid_end)
def getCalendarDict(self): year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] start = monthdates[0] end = monthdates[-1] date_range_query = {'query': (start, end), 'range': 'min:max'} portal_catalog = api.portal.get_tool(name='portal_catalog') if api.user.is_anonymous(): items = portal_catalog.unrestrictedSearchResults( portal_type='genweb.organs.sessio', start=date_range_query, path=self.get_public_organs_fields()) events = [] for event in items: events.append(event._unrestrictedGetObject()) else: items = portal_catalog.unrestrictedSearchResults( portal_type='genweb.organs.sessio', start=date_range_query) events = [] username = api.user.get_current().id for event in items: session = event._unrestrictedGetObject() roles = api.user.get_roles(username=username, obj=session) if 'OG1-Secretari' in roles or 'OG2-Editor' in roles or 'OG3-Membre' in roles or 'OG4-Afectat' in roles or 'Manager' in roles or session.aq_parent.visiblefields: events.append(session) return construct_calendar(events, start=start, end=end)
def test_construct_calendar(self): res = get_events(self.portal, ret_mode=2, expand=True) cal = construct_calendar(res) # keys are date-strings. # Should be more than one, but we can't exactly say how much. This # depends on the date, the test is run. E.g. on last day of month, only # long, past and now without recurrences are returned, others are in # next month. self.assertTrue(len(cal.keys()) > 1)
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] start = monthdates[0] end = monthdates[-1] date_range_query = {'query': (start, end), 'range': 'min:max'} portal_catalog = getToolByName(self.context, 'portal_catalog') events = [] items = portal_catalog.unrestrictedSearchResults( portal_type='Event', start=date_range_query) for event in items: session = event._unrestrictedGetObject() events.append(session) cal_dict = construct_calendar(events, start=start, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] caldata[-1].append( {'date': dat, 'day': dat.day, 'month': dat.month, 'year': dat.year, 'prev_month': dat.month < month, 'next_month': dat.month > month, 'today': dat.year == today.year and dat.month == today.month and dat.day == today.day, 'events': date_events}) return caldata
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] start = monthdates[0] end = monthdates[-1] data = self.data query = {} if data.state: query['review_state'] = data.state events = [] query.update(self.request.get('contentFilter', {})) search_base = self.search_base if ICollection and ICollection.providedBy(search_base): # Whatever sorting is defined, we're overriding it. query = queryparser.parseFormquery( search_base, search_base.query, sort_on='start', sort_order=None ) # restrict start/end with those from query, if given. if 'start' in query and query['start'] > start: start = query['start'] if 'end' in query and query['end'] < end: end = query['end'] start, end = _prepare_range(search_base, start, end) query.update(start_end_query(start, end)) events = search_base.results( batch=False, brains=True, custom_query=query ) events = expand_events( events, ret_mode=RET_MODE_OBJECTS, start=start, end=end, sort='start', sort_reverse=False ) else: search_base_path = self.search_base_path if search_base_path: query['path'] = {'query': search_base_path} events = get_events(context, start=start, end=end, ret_mode=RET_MODE_OBJECTS, expand=True, **query) #today += datetime.timedelta(days=1) cal_dict = construct_calendar(events, start=today, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] events_string_list = [] if date_events: for occ in date_events: accessor = IEventAccessor(occ) location = accessor.location whole_day = accessor.whole_day time = accessor.start.time().strftime('%H:%M') # TODO: make 24/12 hr format configurable events_string_list.append( u'{0}{1}{2}{3}'.format( accessor.title, u' {0}'.format(time) if not whole_day else u'', u', ' if not whole_day and location else u'', u' {0}'.format(location) if location else u'' ) ) caldata[-1].append( {'date': dat, 'day': dat.day, 'prev_month': dat.month < month, 'next_month': dat.month > month, 'today': dat.year == today.year and dat.month == today.month and dat.day == today.day, 'date_string': u"%s-%s-%s" % (dat.year, dat.month, dat.day), 'events_string': u' | '.join(events_string_list), 'events': date_events}) return caldata
def _invalid_end(): return construct_calendar( res, start=datetime(2000, 1, 1, 10, 0), end='invalid' )
def _invalid_start(): return construct_calendar( res, start='invalid', end=datetime(2000, 1, 2, 10, 0) )
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] start = monthdates[0] end = monthdates[-1] data = self.data query = {} if data.state: query['review_state'] = data.state events = [] query.update(self.request.get('contentFilter', {})) search_base = self.search_base if ICollection and ICollection.providedBy(search_base): # Whatever sorting is defined, we're overriding it. query = queryparser.parseFormquery( search_base, search_base.query, sort_on='start', sort_order=None ) # restrict start/end with those from query, if given. if 'start' in query and query['start'] > start: start = query['start'] if 'end' in query and query['end'] < end: end = query['end'] start, end = _prepare_range(search_base, start, end) query.update(start_end_query(start, end)) events = search_base.results( batch=False, brains=True, custom_query=query ) events = expand_events( events, ret_mode=RET_MODE_OBJECTS, start=start, end=end, sort='start', sort_reverse=False ) else: search_base_path = self.search_base_path if search_base_path: query['path'] = {'query': search_base_path} events = get_events(context, start=start, end=end, ret_mode=RET_MODE_OBJECTS, expand=True, **query) cal_dict = construct_calendar(events, start=start, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] events_string = u"" events_title = u"" if date_events: for occ in date_events: accessor = IEventAccessor(occ) location = accessor.location whole_day = accessor.whole_day time = accessor.start.time().strftime('%H:%M') # TODO: make 24/12 hr format configurable base = u'<a href="%s"><span class="title">%s</span>'\ u'%s%s%s</a>' events_title += accessor.title events_string += base % ( accessor.url, accessor.title, u' %s' % time if not whole_day else u'', u', ' if not whole_day and location else u'', u' %s' % location if location else u'') caldata[-1].append( {'date': dat, 'day': dat.day, 'prev_month': dat.month < month, 'next_month': dat.month > month, 'today': dat.year == today.year and dat.month == today.month and dat.day == today.day, 'date_string': u"%s-%s-%s" % (dat.year, dat.month, dat.day), 'events_string': events_string, 'events_title': events_title, 'events': date_events}) return caldata
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] data = self.data query_kw = {} if data.search_base: portal = getToolByName(context, 'portal_url').getPortalObject() query_kw['path'] = {'query': '%s%s' % ( '/'.join(portal.getPhysicalPath()), data.search_base)} if data.state: query_kw['review_state'] = data.state start = monthdates[0] end = monthdates[-1] events = get_events(context, start=start, end=end, ret_mode=2, expand=True, **query_kw) cal_dict = construct_calendar(events, start=start, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] events_string = u"" if date_events: for occ in date_events: accessor = IEventAccessor(occ) location = accessor.location whole_day = accessor.whole_day time = accessor.start.time().strftime('%H:%M') # TODO: make 24/12 hr format configurable base = u'<a href="%s"><span class="title">%s</span>'\ u'%s%s%s</a>' events_string += base % ( accessor.url, accessor.title, not whole_day and u' %s' % time or u'', not whole_day and location and u', ' or u'', location and u' %s' % location or u'') caldata[-1].append( {'date': dat, 'day': dat.day, 'prev_month': dat.month < month, 'next_month': dat.month > month, 'today': dat.year == today.year and\ dat.month == today.month and\ dat.day == today.day, 'date_string': u"%s-%s-%s" % (dat.year, dat.month, dat.day), 'events_string': events_string, 'events': date_events}) return caldata
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] data = self.data query_kw = {} if data.search_base: portal = getToolByName(context, 'portal_url').getPortalObject() query_kw['path'] = { 'query': '%s%s' % ('/'.join(portal.getPhysicalPath()), data.search_base) } if data.state: query_kw['review_state'] = data.state start = monthdates[0] end = monthdates[-1] events = get_events(context, start=start, end=end, ret_mode=2, expand=True, **query_kw) cal_dict = construct_calendar(events, start=start, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] events_string = u"" if date_events: for occ in date_events: accessor = IEventAccessor(occ) location = accessor.location whole_day = accessor.whole_day time = accessor.start.time().strftime('%H:%M') # TODO: make 24/12 hr format configurable base = u'<a href="%s"><span class="title">%s</span>'\ u'%s%s%s</a>' events_string += base % ( accessor.url, accessor.title, not whole_day and u' %s' % time or u'', not whole_day and location and u', ' or u'', location and u' %s' % location or u'') caldata[-1].append( {'date': dat, 'day': dat.day, 'prev_month': dat.month < month, 'next_month': dat.month > month, 'today': dat.year == today.year and\ dat.month == today.month and\ dat.day == today.day, 'date_string': u"%s-%s-%s" % (dat.year, dat.month, dat.day), 'events_string': events_string, 'events': date_events}) return caldata
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] start = monthdates[0] end = monthdates[-1] data = self.data query = {} if data.state: query["review_state"] = data.state events = [] query.update(self.request.get("contentFilter", {})) search_base = self.search_base if ICollection and ICollection.providedBy(search_base): # Whatever sorting is defined, we're overriding it. query = queryparser.parseFormquery(search_base, search_base.query, sort_on="start", sort_order=None) # restrict start/end with those from query, if given. if "start" in query and query["start"] > start: start = query["start"] if "end" in query and query["end"] < end: end = query["end"] start, end = _prepare_range(search_base, start, end) query.update(start_end_query(start, end)) events = search_base.results(batch=False, brains=True, custom_query=query) events = expand_events( events, ret_mode=RET_MODE_OBJECTS, start=start, end=end, sort="start", sort_reverse=False ) else: search_base_path = self.search_base_path if search_base_path: query["path"] = {"query": search_base_path} events = get_events(context, start=start, end=end, ret_mode=RET_MODE_OBJECTS, expand=True, **query) cal_dict = construct_calendar(events, start=start, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] events_string_list = [] if date_events: for occ in date_events: accessor = IEventAccessor(occ) location = accessor.location whole_day = accessor.whole_day time = accessor.start.time().strftime("%H:%M") # TODO: make 24/12 hr format configurable events_string_list.append( u"{0}{1}{2}{3}".format( accessor.title, u" {0}".format(time) if not whole_day else u"", u", " if not whole_day and location else u"", u" {0}".format(location) if location else u"", ) ) caldata[-1].append( { "date": dat, "day": dat.day, "prev_month": dat.month < month, "next_month": dat.month > month, "today": dat.year == today.year and dat.month == today.month and dat.day == today.day, "date_string": u"%s-%s-%s" % (dat.year, dat.month, dat.day), "events_string": u" | ".join(events_string_list), "events": date_events, } ) return caldata
def cal_data(self): """Calendar iterator over weeks and days of the month to display. """ context = aq_inner(self.context) today = localized_today(context) year, month = self.year_month_display() monthdates = [dat for dat in self.cal.itermonthdates(year, month)] start = monthdates[0] end = monthdates[-1] date_range_query = {'query': (start, end), 'range': 'min:max'} portal_catalog = api.portal.get_tool(name='portal_catalog') events = [] if api.user.is_anonymous(): items = portal_catalog.unrestrictedSearchResults( portal_type='genweb.organs.sessio', start=date_range_query, path=self.get_public_organs_fields()) for event in items: events.append(event._unrestrictedGetObject()) else: items = portal_catalog.unrestrictedSearchResults( portal_type='genweb.organs.sessio', start=date_range_query) username = api.user.get_current().id for event in items: session = event._unrestrictedGetObject() roles = api.user.get_roles(username=username, obj=session) if 'OG1-Secretari' in roles or 'OG2-Editor' in roles or 'OG3-Membre' in roles or 'OG4-Afectat' in roles or 'Manager' in roles or session.aq_parent.visiblefields: events.append(session) cal_dict = construct_calendar(events, start=start, end=end) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] caldata = [[]] for dat in monthdates: if len(caldata[-1]) == 7: caldata.append([]) date_events = None isodat = dat.isoformat() if isodat in cal_dict: date_events = cal_dict[isodat] color = '' if date_events: for occ in date_events: color = occ.aq_parent.eventsColor caldata[-1].append({ 'date': dat, 'day': dat.day, 'month': dat.month, 'year': dat.year, 'prev_month': dat.month < month, 'next_month': dat.month > month, 'color': color, 'today': dat.year == today.year and dat.month == today.month and dat.day == today.day, 'events': date_events }) return caldata