Removed 'use_numeric_months' parameter in generic date views (from [308]) in favor of something more powerful -- you can now provide month_format and day_format, which are format strings that specify how you expect the month and day to be formatted in the URL.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@312 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-07-26 03:48:41 +00:00
parent 48441f467f
commit 7d574e8f76
2 changed files with 47 additions and 52 deletions

View File

@ -6,7 +6,7 @@ from django.models import get_module
from django.utils.httpwrappers import HttpResponse from django.utils.httpwrappers import HttpResponse
import datetime, time import datetime, time
def archive_index(request, app_label, module_name, date_field, num_latest=15, def archive_index(request, app_label, module_name, date_field, num_latest=15,
template_name=None, extra_lookup_kwargs={}, extra_context={}): template_name=None, extra_lookup_kwargs={}, extra_context={}):
""" """
Generic top-level archive of date-based objects. Generic top-level archive of date-based objects.
@ -44,11 +44,11 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
c[key] = value() c[key] = value()
else: else:
c[key] = value c[key] = value
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
def archive_year(request, year, app_label, module_name, date_field, def archive_year(request, year, app_label, module_name, date_field,
template_name=None, extra_lookup_kwargs={}, extra_context={}): template_name=None, extra_lookup_kwargs={}, extra_context={}):
""" """
Generic yearly archive view. Generic yearly archive view.
@ -80,13 +80,13 @@ def archive_year(request, year, app_label, module_name, date_field,
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
c[key] = value() c[key] = value()
else: else:
c[key] = value c[key] = value
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
def archive_month(request, year, month, app_label, module_name, date_field, def archive_month(request, year, month, app_label, module_name, date_field,
use_numeric_months=False, template_name=None, month_format='%b', template_name=None, extra_lookup_kwargs={},
extra_lookup_kwargs={}, extra_context={}): extra_context={}):
""" """
Generic monthly archive view. Generic monthly archive view.
@ -97,17 +97,11 @@ def archive_month(request, year, month, app_label, module_name, date_field,
object_list: object_list:
list of objects published in the given month list of objects published in the given month
""" """
if use_numeric_months: try:
try: date = datetime.date(*time.strptime(year+month, '%Y'+month_format)[:3])
date = datetime.date(int(year), int(month), 1) except ValueError:
except (ValueError, TypeError): raise Http404
raise Http404
else:
try:
date = datetime.date(*time.strptime(year+month, '%Y%b')[:3])
except ValueError:
raise Http404
mod = get_module(app_label, module_name) mod = get_module(app_label, module_name)
now = datetime.datetime.now() now = datetime.datetime.now()
# Calculate first and last day of month, for use in a date-range lookup. # Calculate first and last day of month, for use in a date-range lookup.
@ -138,13 +132,13 @@ def archive_month(request, year, month, app_label, module_name, date_field,
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
c[key] = value() c[key] = value()
else: else:
c[key] = value c[key] = value
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
def archive_day(request, year, month, day, app_label, module_name, date_field, def archive_day(request, year, month, day, app_label, module_name, date_field,
use_numeric_months=False, template_name=None, extra_lookup_kwargs={}, month_format='%b', day_format='%d', template_name=None,
extra_context={}, allow_empty=False): extra_lookup_kwargs={}, extra_context={}, allow_empty=False):
""" """
Generic daily archive view. Generic daily archive view.
@ -159,16 +153,10 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
next_day next_day
(datetime) the next day, or None if the current day is today (datetime) the next day, or None if the current day is today
""" """
if use_numeric_months: try:
try: date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
date = datetime.date(int(year), int(month), int(day)) except ValueError:
except (ValueError, TypeError): raise Http404
raise Http404
else:
try:
date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
except ValueError:
raise Http404
mod = get_module(app_label, module_name) mod = get_module(app_label, module_name)
now = datetime.datetime.now() now = datetime.datetime.now()
@ -194,7 +182,7 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
c[key] = value() c[key] = value()
else: else:
c[key] = value c[key] = value
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
@ -210,10 +198,10 @@ def archive_today(request, **kwargs):
}) })
return archive_day(request, **kwargs) return archive_day(request, **kwargs)
def object_detail(request, year, month, day, app_label, module_name, date_field, def object_detail(request, year, month, day, app_label, module_name, date_field,
use_numeric_months=False, object_id=None, slug=None, slug_field=None, month_format='%b', day_format='%d', object_id=None, slug=None,
template_name=None, template_name_field=None, extra_lookup_kwargs={}, slug_field=None, template_name=None, template_name_field=None,
extra_context={}): extra_lookup_kwargs={}, extra_context={}):
""" """
Generic detail view from year/month/day/slug or year/month/day/id structure. Generic detail view from year/month/day/slug or year/month/day/id structure.
@ -222,16 +210,10 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
object: object:
the object to be detailed the object to be detailed
""" """
if use_numeric_months: try:
try: date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
date = datetime.date(int(year), int(month), int(day)) except ValueError:
except (ValueError, TypeError): raise Http404
raise Http404
else:
try:
date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
except ValueError:
raise Http404
mod = get_module(app_label, module_name) mod = get_module(app_label, module_name)
now = datetime.datetime.now() now = datetime.datetime.now()
@ -265,7 +247,7 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
c[key] = value() c[key] = value()
else: else:
c[key] = value c[key] = value
response = HttpResponse(t.render(c)) response = HttpResponse(t.render(c))
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name)) populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))

View File

@ -126,10 +126,14 @@ The date-based generic functions are:
``archive_month`` ``archive_month``
Monthly archive. Requires that ``year`` and ``month`` arguments be given. Monthly archive. Requires that ``year`` and ``month`` arguments be given.
You may pass the additional option ``use_numeric_months`` if you'd like to You can pass the additional option ``month_format`` if you'd like to change
use URLs that use numbers instead of names for months (i.e. ``/2005/01/15/`` the way months are specified in the URL.
instead of ``/2005/jan/15/``.
``month_format`` is a format string in the same syntax accepted by Python's
``time.strftime``. (See the `strftime docs`_.) It's set to ``"%b"`` by
default, which is a three-letter month abbreviation. To change it to use
numbers, use ``"%m"``.
Uses the template ``app_label/module_name__archive_month`` by default. Uses the template ``app_label/module_name__archive_month`` by default.
Has the following template context: Has the following template context:
@ -143,6 +147,10 @@ The date-based generic functions are:
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
given. given.
As in ``archive_month``, you can pass an optional ``month_format``. You can
also pass ``day_format``, which defaults to ``"%d"`` (day of the month as a
decimal number, 1-31).
Uses the template ``app_label/module_name__archive_day`` by default. Uses the template ``app_label/module_name__archive_day`` by default.
Has the following template context: Has the following template context:
@ -177,6 +185,11 @@ The date-based generic functions are:
You can also pass the ``template_name_field`` argument to indicate that the You can also pass the ``template_name_field`` argument to indicate that the
the object stores the name of its template in a field on the object itself. the object stores the name of its template in a field on the object itself.
As in ``archive_day``, ``object_detail`` takes optional ``month_format``
and ``day_format`` parameters.
.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
Using list/detail generic views Using list/detail generic views
=============================== ===============================