def get_context_data(self, **kwargs): context = super(AvailabilityReportView, self).get_context_data(**kwargs) if 'report-month' in self.request.GET: year, month = [int(x) for x in self.request.GET.get('report-month').split('-')] sometime = datetime(year, month, 1) start, end = utils.get_interval(sometime, ReportSubscription.MONTH) context['start'] = start context['end'] = end context['records'] = sorted(self.get_records(start, end), key=attrgetter('availability')) context['months'] = utils.get_months() context['report'] = self context['exclude_maintenance'] = self.exclude_maintenance() return context
def get_last_interval(sometime, period): """Gets the start and end dates for the previous period This means that if `sometime` is a datetime object from the current month and `period` indicates month, then this function will return two datetime-objects - the first one is the first day of _previous_ month and the second one is the first day of the _current_ month. :returns: start and end date objects :rtype: list[datetime.datetime] """ if period == ReportSubscription.MONTH: first_day = sometime.replace(day=1) elif period == ReportSubscription.WEEK: first_day = sometime - timedelta(days=sometime.weekday()) else: first_day = sometime # Daily last_period = first_day - timedelta(days=1) start, end = utils.get_interval(last_period, period) return convert_to_datetime(start, end)