2006-05-02 01:31:56 +00:00
|
|
|
# This module collects helper functions and classes that "span" multiple levels
|
|
|
|
# of MVC. In other words, these functions/classes introduce controlled coupling
|
|
|
|
# for convenience's sake.
|
|
|
|
|
|
|
|
from django.template import loader
|
|
|
|
from django.http import HttpResponse, Http404
|
2007-01-03 14:16:58 +00:00
|
|
|
from django.db.models.manager import Manager
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
def render_to_response(*args, **kwargs):
|
2007-06-17 07:11:37 +00:00
|
|
|
return HttpResponse(loader.render_to_iter(*args, **kwargs))
|
2006-05-02 01:31:56 +00:00
|
|
|
load_and_render = render_to_response # For backwards compatibility.
|
|
|
|
|
2006-05-31 12:34:05 +00:00
|
|
|
def get_object_or_404(klass, *args, **kwargs):
|
2007-01-03 14:16:58 +00:00
|
|
|
if isinstance(klass, Manager):
|
|
|
|
manager = klass
|
|
|
|
klass = manager.model
|
|
|
|
else:
|
|
|
|
manager = klass._default_manager
|
2006-05-02 01:31:56 +00:00
|
|
|
try:
|
2007-01-03 14:16:58 +00:00
|
|
|
return manager.get(*args, **kwargs)
|
2006-05-02 01:31:56 +00:00
|
|
|
except klass.DoesNotExist:
|
2007-03-20 16:48:41 +00:00
|
|
|
raise Http404('No %s matches the given query.' % klass._meta.object_name)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2006-05-31 12:36:01 +00:00
|
|
|
def get_list_or_404(klass, *args, **kwargs):
|
2007-01-03 14:16:58 +00:00
|
|
|
if isinstance(klass, Manager):
|
|
|
|
manager = klass
|
|
|
|
else:
|
|
|
|
manager = klass._default_manager
|
|
|
|
obj_list = list(manager.filter(*args, **kwargs))
|
2006-05-02 01:31:56 +00:00
|
|
|
if not obj_list:
|
2007-03-20 16:48:41 +00:00
|
|
|
raise Http404('No %s matches the given query.' % manager.model._meta.object_name)
|
2006-05-02 01:31:56 +00:00
|
|
|
return obj_list
|