Ticket #6603: extra_context.diff

File extra_context.diff, 8.2 KB (added by Waldemar Kornewald, 17 years ago)

patch for trunk

  • django/views/generic/common.py

     
     1from django.db.models.query import QuerySet
     2
     3def _add_extra_context(c, raw_extra_context):
     4    for key, value in extra_context.items():
     5        if callable(value):
     6            c[key] = value()
     7        elif isinstance(extra, QuerySet):
     8            c[key] = value._clone()
     9        else:
     10            c[key] = value
  • django/views/generic/simple.py

     
    11from django.shortcuts import render_to_response
    22from django.template import loader, RequestContext
    33from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
     4from django.views.generic.common import _add_extra_context
    45
    56def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
    67    """
     
    910    """
    1011    if extra_context is None: extra_context = {}
    1112    dictionary = {'params': kwargs}
    12     for key, value in extra_context.items():
    13         if callable(value):
    14             dictionary[key] = value()
    15         else:
    16             dictionary[key] = value
     13    _add_extra_context(dictionary, extra_context)
    1714    c = RequestContext(request, dictionary)
    1815    t = loader.get_template(template)
    1916    return HttpResponse(t.render(c), mimetype=mimetype)
  • django/views/generic/date_based.py

     
    66from django.core.xheaders import populate_xheaders
    77from django.db.models.fields import DateTimeField
    88from django.http import Http404, HttpResponse
     9from django.views.generic.common import _add_extra_context
    910
    1011def archive_index(request, queryset, date_field, num_latest=15,
    1112        template_name=None, template_loader=loader,
     
    4142        'date_list' : date_list,
    4243        template_object_name : latest,
    4344    }, context_processors)
    44     for key, value in extra_context.items():
    45         if callable(value):
    46             c[key] = value()
    47         else:
    48             c[key] = value
     45    _add_extra_context(c, extra_context)
    4946    return HttpResponse(t.render(c), mimetype=mimetype)
    5047
    5148def archive_year(request, year, queryset, date_field, template_name=None,
     
    8986        'year': year,
    9087        '%s_list' % template_object_name: object_list,
    9188    }, context_processors)
    92     for key, value in extra_context.items():
    93         if callable(value):
    94             c[key] = value()
    95         else:
    96             c[key] = value
     89    _add_extra_context(c, extra_context)
    9790    return HttpResponse(t.render(c), mimetype=mimetype)
    9891
    9992def archive_month(request, year, month, queryset, date_field,
     
    155148        'next_month': next_month,
    156149        'previous_month': first_day - datetime.timedelta(days=1),
    157150    }, context_processors)
    158     for key, value in extra_context.items():
    159         if callable(value):
    160             c[key] = value()
    161         else:
    162             c[key] = value
     151    _add_extra_context(c, extra_context)
    163152    return HttpResponse(t.render(c), mimetype=mimetype)
    164153
    165154def archive_week(request, year, week, queryset, date_field,
     
    203192        '%s_list' % template_object_name: object_list,
    204193        'week': date,
    205194    })
    206     for key, value in extra_context.items():
    207         if callable(value):
    208             c[key] = value()
    209         else:
    210             c[key] = value
     195    _add_extra_context(c, extra_context)
    211196    return HttpResponse(t.render(c), mimetype=mimetype)
    212197
    213198def archive_day(request, year, month, day, queryset, date_field,
     
    267252        'previous_day': date - datetime.timedelta(days=1),
    268253        'next_day': next_day,
    269254    }, context_processors)
    270     for key, value in extra_context.items():
    271         if callable(value):
    272             c[key] = value()
    273         else:
    274             c[key] = value
     255    _add_extra_context(c, extra_context)
    275256    return HttpResponse(t.render(c), mimetype=mimetype)
    276257
    277258def archive_today(request, **kwargs):
     
    336317    c = RequestContext(request, {
    337318        template_object_name: obj,
    338319    }, context_processors)
    339     for key, value in extra_context.items():
    340         if callable(value):
    341             c[key] = value()
    342         else:
    343             c[key] = value
     320    _add_extra_context(c, extra_context)
    344321    response = HttpResponse(t.render(c), mimetype=mimetype)
    345322    populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name))
    346323    return response
  • django/views/generic/list_detail.py

     
    33from django.core.xheaders import populate_xheaders
    44from django.core.paginator import ObjectPaginator, InvalidPage
    55from django.core.exceptions import ObjectDoesNotExist
     6from django.views.generic.common import _add_extra_context
    67
    78def object_list(request, queryset, paginate_by=None, page=None,
    89        allow_empty=True, template_name=None, template_loader=loader,
     
    8586        }, context_processors)
    8687        if not allow_empty and len(queryset) == 0:
    8788            raise Http404
    88     for key, value in extra_context.items():
    89         if callable(value):
    90             c[key] = value()
    91         else:
    92             c[key] = value
     89    _add_extra_context(c, extra_context)
    9390    if not template_name:
    9491        model = queryset.model
    9592        template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     
    131128    c = RequestContext(request, {
    132129        template_object_name: obj,
    133130    }, context_processors)
    134     for key, value in extra_context.items():
    135         if callable(value):
    136             c[key] = value()
    137         else:
    138             c[key] = value
     131    _add_extra_context(c, extra_context)
    139132    response = HttpResponse(t.render(c), mimetype=mimetype)
    140133    populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name))
    141134    return response
  • django/views/generic/create_update.py

     
    77from django.http import Http404, HttpResponse, HttpResponseRedirect
    88from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
    99from django.utils.translation import ugettext
     10from django.views.generic.common import _add_extra_context
    1011
    1112def create_object(request, model, template_name=None,
    1213        template_loader=loader, extra_context=None, post_save_redirect=None,
     
    6364    c = RequestContext(request, {
    6465        'form': form,
    6566    }, context_processors)
    66     for key, value in extra_context.items():
    67         if callable(value):
    68             c[key] = value()
    69         else:
    70             c[key] = value
     67    _add_extra_context(c, extra_context)
    7168    return HttpResponse(t.render(c))
    7269
    7370def update_object(request, model, object_id=None, slug=None,
     
    136133        'form': form,
    137134        template_object_name: object,
    138135    }, context_processors)
    139     for key, value in extra_context.items():
    140         if callable(value):
    141             c[key] = value()
    142         else:
    143             c[key] = value
     136    _add_extra_context(c, extra_context)
    144137    response = HttpResponse(t.render(c))
    145138    populate_xheaders(request, response, model, getattr(object, object._meta.pk.attname))
    146139    return response
     
    190183        c = RequestContext(request, {
    191184            template_object_name: object,
    192185        }, context_processors)
    193         for key, value in extra_context.items():
    194             if callable(value):
    195                 c[key] = value()
    196             else:
    197                 c[key] = value
     186        _add_extra_context(c, extra_context)
    198187        response = HttpResponse(t.render(c))
    199188        populate_xheaders(request, response, model, getattr(object, object._meta.pk.attname))
    200189        return response
Back to Top