Ticket #925: request_processors_patch.diff
File request_processors_patch.diff, 15.6 KB (added by , 19 years ago) |
---|
-
django/conf/global_settings.py
174 174 # http://www.djangoproject.com/documentation/templates/#now 175 175 TIME_FORMAT = 'P' 176 176 177 # List of processors used by DjangoContext to populate the context. 178 # Each one should be a callable that takes the request object as it's 179 # only parameter and returns a dictionary to add to the context. 180 REQUEST_PROCESSORS = ( 181 "django.core.processors.auth", 182 "django.core.processors.debug", 183 "django.core.processors.i18n", 184 ) 185 177 186 ############## 178 187 # MIDDLEWARE # 179 188 ############## -
django/core/extensions.py
2 2 # of MVC. In other words, these functions/classes introduce controlled coupling 3 3 # for convenience's sake. 4 4 5 from django.core.exceptions import Http404, ObjectDoesNotExist 5 from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured 6 6 from django.core.template import Context, loader 7 from django.conf.settings import DEBUG, INTERNAL_IPS7 from django.conf.settings import REQUEST_PROCESSORS 8 8 from django.utils.httpwrappers import HttpResponse 9 9 10 # Load standard request processors 11 _standard_request_processors = [] 12 13 for path in REQUEST_PROCESSORS: 14 i = path.rfind('.') 15 module, attr = path[:i], path[i+1:] 16 try: 17 mod = __import__(module, globals(), locals(), [attr]) 18 except ImportError, e: 19 raise ImproperlyConfigured, 'Error importing request processor module %s: "%s"' % (module, e) 20 try: 21 func = getattr(mod, attr) 22 except AttributeError: 23 raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable request processor' % (module, attr) 24 _standard_request_processors.append(func) 25 10 26 def render_to_response(*args, **kwargs): 11 27 return HttpResponse(loader.render_to_string(*args, **kwargs)) 12 28 load_and_render = render_to_response # For backwards compatibility. … … 25 41 26 42 class DjangoContext(Context): 27 43 """ 28 This subclass of template.Context automatically populates 'user' and 29 'messages' in the context. 44 This subclass of template.Context automatically populates itself using 45 the request processors defined in REQUEST_PROCESSORS. 46 Additional processors can be specified as a list of callables 47 using keyword argument 'processors'. 30 48 """ 31 def __init__(self, request, dict=None ):49 def __init__(self, request, dict=None, processors=None): 32 50 Context.__init__(self, dict) 33 self['user'] = request.user 34 self['messages'] = request.user.get_and_delete_messages() 35 self['perms'] = PermWrapper(request.user) 36 from django.conf import settings 37 self['LANGUAGES'] = settings.LANGUAGES 38 if hasattr(request, 'LANGUAGE_CODE'): 39 self['LANGUAGE_CODE'] = request.LANGUAGE_CODE 51 if processors is None: 52 processors = [] 40 53 else: 41 self['LANGUAGE_CODE'] = settings.LANGUAGE_CODE 42 if DEBUG and request.META.get('REMOTE_ADDR') in INTERNAL_IPS: 43 self['debug'] = True 44 from django.core import db 45 self['sql_queries'] = db.db.queries 46 47 # PermWrapper and PermLookupDict proxy the permissions system into objects that 48 # the template system can understand. 49 50 class PermLookupDict: 51 def __init__(self, user, module_name): 52 self.user, self.module_name = user, module_name 53 def __repr__(self): 54 return str(self.user.get_permission_list()) 55 def __getitem__(self, perm_name): 56 return self.user.has_perm("%s.%s" % (self.module_name, perm_name)) 57 def __nonzero__(self): 58 return self.user.has_module_perms(self.module_name) 59 60 class PermWrapper: 61 def __init__(self, user): 62 self.user = user 63 def __getitem__(self, module_name): 64 return PermLookupDict(self.user, module_name) 54 processors = list(processors) 55 56 for processor in processors + _standard_request_processors: 57 self.update(processor(request)) -
django/core/processors.py
1 """ 2 A set of request processors that return dictionaries to be merged 3 into the context for templates. Each function takes 4 5 These are referenced from the setting REQUEST_PROCESSORS and used 6 in turn by DjangoContext 7 """ 8 9 from django.conf.settings import DEBUG, INTERNAL_IPS, LANGUAGES, LANGUAGE_CODE 10 11 def auth(request): 12 """ 13 Returns a dictionary of context variables required by the 14 admin app and other apps that use Django's authentication system. 15 """ 16 return { 17 'user': request.user, 18 'messages': request.user.get_and_delete_messages(), 19 'perms': PermWrapper(request.user), 20 } 21 22 def debug(request): 23 """ 24 Returns a dictionary of context variables required 25 for debugging. 26 """ 27 context_extras = {} 28 if DEBUG and request.META.get('REMOTE_ADDR') in INTERNAL_IPS: 29 context_extras['debug'] = True 30 from django.core import db 31 context_extras['sql_queries'] = db.db.queries 32 return context_extras 33 34 def i18n(request): 35 context_extras = {} 36 context_extras['LANGUAGES'] = LANGUAGES 37 if hasattr(request, 'LANGUAGE_CODE'): 38 context_extras['LANGUAGE_CODE'] = request.LANGUAGE_CODE 39 else: 40 context_extras['LANGUAGE_CODE'] = LANGUAGE_CODE 41 return context_extras 42 43 # PermWrapper and PermLookupDict proxy the permissions system into objects that 44 # the template system can understand. 45 46 class PermLookupDict: 47 def __init__(self, user, module_name): 48 self.user, self.module_name = user, module_name 49 def __repr__(self): 50 return str(self.user.get_permission_list()) 51 def __getitem__(self, perm_name): 52 return self.user.has_perm("%s.%s" % (self.module_name, perm_name)) 53 def __nonzero__(self): 54 return self.user.has_module_perms(self.module_name) 55 56 class PermWrapper: 57 def __init__(self, user): 58 self.user = user 59 def __getitem__(self, module_name): 60 return PermLookupDict(self.user, module_name) -
django/views/generic/date_based.py
8 8 9 9 def archive_index(request, app_label, module_name, date_field, num_latest=15, 10 10 template_name=None, template_loader=template_loader, 11 extra_lookup_kwargs={}, extra_context={} ):11 extra_lookup_kwargs={}, extra_context={}, processors=None): 12 12 """ 13 13 Generic top-level archive of date-based objects. 14 14 … … 41 41 c = Context(request, { 42 42 'date_list' : date_list, 43 43 'latest' : latest, 44 } )44 }, processors) 45 45 for key, value in extra_context.items(): 46 46 if callable(value): 47 47 c[key] = value() … … 51 51 52 52 def archive_year(request, year, app_label, module_name, date_field, 53 53 template_name=None, template_loader=template_loader, 54 extra_lookup_kwargs={}, extra_context={} ):54 extra_lookup_kwargs={}, extra_context={}, processors=None): 55 55 """ 56 56 Generic yearly archive view. 57 57 … … 78 78 c = Context(request, { 79 79 'date_list': date_list, 80 80 'year': year, 81 } )81 }, processors) 82 82 for key, value in extra_context.items(): 83 83 if callable(value): 84 84 c[key] = value() … … 88 88 89 89 def archive_month(request, year, month, app_label, module_name, date_field, 90 90 month_format='%b', template_name=None, template_loader=template_loader, 91 extra_lookup_kwargs={}, extra_context={} ):91 extra_lookup_kwargs={}, extra_context={}, processors=None): 92 92 """ 93 93 Generic monthly archive view. 94 94 … … 130 130 c = Context(request, { 131 131 'object_list': object_list, 132 132 'month': date, 133 } )133 }, processors) 134 134 for key, value in extra_context.items(): 135 135 if callable(value): 136 136 c[key] = value() … … 141 141 def archive_day(request, year, month, day, app_label, module_name, date_field, 142 142 month_format='%b', day_format='%d', template_name=None, 143 143 template_loader=template_loader, extra_lookup_kwargs={}, 144 extra_context={}, allow_empty=False ):144 extra_context={}, allow_empty=False, processors=None): 145 145 """ 146 146 Generic daily archive view. 147 147 … … 181 181 'day': date, 182 182 'previous_day': date - datetime.timedelta(days=1), 183 183 'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None, 184 } )184 }, processors) 185 185 for key, value in extra_context.items(): 186 186 if callable(value): 187 187 c[key] = value() … … 205 205 month_format='%b', day_format='%d', object_id=None, slug=None, 206 206 slug_field=None, template_name=None, template_name_field=None, 207 207 template_loader=template_loader, extra_lookup_kwargs={}, 208 extra_context={} ):208 extra_context={}, processors=None): 209 209 """ 210 210 Generic detail view from year/month/day/slug or year/month/day/id structure. 211 211 … … 247 247 t = template_loader.get_template(template_name) 248 248 c = Context(request, { 249 249 'object': object, 250 } )250 }, processors) 251 251 for key, value in extra_context.items(): 252 252 if callable(value): 253 253 c[key] = value() -
django/views/generic/list_detail.py
8 8 9 9 def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False, 10 10 template_name=None, template_loader=template_loader, 11 extra_lookup_kwargs={}, extra_context={} ):11 extra_lookup_kwargs={}, extra_context={}, processors=None): 12 12 """ 13 13 Generic list of objects. 14 14 … … 59 59 'previous': page - 1, 60 60 'pages': paginator.pages, 61 61 'hits' : paginator.hits, 62 } )62 }, processors) 63 63 else: 64 64 object_list = mod.get_list(**lookup_kwargs) 65 65 c = Context(request, { 66 66 'object_list': object_list, 67 67 'is_paginated': False 68 } )68 }, processors) 69 69 if len(object_list) == 0 and not allow_empty: 70 70 raise Http404 71 71 for key, value in extra_context.items(): … … 81 81 def object_detail(request, app_label, module_name, object_id=None, slug=None, 82 82 slug_field=None, template_name=None, template_name_field=None, 83 83 template_loader=template_loader, extra_lookup_kwargs={}, 84 extra_context={} ):84 extra_context={}, processors=None): 85 85 """ 86 86 Generic list of objects. 87 87 … … 112 112 t = template_loader.get_template(template_name) 113 113 c = Context(request, { 114 114 'object': object, 115 } )115 }, processors) 116 116 for key, value in extra_context.items(): 117 117 if callable(value): 118 118 c[key] = value() -
django/views/generic/create_update.py
9 9 10 10 def create_object(request, app_label, module_name, template_name=None, 11 11 template_loader=template_loader, extra_context={}, 12 post_save_redirect=None, login_required=False, follow=None): 12 post_save_redirect=None, login_required=False, follow=None, 13 processors=None): 13 14 """ 14 15 Generic object-creation function. 15 16 … … 58 59 t = template_loader.get_template(template_name) 59 60 c = Context(request, { 60 61 'form' : form, 61 } )62 }, processors) 62 63 for key, value in extra_context.items(): 63 64 if callable(value): 64 65 c[key] = value() … … 69 70 def update_object(request, app_label, module_name, object_id=None, slug=None, 70 71 slug_field=None, template_name=None, template_loader=template_loader, 71 72 extra_lookup_kwargs={}, extra_context={}, post_save_redirect=None, 72 login_required=False, follow=None ):73 login_required=False, follow=None, processors=None): 73 74 """ 74 75 Generic object-update function. 75 76 … … 130 131 c = Context(request, { 131 132 'form' : form, 132 133 'object' : object, 133 } )134 }, processors) 134 135 for key, value in extra_context.items(): 135 136 if callable(value): 136 137 c[key] = value() … … 143 144 def delete_object(request, app_label, module_name, post_delete_redirect, 144 145 object_id=None, slug=None, slug_field=None, template_name=None, 145 146 template_loader=template_loader, extra_lookup_kwargs={}, 146 extra_context={}, login_required=False ):147 extra_context={}, login_required=False, processors=None): 147 148 """ 148 149 Generic object-delete function. 149 150 … … 186 187 t = template_loader.get_template(template_name) 187 188 c = Context(request, { 188 189 'object' : object, 189 } )190 }, processors) 190 191 for key, value in extra_context.items(): 191 192 if callable(value): 192 193 c[key] = value() -
docs/settings.txt
478 478 only used if ``CommonMiddleware`` is installed (see the `middleware docs`_). 479 479 See also ``APPEND_SLASH``. 480 480 481 REQUEST_PROCESSORS 482 ------------------ 483 484 Default:: 485 486 ("django.core.processors.auth", 487 "django.core.processors.debug", 488 "django.core.processors.i18n") 489 490 A tuple of callables that are used by DjangoContext to populate itself. 491 These callables take a request object as their argument and return a 492 dictionary of items to be merged into the context. 493 481 494 SECRET_KEY 482 495 ---------- 483 496 -
docs/authentication.txt
434 434 ----------- 435 435 436 436 The currently logged-in user's permissions are stored in the template variable 437 ``{{ perms }}``. This is an instance of ``django.core. extensions.PermWrapper``,437 ``{{ perms }}``. This is an instance of ``django.core.processors.PermWrapper``, 438 438 which is a template-friendly proxy of permissions. 439 439 440 440 In the ``{{ perms }}`` object, single-attribute lookup is a proxy to -
docs/templates_python.txt
249 249 logged in). See the `user authentication docs`. 250 250 * ``messages`` -- A list of ``auth.Message`` objects for the currently 251 251 logged-in user. 252 * ``perms`` -- An instance of ``django.core. extensions.PermWrapper``,252 * ``perms`` -- An instance of ``django.core.processors.PermWrapper``, 253 253 representing the permissions that the currently logged-in user has. See 254 254 the `permissions docs`_. 255 255