def render(self, context): count = self.count_var.resolve(context) try: count = int(count) except (TypeError, ValueError): return "" upcoming = [] # Start from tomorrow day = datetime.date.today() + datetime.timedelta(days=1) if self.queryset_var: all_assignments = self.queryset_var.resolve(context) if all_assignments == "": return "" else: all_assignments = models.Assignment.objects.assignments() all_assignments = all_assignments.order_by("-created_at") current_days = 0 i = 0 # Get count days, looking forwards one week more than count in case of # blank days. while current_days < count and i < 7 + count: assignments = all_assignments.filter(pub_date=day) if assignments: url = assignment_day_url(day) upcoming.append((day, url, assignments)) current_days += 1 i += 1 day += datetime.timedelta(days=1) context[self.output_name] = upcoming return ""
def newsroom_calendar(context, days, assignments=None): try: days = int(days) except (TypeError, ValueError): return {} month = None day = datetime.date.today() request = context.get("request") if request: match = RE_CAL.match(request.GET.get("cal", "")) if match and match.group(1).lower() in MONTHS: day = datetime.date(int(match.group(2)), MONTHS.index(match.group(1).lower()) + 1, 1) last_day = day + datetime.timedelta(days=days) if assignments is None: assignments = models.Assignment.objects.assignments() assignments = assignments.filter(pub_date__gte=day, pub_date__lte=last_day) assignment_days = {} for assignment in assignments: unfinished, finished = assignment_days.setdefault(assignment.pub_date, ([], [])) if assignment.status.means_completed: finished.append(assignment) else: unfinished.append(assignment) calendar = [] day1 = None for i in range(days): if not day1 and day.day == 1: day1 = day url = assignment_day_url(day) unfinished, finished = assignment_days.get(day, ([], [])) calendar.append((day, url, len(unfinished), len(finished))) day += datetime.timedelta(days=1) c = { "calendar": calendar, "this_year": str(datetime.date.today().year), "request": context.get("request"), "day1": day1, } if day1: prev = day1 - datetime.timedelta(days=20) next = day1 + datetime.timedelta(days=40) c["m_prev"] = datetime.date(prev.year, prev.month, 1) c["m_next"] = datetime.date(next.year, next.month, 1) return c
def listing_day(request, year, month, day): """ Show all assignments for a day. """ try: day = datetime.date(int(year), int(month), int(day)) except ValueError: raise Http404 c = { 'sections': models.Section.objects.all(), 'top_categories': models.Category.objects.filter(top_category=True), 'day': day, 'prev_day_url': assignment_day_url(day-datetime.timedelta(days=1)), 'next_day_url': assignment_day_url(day+datetime.timedelta(days=1)), } assignments = _filter(request, c) assignments = assignments.filter(pub_date=day) c['assignments'] = assignments _sort(request, assignments, c) return direct_to_template(request, 'newsroom/assignments_day.html', c)