Ticket #1229: date_based.diff

File date_based.diff, 2.4 KB (added by nick@…, 19 years ago)

Fixes date-based generic views to universally support allow_empty

  • date_based.py

     
    4848
    4949def archive_year(request, year, model, date_field,
    5050        template_name=None, template_loader=loader, extra_lookup_kwargs={},
    51         extra_context={}, context_processors=None):
     51        extra_context={}, allow_empty=False, context_processors=None):
    5252    """
    5353    Generic yearly archive view.
    5454
     
    6666        lookup_kwargs['%s__lte' % date_field] = now
    6767    lookup_kwargs.update(extra_lookup_kwargs)
    6868    date_list = getattr(model._default_manager, "get_%s_list" % date_field)('month', **lookup_kwargs)
    69     if not date_list:
    70         raise Http404
     69    if not date_list and not allow_empty:
     70        raise Http404, "No %s available" % model._meta.verbose_name
    7171    if not template_name:
    7272        template_name = "%s/%s_archive_year" % (model._meta.app_label, model._meta.object_name.lower())
    7373    t = template_loader.get_template(template_name)
     
    8484
    8585def archive_month(request, year, month, model, date_field,
    8686        month_format='%b', template_name=None, template_loader=loader,
    87         extra_lookup_kwargs={}, extra_context={}, context_processors=None):
     87        extra_lookup_kwargs={}, extra_context={}, allow_empty=False,
     88        context_processors=None):
    8889    """
    8990    Generic monthly archive view.
    9091
     
    113114        lookup_kwargs['%s__lte' % date_field] = now
    114115    lookup_kwargs.update(extra_lookup_kwargs)
    115116    object_list = model._default_manager.get_list(**lookup_kwargs)
    116     if not object_list:
    117         raise Http404
     117    if not object_list and not allow_empty:
     118        raise Http404, "No %s available" % model._meta.verbose_name
    118119    if not template_name:
    119120        template_name = "%s/%s_archive_month" % (model._meta.app_label, model._meta.object_name.lower())
    120121    t = template_loader.get_template(template_name)
     
    162163    lookup_kwargs.update(extra_lookup_kwargs)
    163164    object_list = model._default_manager.get_list(**lookup_kwargs)
    164165    if not allow_empty and not object_list:
    165         raise Http404
     166        raise Http404, "No %s available" % model._meta.verbose_name
    166167    if not template_name:
    167168        template_name = "%s/%s_archive_day" % (model._meta.app_label, model._meta.object_name.lower())
    168169    t = template_loader.get_template(template_name)
Back to Top