def get_week_agenda(model, queryset, start_date): """ Get list of events that will occur in the given week. :param queryset: EventInstance queryset :param start_date: period start_date :type start_date: datetime.datetime() :return: data dictionary """ period = Week(start_date.year, start_date.date().isocalendar()[1]) end_date = utils.date_to_datetime(period.sunday(), 'max') start_date = utils.date_to_datetime(period.monday()) return { 'start_date': start_date, 'end_date': end_date, 'scope': 'Week', 'items': model.objects.in_date_range(start_date, end_date).filter(event__in=queryset), 'next_date': start_date + timedelta(days=7), 'previous_date': start_date + timedelta(days=-7), }
def get_month_agenda(model, queryset, start_date): """ Get list of events that will occur in the given week. :param queryset: EventInstance queryset :param start_date: period start_date :type start_date: datetime.datetime() :return: data dictionary """ start_date = datetime(start_date.year, start_date.month, 1) end_date = utils.date_to_datetime(utils.add_months(start_date.date(), 1), 'max') return { 'start_date': start_date, 'end_date': end_date, 'scope': 'Month', 'items': model.objects.in_date_range(start_date, end_date).filter(event__in=queryset), 'next_date': utils.date_to_datetime(utils.add_months(start_date.date(), 1)), 'previous_date': utils.date_to_datetime(utils.remove_months(start_date.date(), 1)), }
def get_day_range(start_date): """ Get the start and end datetimes for the day :param start_date: period start_date :type start_date: datetime.datetime() :return: tuple start_datetime, end_datetime """ start_date = utils.date_to_datetime(start_date.date(), 'min') end_date = utils.date_to_datetime(start_date.date(), 'max') return start_date, end_date
def get_week_range(start_date): """ Get the start and end datetimes for the week :param start_date: period start_date :type start_date: datetime.datetime() :return: tuple start_datetime, end_datetime """ period = Week(start_date.year, start_date.date().isocalendar()[1]) start_date = utils.date_to_datetime(period.monday()) end_date = utils.date_to_datetime(period.sunday(), 'max') return start_date, end_date
def get_day_agenda(model, queryset, start_date): """ Get list of events that will occur in the given date :param queryset: EventInstance queryset :param start_date: period start_date :type start_date: datetime.datetime() :return: data dictionary """ next_date = start_date + timedelta(days=1) return { 'start_date': start_date, 'end_date': utils.date_to_datetime(start_date.date(), 'max'), 'scope': 'Day', 'items': model.objects.in_date_range(start_date, next_date).filter(event__in=queryset), 'next_date': next_date, 'previous_date': start_date + timedelta(days=-1), }
def _get_children(self, request): """ Gets the EventOccurrences related to the EventDetails. :param request: django request :return: Queryset of child model instances """ qs = super(EventIndex, self)._get_children(request) default_period = 'day' time_periods = { 'year': date_filters.get_year_agenda, 'week': date_filters.get_week_agenda, 'month': date_filters.get_month_agenda, default_period: date_filters.get_day_agenda, } period = request.GET.get('scope', default_period).lower() if period not in time_periods.keys(): return {'items': EventOccurrence.objects.filter(event__in=qs)} start_date = request.GET.get('start_date', '') if re.match(self.get_dateformat(), start_date): date_params = [int(i) for i in start_date.split('.')] start_date = utils.date_to_datetime(datetime.date(*date_params)) else: start_date = timezone.now().replace( hour=0, minute=0, second=0, microsecond=0, ) return time_periods[period](EventOccurrence, qs, start_date)
def get_year_range(start_date): """ Get the start and end datetimes for the year :param start_date: period start_date :type start_date: datetime.datetime() :return: tuple start_datetime, end_datetime """ start_date = datetime(start_date.year, 1, 1) end_date = utils.date_to_datetime(utils.add_months(start_date, 12), 'max') return start_date, end_date
def _get_children(self, request, *args, **kwargs): """ :param request: django request :return: Queryset of child model instances, filtered by a scope and start date in the request params """ qs = super(AbstractEventIndex, self)._get_children(request).specific() time_periods = { 'year': date_filters.get_year_range, 'week': date_filters.get_week_range, 'month': date_filters.get_month_range, 'day': date_filters.get_day_range, } period = request.GET.get('scope', None) if period: # Get the start date from the request (or use default now) start_date = request.GET.get('start_date', '') if re.match(self.get_dateformat(), start_date): date_params = [int(i) for i in start_date.split('.')] start_date = utils.date_to_datetime( datetime.date(*date_params)) else: start_date = timezone.now().replace( hour=0, minute=0, second=0, microsecond=0, ) # Clean the start and end dates to conform to the requested period start_date, end_date = time_periods[period.lower()](start_date) # Filter the original queryset to the date range specified qs.filter(start_date__gte=start_date).filter( end_date__lte=end_date) return qs