Ticket #3189: date_based.py.2.diff

File date_based.py.2.diff, 2.0 KB (added by jgomo3, 16 years ago)

Calendar based archive_week generic view.

  • date_based.py

     
    11import datetime
    22import time
     3import calendar
    34
    45from django.template import loader, RequestContext
    56from django.core.exceptions import ObjectDoesNotExist
     
    166167    return HttpResponse(t.render(c), mimetype=mimetype)
    167168
    168169def archive_week(request, year, week, queryset, date_field,
    169         template_name=None, template_loader=loader,
     170        template_name=None, template_loader=loader, firstweekday=6,
    170171        extra_context=None, allow_empty=True, context_processors=None,
    171172        template_object_name='object', mimetype=None, allow_future=False):
    172173    """
     
    180181            list of objects published in the given week
    181182    """
    182183    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
    183193    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:
    186196        raise Http404
    187197
    188198    model = queryset.model
    189199    now = datetime.datetime.now()
    190200
    191201    # Calculate first and last day of week, for use in a date-range lookup.
    192     first_day = date
    193     last_day = date + datetime.timedelta(days=7)
     202    first_day = the_week[0]
     203    last_day = the_week[-1]
    194204    lookup_kwargs = {
    195205        '%s__gte' % date_field: first_day,
    196206        '%s__lt' % date_field: last_day,
     
    207217    t = template_loader.get_template(template_name)
    208218    c = RequestContext(request, {
    209219        '%s_list' % template_object_name: object_list,
    210         'week': date,
     220        'week': first_day,
    211221    })
    212222    for key, value in extra_context.items():
    213223        if callable(value):
Back to Top