def index_queryset(self, using=None): """ Used when the entire index for model is updated. Only retrieve published events that are not archived (no pending events; allow canceled.) """ now = datetime.now() unarchived_event_pks = EventInstance.objects.filter(end__gte=now, event__state__in=State.get_published_states()).values('event__pk') published_events = self.get_model().objects.filter(pk__in=unarchived_event_pks) return published_events
def calendar_widget(context, calendars, year, month, pk=None, day=None, is_manager=0, size='small', use_pagers=True): # Catch requests for frontend widget with no specified calendar if calendars is "" and is_manager is 0: raise Http404 if pk: calendars = get_object_or_404(Calendar, pk=pk) if day is None or day is "": relative_day = None else: if isinstance(day, datetime): relative_day = day.date() elif isinstance(day, date): relative_day = day else: raise TypeError('day must be a datetime.date or datetime.datetime, not a %s' % type(day)) # Get this month, next and last month (1st day of month) if relative_day is None: this_month = date(int(year), int(month), 1) else: this_month = date(relative_day.year, relative_day.month, 1) next_month = date((this_month + relativedelta(months=+1)).year, (this_month + relativedelta(months=+1)).month, 1) last_month = date((this_month + relativedelta(months=-1)).year, (this_month + relativedelta(months=-1)).month, 1) # Make sure last and next month fall within a valid year/month range if not is_date_in_valid_range(next_month): next_month = None if not is_date_in_valid_range(last_month): last_month = None # Create new list of days in month (strip week grouping) this_month_cal = list(itertools.chain.from_iterable(calgenerator.Calendar(settings.FIRST_DAY_OF_WEEK).monthdatescalendar(this_month.year, this_month.month))) # Set dates as dict keys. Use OrderedDict to sort by date. this_month_cal = OrderedDict((v, []) for k, v in enumerate(this_month_cal)) # Create map of month and day/event list. month_calendar_map = dict({this_month: this_month_cal}) # Get a date range by which we will fetch events start = this_month_cal.keys()[0] end = datetime.combine(this_month_cal.keys()[-1], datetime.max.time()) # Fetch posted events within our date range. calendar = None events = list() if (isinstance(calendars, Calendar)): events.extend(calendars.range_event_instances(start, end).filter(event__state__in=State.get_published_states())) calendar = calendars else: for cal in calendars: events.extend(cal.range_event_instances(start, end).filter(event__state__in=State.get_published_states())) # Assign event to all days the event falls on. events = map_event_range(start, end, events) for event in events: if event.start.date() in month_calendar_map[this_month].keys(): month_calendar_map[this_month][event.start.date()].append(event) context = { 'request': context['request'], 'CANONICAL_ROOT': context['CANONICAL_ROOT'], 'is_manager': is_manager, 'calendar': calendar, 'this_month': this_month, 'next_month': next_month, 'last_month': last_month, 'today': date.today(), 'relative': relative_day, 'calendar_map': month_calendar_map, 'use_pagers': use_pagers, } if size == 'small': template = loader.get_template('events/widgets/calendar-sidebar.html') else: template = loader.get_template('events/widgets/calendar-large.html') html = template.render(Context(context)) return html