Django

Code

Changeset 2743

Show
Ignore:
Timestamp:
04/23/06 19:18:57 (3 years ago)
Author:
adrian
Message:

magic-removal: Fixed #328 -- Added archive_week generic view

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/views/generic/date_based.py

    r2701 r2743  
    5858    model = queryset.model 
    5959    now = datetime.datetime.now() 
     60 
    6061    lookup_kwargs = {'%s__year' % date_field: year} 
     62 
    6163    # Only bother to check current date if the year isn't in the past. 
    6264    if int(year) >= now.year: 
     
    104106    model = queryset.model 
    105107    now = datetime.datetime.now() 
     108 
    106109    # Calculate first and last day of month, for use in a date-range lookup. 
    107110    first_day = date.replace(day=1) 
     
    111114        last_day = first_day.replace(month=first_day.month + 1) 
    112115    lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 
     116 
    113117    # Only bother to check current date if the month isn't in the past. 
    114118    if last_day >= now.date(): 
     
    126130        'previous_month': first_day - datetime.timedelta(days=1), 
    127131    }, context_processors) 
     132    for key, value in extra_context.items(): 
     133        if callable(value): 
     134            c[key] = value() 
     135        else: 
     136            c[key] = value 
     137    return HttpResponse(t.render(c)) 
     138 
     139def archive_week(request, year, week, queryset, date_field, 
     140        template_name=None, template_loader=loader, 
     141        extra_context={}, allow_empty=True, context_processors=None, 
     142        template_object_name='object'): 
     143    """ 
     144    Generic weekly archive view. 
     145 
     146    Templates: ``<app_label>/<model_name>_archive_week.html`` 
     147    Context: 
     148        week: 
     149            (date) this week 
     150        object_list: 
     151            list of objects published in the given week 
     152    """ 
     153    try: 
     154        date = datetime.date(*time.strptime(year+'-0-'+week, '%Y-%w-%U')[:3]) 
     155    except ValueError: 
     156        raise Http404 
     157 
     158    model = queryset.model 
     159    now = datetime.datetime.now() 
     160 
     161    # Calculate first and last day of week, for use in a date-range lookup. 
     162    first_day = date 
     163    last_day = date + datetime.timedelta(days=7) 
     164    lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 
     165 
     166    # Only bother to check current date if the week isn't in the past. 
     167    if last_day >= now.date(): 
     168        lookup_kwargs['%s__lte' % date_field] = now 
     169    object_list = queryset.filter(**lookup_kwargs) 
     170    if not object_list and not allow_empty: 
     171        raise Http404 
     172    if not template_name: 
     173        template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower()) 
     174    t = template_loader.get_template(template_name) 
     175    c = RequestContext(request, { 
     176        '%s_list' % template_object_name: object_list, 
     177        'week': date, 
     178    }) 
    128179    for key, value in extra_context.items(): 
    129180        if callable(value): 
     
    158209    model = queryset.model 
    159210    now = datetime.datetime.now() 
     211 
    160212    lookup_kwargs = { 
    161213        '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), 
    162214    } 
     215 
    163216    # Only bother to check current date if the date isn't in the past. 
    164217    if date >= now.date(): 
     
    215268    model = queryset.model 
    216269    now = datetime.datetime.now() 
     270 
    217271    lookup_kwargs = { 
    218272        '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), 
    219273    } 
     274 
    220275    # Only bother to check current date if the date isn't in the past. 
    221276    if date >= now.date(): 
  • django/branches/magic-removal/docs/generic_views.txt

    r2701 r2743  
    213213            parameter. (See above.) For example, if ``template_object_name`` is 
    214214            ``foo``, the variable will be ``foo_list``. 
     215 
     216``archive_week`` 
     217    Weekly archive. Requires that ``year`` and ``week`` arguments be given. The 
     218    ``week`` argument should be an integer (as a string) representing the week 
     219    number, where weeks start with Sunday. 
     220 
     221    Takes an optional ``template_object_name`` parameter, which designates the 
     222    name of the template variable to use. Default is ``'object'``. 
     223 
     224    Uses the template ``<app_label>/<model_name>_archive_week.html`` by default. 
     225 
     226    Has the following template context: 
     227 
     228        ``object_list`` 
     229            List of objects published on the given day. 
     230 
     231            You can change this variable name from ``object_list`` by using the 
     232            ``template_object_name`` parameter. (See above.) For example, if 
     233            ``template_object_name`` is ``foo``, the variable will be ``foo_list``. 
     234        ``week`` 
     235            The first day of the given week (a datetime.datetime object) 
    215236 
    216237``archive_day``