Skip to content

Commit

Permalink
Fixed #196: date-based generic views now have a "use_numeric_months" …
Browse files Browse the repository at this point in the history
…option if you'd like to use numeric months in the urls. Also fixed #183 while I was at it.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@308 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Jul 25, 2005
1 parent 6c43b0e commit 5498dbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
63 changes: 43 additions & 20 deletions django/views/generic/date_based.py
Expand Up @@ -85,7 +85,8 @@ def archive_year(request, year, app_label, module_name, date_field,
return HttpResponse(t.render(c))

def archive_month(request, year, month, app_label, module_name, date_field,
template_name=None, extra_lookup_kwargs={}, extra_context={}):
use_numeric_months=False, template_name=None,
extra_lookup_kwargs={}, extra_context={}):
"""
Generic monthly archive view.
Expand All @@ -96,10 +97,17 @@ def archive_month(request, year, month, app_label, module_name, date_field,
object_list:
list of objects published in the given month
"""
try:
date = datetime.date(*time.strptime(year+month, '%Y%b')[:3])
except ValueError:
raise Http404
if use_numeric_months:
try:
date = datetime.date(int(year), int(month), 1)
except (ValueError, TypeError):
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)
now = datetime.datetime.now()
# Calculate first and last day of month, for use in a date-range lookup.
Expand All @@ -114,7 +122,7 @@ def archive_month(request, year, month, app_label, module_name, date_field,
break
lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
# Only bother to check current date if the month isn't in the past.
if date >= now:
if last_day >= now.date():
lookup_kwargs['%s__lte' % date_field] = now
lookup_kwargs.update(extra_lookup_kwargs)
object_list = mod.get_list(**lookup_kwargs)
Expand All @@ -135,8 +143,8 @@ def archive_month(request, year, month, app_label, module_name, date_field,
return HttpResponse(t.render(c))

def archive_day(request, year, month, day, app_label, module_name, date_field,
template_name=None, extra_lookup_kwargs={}, extra_context={},
allow_empty=False):
use_numeric_months=False, template_name=None, extra_lookup_kwargs={},
extra_context={}, allow_empty=False):
"""
Generic daily archive view.
Expand All @@ -151,17 +159,24 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
next_day
(datetime) the next day, or None if the current day is today
"""
try:
date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
except ValueError:
raise Http404
if use_numeric_months:
try:
date = datetime.date(int(year), int(month), int(day))
except (ValueError, TypeError):
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)
now = datetime.datetime.now()
lookup_kwargs = {
'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
}
# Only bother to check current date if the date isn't in the past.
if date >= now:
if date >= now.date():
lookup_kwargs['%s__lte' % date_field] = now
lookup_kwargs.update(extra_lookup_kwargs)
object_list = mod.get_list(**lookup_kwargs)
Expand Down Expand Up @@ -196,8 +211,9 @@ def archive_today(request, **kwargs):
return archive_day(request, **kwargs)

def object_detail(request, year, month, day, app_label, module_name, date_field,
object_id=None, slug=None, slug_field=None, template_name=None,
template_name_field=None, extra_lookup_kwargs={}, extra_context={}):
use_numeric_months=False, object_id=None, slug=None, slug_field=None,
template_name=None, template_name_field=None, extra_lookup_kwargs={},
extra_context={}):
"""
Generic detail view from year/month/day/slug or year/month/day/id structure.
Expand All @@ -206,17 +222,24 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
object:
the object to be detailed
"""
try:
date = datetime.datetime(*time.strptime(year+month+day, '%Y%b%d')[:3])
except ValueError:
raise Http404
if use_numeric_months:
try:
date = datetime.date(int(year), int(month), int(day))
except (ValueError, TypeError):
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)
now = datetime.datetime.now()
lookup_kwargs = {
'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
}
# Only bother to check current date if the date isn't in the past.
if date >= now:
if date >= now.date():
lookup_kwargs['%s__lte' % date_field] = now
if object_id:
lookup_kwargs['%s__exact' % mod.Klass._meta.pk.name] = object_id
Expand Down
5 changes: 4 additions & 1 deletion docs/generic_views.txt
Expand Up @@ -126,7 +126,10 @@ The date-based generic functions are:

``archive_month``
Monthly archive. Requires that ``year`` and ``month`` arguments be given.

You may pass the additional option ``use_numeric_months`` if you'd like to
use URLs that use numbers instead of names for months (i.e. ``/2005/01/15/``
instead of ``/2005/jan/15/``.

Uses the template ``app_label/module_name__archive_month`` by default.

Has the following template context:
Expand Down

0 comments on commit 5498dbe

Please sign in to comment.