Ticket #3189: date_based.py.2.diff
File date_based.py.2.diff, 2.0 KB (added by , 16 years ago) |
---|
-
date_based.py
1 1 import datetime 2 2 import time 3 import calendar 3 4 4 5 from django.template import loader, RequestContext 5 6 from django.core.exceptions import ObjectDoesNotExist … … 166 167 return HttpResponse(t.render(c), mimetype=mimetype) 167 168 168 169 def archive_week(request, year, week, queryset, date_field, 169 template_name=None, template_loader=loader, 170 template_name=None, template_loader=loader, firstweekday=6, 170 171 extra_context=None, allow_empty=True, context_processors=None, 171 172 template_object_name='object', mimetype=None, allow_future=False): 172 173 """ … … 180 181 list of objects published in the given week 181 182 """ 182 183 if extra_context is None: extra_context = {} 184 cal = calendar.Calendar(firstweekday) 185 weeks = [] 186 last_week = [] 187 the_week = [] 188 for month in cal.yeardatescalendar(int(year), 12)[0]: 189 for a_week in month: 190 if a_week != last_week: 191 weeks.append(a_week) 192 last_week = a_week 183 193 try: 184 date = datetime.date(*time.strptime(year+'-0-'+week, '%Y-%w-%U')[:3])185 except ValueError:194 the_week = weeks[int(week)] 195 except IndexError: 186 196 raise Http404 187 197 188 198 model = queryset.model 189 199 now = datetime.datetime.now() 190 200 191 201 # Calculate first and last day of week, for use in a date-range lookup. 192 first_day = date193 last_day = date + datetime.timedelta(days=7)202 first_day = the_week[0] 203 last_day = the_week[-1] 194 204 lookup_kwargs = { 195 205 '%s__gte' % date_field: first_day, 196 206 '%s__lt' % date_field: last_day, … … 207 217 t = template_loader.get_template(template_name) 208 218 c = RequestContext(request, { 209 219 '%s_list' % template_object_name: object_list, 210 'week': date,220 'week': first_day, 211 221 }) 212 222 for key, value in extra_context.items(): 213 223 if callable(value):