Fixed #195: generic views now allow callables in extra_context dicts -- thanks, Moof!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2005-07-25 21:53:00 +00:00
parent f6c4395329
commit 6c43b0e06c
3 changed files with 60 additions and 30 deletions

View File

@ -8,7 +8,7 @@ from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured
def create_object(request, app_label, module_name, template_name=None,
extra_context=None, post_save_redirect=None, login_required=False):
extra_context={}, post_save_redirect=None, login_required=False):
"""
Generic object-creation function.
@ -57,13 +57,16 @@ def create_object(request, app_label, module_name, template_name=None,
c = Context(request, {
'form' : form,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
return HttpResponse(t.render(c))
def update_object(request, app_label, module_name, object_id=None, slug=None,
slug_field=None, template_name=None, extra_lookup_kwargs={},
extra_context=None, post_save_redirect=None, login_required=False):
extra_context={}, post_save_redirect=None, login_required=False):
"""
Generic object-update function.
@ -125,15 +128,18 @@ def update_object(request, app_label, module_name, object_id=None, slug=None,
'form' : form,
'object' : object,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
response = HttpResponse(t.render(c))
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
return response
def delete_object(request, app_label, module_name, post_delete_redirect,
object_id=None, slug=None, slug_field=None, template_name=None,
extra_lookup_kwargs={}, extra_context=None, login_required=False):
extra_lookup_kwargs={}, extra_context={}, login_required=False):
"""
Generic object-delete function.
@ -177,8 +183,11 @@ def delete_object(request, app_label, module_name, post_delete_redirect,
c = Context(request, {
'object' : object,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
response = HttpResponse(t.render(c))
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
return response

View File

@ -7,7 +7,7 @@ from django.utils.httpwrappers import HttpResponse
import datetime, time
def archive_index(request, app_label, module_name, date_field, num_latest=15,
template_name=None, extra_lookup_kwargs={}, extra_context=None):
template_name=None, extra_lookup_kwargs={}, extra_context={}):
"""
Generic top-level archive of date-based objects.
@ -41,12 +41,15 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
'date_list' : date_list,
'latest' : latest,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
return HttpResponse(t.render(c))
def archive_year(request, year, app_label, module_name, date_field,
template_name=None, extra_lookup_kwargs={}, extra_context=None):
template_name=None, extra_lookup_kwargs={}, extra_context={}):
"""
Generic yearly archive view.
@ -74,12 +77,15 @@ def archive_year(request, year, app_label, module_name, date_field,
'date_list': date_list,
'year': year,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
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=None):
template_name=None, extra_lookup_kwargs={}, extra_context={}):
"""
Generic monthly archive view.
@ -121,12 +127,15 @@ def archive_month(request, year, month, app_label, module_name, date_field,
'object_list': object_list,
'month': date,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
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=None,
template_name=None, extra_lookup_kwargs={}, extra_context={},
allow_empty=False):
"""
Generic daily archive view.
@ -167,8 +176,11 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
'previous_day': date - datetime.timedelta(days=1),
'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
return HttpResponse(t.render(c))
def archive_today(request, **kwargs):
@ -185,7 +197,7 @@ def archive_today(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=None):
template_name_field=None, extra_lookup_kwargs={}, extra_context={}):
"""
Generic detail view from year/month/day/slug or year/month/day/id structure.
@ -227,8 +239,11 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
c = Context(request, {
'object': object,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
response = HttpResponse(t.render(c))
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
return response

View File

@ -7,7 +7,7 @@ from django.core.paginator import ObjectPaginator, InvalidPage
from django.core.exceptions import Http404, ObjectDoesNotExist
def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False,
template_name=None, extra_lookup_kwargs={}, extra_context=None):
template_name=None, extra_lookup_kwargs={}, extra_context={}):
"""
Generic list of objects.
@ -61,8 +61,11 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
})
if len(object_list) == 0 and not allow_empty:
raise Http404
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
if not template_name:
template_name = "%s/%s_list" % (app_label, module_name)
t = template_loader.get_template(template_name)
@ -70,7 +73,7 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
def object_detail(request, app_label, module_name, object_id=None, slug=None,
slug_field=None, template_name=None, template_name_field=None,
extra_lookup_kwargs={}, extra_context=None):
extra_lookup_kwargs={}, extra_context={}):
"""
Generic list of objects.
@ -102,8 +105,11 @@ def object_detail(request, app_label, module_name, object_id=None, slug=None,
c = Context(request, {
'object' : object,
})
if extra_context:
c.update(extra_context)
for key, value in extra_context.items():
if callable(value):
c[key] = value()
else:
c[key] = value
response = HttpResponse(t.render(c))
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
return response