def year_month_display(self): """ Return the year and month to display in the calendar. """ context = aq_inner(self.context) request = self.request # Try to get year and month from request year = request.get('year', None) month = request.get('month', None) # Or use current date today = localized_today(context) if not year: year = today.year if not month: month = today.month # try to transform to number but fall back to current # date if this is ambiguous try: year, month = int(year), int(month) except (TypeError, ValueError): year, month = today.year, today.month return year, month
def today(self): today = {} loc_today = localized_today(self.context) weekday = loc_today.isoweekday() today['weekday'] = PLMF(self._ts.day_msgid(0 if weekday == 7 else weekday, format='l')) today['number'] = loc_today.day return today
def test_invalid_request(self): self.request.form['month'] = [3, 4] self.request.form['year'] = [2011] r = self.renderer() r.update() today = localized_today(self.portal) self.assertEqual(r.year_month_display(), (today.year, today.month))
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 occurrences = get_occurrences_by_date( context, monthdates[0], monthdates[-1], **query_kw) # [[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 occurrences: date_events = occurrences[isodat] events_string = u"" if date_events: for occ in date_events: accessor = IEventAccessor(occ) location = accessor.location events_string += u'%s<a href="%s">%s</a>%s' % ( events_string and u"</br>" or u"", accessor.url, accessor.title, 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] 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 year_month_display(self): """ Return the year and month to display in the calendar. """ context = aq_inner(self.context) request = self.request # Try to get year and month from requst year = request.get("year", None) month = request.get("month", None) # Or use current date if not year or month: today = localized_today(context) if not year: year = today.year if not month: month = today.month return int(year), int(month)
def cal(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() cal = calendar.Calendar(first_weekday()) monthdates = [dat for dat in cal.itermonthdates(year, month)] events = get_events_by_date(context, monthdates[0], monthdates[-1]) # [[day1week1, day2week1, ... day7week1], [day1week2, ...]] cal = [[]] for dat in monthdates: if len(cal[-1]) == 7: cal.append([]) date_events = None isodat = dat.isoformat() if isodat in events: date_events = events[isodat] events_string = u"" if date_events: for event in date_events: events_string += u'%s<a href="%s">%s</a>%s' % ( events_string and u"</br>" or u"", event.getURL(), event.Title, event.location and u" %s" % event.location or u"", ) cal[-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 cal
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 test_get_events_today(self): today = localized_today().isoformat() self.request.form.update({'mode': 'day', 'date': today}) view = self.portal.restrictedTraverse('@@event_listing') self.assertEqual(len(view._get_events()), 2)
def test_get_events_today(self): today = localized_today().isoformat() self.request.form.update({'mode': 'day', 'date': today}) view = self._listing_view() self.assertEqual(len(view.events(batch=False)), 2)
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)] 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
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