Ticket #6470: nfa-urls.diff
File nfa-urls.diff, 16.2 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 234cfd1..977199a 100644
a b from django.utils.text import capfirst, get_text_list 16 16 from django.utils.translation import ugettext as _ 17 17 from django.utils.encoding import force_unicode 18 18 import sets 19 import types 19 20 20 21 HORIZONTAL, VERTICAL = 1, 2 21 22 # returns the <ul> class for a given radio_admin field … … def flatten_fieldsets(fieldsets): 56 57 field_names.append(field) 57 58 return field_names 58 59 60 def admin_perm_test(func): 61 def inner(model_admin, request, *args, **kwargs): 62 if not model_admin.admin_site.has_permission(request): 63 response = model_admin.admin_site.login(request) 64 if response: 65 return response 66 return func(model_admin, request, *args, **kwargs) 67 return inner 68 59 69 class AdminForm(object): 60 70 def __init__(self, form, fieldsets, prepopulated_fields): 61 71 self.form, self.fieldsets = form, fieldsets … … class ModelAdmin(BaseModelAdmin): 265 275 raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in your INSTALLED_APPS setting in order to use the admin application.") 266 276 if 'django.core.context_processors.auth' not in settings.TEMPLATE_CONTEXT_PROCESSORS: 267 277 raise ImproperlyConfigured("Put 'django.core.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.") 268 269 # Delegate to the appropriate method, based on the URL. 270 if url is None: 271 return self.changelist_view(request) 272 elif url.endswith('add'): 273 return self.add_view(request) 274 elif url.endswith('history'): 275 return self.history_view(request, unquote(url[:-8])) 276 elif url.endswith('delete'): 277 return self.delete_view(request, unquote(url[:-7])) 278 else: 279 return self.change_view(request, unquote(url)) 278 279 def _get_urls(self): 280 from django.conf.urls.defaults import patterns, url 281 urls_module = types.ModuleType('%s.urls' % self.__class__.__name__) 282 info = self.model._meta.app_label, self.model._meta.module_name 283 urlpatterns = patterns('', 284 url(r'^$', self.changelist_view, name='admin_%s_%s_changelist' % info), 285 url(r'^add/$', self.add_view, name='admin_%s_%s_add' % info), 286 url(r'^(.+)/history/$', self.history_view, name='admin_%s_%s_history' % info), 287 url(r'^(.+)/delete/$', self.delete_view, name='admin_%s_%s_delete' % info), 288 url(r'^(.+)/$', self.change_view, name='admin_%s_%s_change' % info), 289 ) 290 urls_module.urlpatterns = urlpatterns 291 return urls_module 292 urls = property(_get_urls) 280 293 281 294 def _media(self): 282 295 from django.conf import settings … … class ModelAdmin(BaseModelAdmin): 480 493 'content_type_id': ContentType.objects.get_for_model(model).id, 481 494 'save_as': self.save_as, 482 495 'save_on_top': self.save_on_top, 483 'root_path': self.admin_site.root_path,484 496 }) 485 497 return render_to_response(self.change_form_template or [ 486 498 "admin/%s/%s/change_form.html" % (app_label, opts.object_name.lower()), … … class ModelAdmin(BaseModelAdmin): 540 552 'media': mark_safe(media), 541 553 'inline_admin_formsets': inline_admin_formsets, 542 554 'errors': AdminErrorList(form, inline_formsets), 543 'root_path': self.admin_site.root_path,544 555 } 545 556 context.update(extra_context or {}) 546 557 return self.render_change_form(request, model, context, add=True) 558 add_view = admin_perm_test(add_view) 547 559 548 560 def change_view(self, request, object_id, extra_context=None): 549 561 "The 'change' admin view for this model." … … class ModelAdmin(BaseModelAdmin): 618 630 'media': mark_safe(media), 619 631 'inline_admin_formsets': inline_admin_formsets, 620 632 'errors': AdminErrorList(form, inline_formsets), 621 'root_path': self.admin_site.root_path,622 633 } 623 634 context.update(extra_context or {}) 624 635 return self.render_change_form(request, model, context, change=True, obj=obj) 636 change_view = admin_perm_test(change_view) 625 637 626 638 def changelist_view(self, request, extra_context=None): 627 639 "The 'change list' admin view for this model." … … class ModelAdmin(BaseModelAdmin): 648 660 'is_popup': cl.is_popup, 649 661 'cl': cl, 650 662 'has_add_permission': self.has_add_permission(request), 651 'root_path': self.admin_site.root_path,652 663 } 653 664 context.update(extra_context or {}) 654 665 return render_to_response(self.change_list_template or [ … … class ModelAdmin(BaseModelAdmin): 656 667 'admin/%s/change_list.html' % app_label, 657 668 'admin/change_list.html' 658 669 ], context, context_instance=template.RequestContext(request)) 670 changelist_view = admin_perm_test(changelist_view) 659 671 660 672 def delete_view(self, request, object_id, extra_context=None): 661 673 "The 'delete' admin view for this model." … … class ModelAdmin(BaseModelAdmin): 702 714 "deleted_objects": deleted_objects, 703 715 "perms_lacking": perms_needed, 704 716 "opts": opts, 705 "root_path": self.admin_site.root_path,706 717 } 707 718 context.update(extra_context or {}) 708 719 return render_to_response(self.delete_confirmation_template or [ … … class ModelAdmin(BaseModelAdmin): 710 721 "admin/%s/delete_confirmation.html" % app_label, 711 722 "admin/delete_confirmation.html" 712 723 ], context, context_instance=template.RequestContext(request)) 724 delete_view = admin_perm_test(delete_view) 713 725 714 726 def history_view(self, request, object_id, extra_context=None): 715 727 "The 'history' admin view for this model." … … class ModelAdmin(BaseModelAdmin): 728 740 'action_list': action_list, 729 741 'module_name': capfirst(opts.verbose_name_plural), 730 742 'object': obj, 731 'root_path': self.admin_site.root_path,732 743 } 733 744 context.update(extra_context or {}) 734 745 return render_to_response(self.object_history_template or [ … … class ModelAdmin(BaseModelAdmin): 736 747 "admin/%s/object_history.html" % opts.app_label, 737 748 "admin/object_history.html" 738 749 ], context, context_instance=template.RequestContext(request)) 750 history_view = admin_perm_test(history_view) 739 751 740 752 class InlineModelAdmin(BaseModelAdmin): 741 753 """ -
django/contrib/admin/sites.py
diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 6aa65c2..33f152e 100644
a b import base64 11 11 import cPickle as pickle 12 12 import datetime 13 13 import md5 14 import types 14 15 import re 15 16 16 17 ERROR_MESSAGE = ugettext_lazy("Please enter a correct username and password. Note that both fields are case-sensitive.") … … def _decode_post_data(encoded_data): 39 40 raise SuspiciousOperation, "User may have tampered with session cookie." 40 41 return pickle.loads(pickled) 41 42 43 def admin_perm_test(func): 44 def inner(admin_site, request, *args, **kwargs): 45 if not admin_site.has_permission(request): 46 response = admin_site.login(request) 47 if response: 48 return response 49 return func(admin_site, request, *args, **kwargs) 50 return inner 51 42 52 class AdminSite(object): 43 53 """ 44 54 An AdminSite object encapsulates an instance of the Django admin application, ready … … class AdminSite(object): 93 103 *at least one* page in the admin site. 94 104 """ 95 105 return request.user.is_authenticated() and request.user.is_staff 96 97 def root(self, request, url): 98 """ 99 Handles main URL routing for the admin app. 100 101 `url` is the remainder of the URL -- e.g. 'comments/comment/'. 102 """ 103 if request.method == 'GET' and not request.path.endswith('/'): 104 return http.HttpResponseRedirect(request.path + '/') 105 106 # Figure out the admin base URL path and stash it for later use 107 self.root_path = re.sub(re.escape(url) + '$', '', request.path) 106 107 def _get_urls(self): 108 from django.conf.urls.defaults import patterns, url, include 109 from django.core.urlresolvers import RegexURLResolver 108 110 109 url = url.rstrip('/') # Trim trailing slash, if it exists. 110 111 # The 'logout' view doesn't require that the person is logged in. 112 if url == 'logout': 113 return self.logout(request) 114 115 if not self.has_permission(request): 116 response = self.login(request) 117 if response: 118 # make sure that there is a response before returning 119 # this addresses any post data that might persist from 120 # expired sessions and continue through (#5999) 121 return response 122 123 if url == '': 124 return self.index(request) 125 elif url == 'password_change': 126 return self.password_change(request) 127 elif url == 'password_change/done': 128 return self.password_change_done(request) 129 elif url == 'jsi18n': 130 return self.i18n_javascript(request) 131 # urls starting with 'r/' are for the "show in web" links 132 elif url.startswith('r/'): 133 from django.views.defaults import shortcut 134 return shortcut(request, *url.split('/')[1:]) 135 else: 136 match = USER_CHANGE_PASSWORD_URL_RE.match(url) 137 if match: 138 return self.user_change_password(request, match.group(1)) 139 140 if '/' in url: 141 return self.model_page(request, *url.split('/', 2)) 111 urls_module = types.ModuleType('%s.urls' % self.__class__.__name__) 112 urlpatterns = patterns('', 113 url(r'^$', self.index, name='admin_index'), 114 url(r'^logout/$', self.logout, name='admin_logout'), 115 url(r'^password_change/$', self.password_change, name='admin_password_change'), 116 url(r'^password_change_done/$', self.password_change_done, name='admin_password_change_done'), 117 url(r'^jsi18n/$', self.i18n_javascript, name='admin_jsi18n'), 118 url(r'^auth/user/(\d+)/password/$', self.user_change_password, name='admin_change_password'), 119 url('^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$', 'django.views.defaults.shortcut') 120 ) 142 121 143 raise http.Http404('The requested admin page does not exist.') 122 for model, model_admin in self._registry.iteritems(): 123 urlpatterns += patterns('', 124 url('^%s/%s/' % (model._meta.app_label, model._meta.module_name), include(model_admin.urls)) 125 ) 126 urls_module.urlpatterns = urlpatterns 127 return urls_module 128 urls = property(_get_urls) 144 129 145 def model_page(self, request, app_label, model_name, rest_of_url=None):146 """147 Handles the model-specific functionality of the admin site, delegating148 to the appropriate ModelAdmin class.149 """150 from django.db import models151 model = models.get_model(app_label, model_name)152 if model is None:153 raise http.Http404("App %r, model %r, not found." % (app_label, model_name))154 try:155 admin_obj = self._registry[model]156 except KeyError:157 raise http.Http404("This model exists but has not been registered with the admin site.")158 return admin_obj(request, rest_of_url)159 model_page = never_cache(model_page)160 130 161 131 def password_change(self, request): 162 132 """ … … class AdminSite(object): 164 134 """ 165 135 from django.contrib.auth.views import password_change 166 136 return password_change(request) 137 passoword_change = admin_perm_test(password_change) 167 138 168 139 def password_change_done(self, request): 169 140 """ … … class AdminSite(object): 171 142 """ 172 143 from django.contrib.auth.views import password_change_done 173 144 return password_change_done(request) 145 password_change_done = admin_perm_test(password_change_done) 174 146 175 147 def user_change_password(self, request, id): 176 148 """ … … class AdminSite(object): 178 150 """ 179 151 from django.contrib.auth.views import user_change_password 180 152 return user_change_password(request, id) 153 user_change_password = admin_perm_test(user_change_password) 181 154 182 155 def i18n_javascript(self, request): 183 156 """ … … class AdminSite(object): 192 165 else: 193 166 from django.views.i18n import null_javascript_catalog as javascript_catalog 194 167 return javascript_catalog(request, packages='django.conf') 168 i18n_javascript = admin_perm_test(i18n_javascript) 195 169 196 170 def logout(self, request): 197 171 """ … … class AdminSite(object): 309 283 context = { 310 284 'title': _('Site administration'), 311 285 'app_list': app_list, 312 'root_path': self.root_path,313 286 } 314 287 context.update(extra_context or {}) 315 288 return render_to_response(self.index_template or 'admin/index.html', context, 316 289 context_instance=template.RequestContext(request) 317 290 ) 318 index = never_cache( index)291 index = never_cache(admin_perm_test(index)) 319 292 320 293 def display_login_form(self, request, error_message='', extra_context=None): 321 294 request.session.set_test_cookie() … … class AdminSite(object): 333 306 'app_path': request.path, 334 307 'post_data': post_data, 335 308 'error_message': error_message, 336 'root_path': self.root_path,337 309 } 338 310 context.update(extra_context or {}) 339 311 return render_to_response(self.login_template or 'admin/login.html', context, -
django/contrib/auth/admin.py
diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py index 998692a..e184353 100644
a b class UserAdmin(admin.ModelAdmin): 58 58 'opts': User._meta, 59 59 'save_as': False, 60 60 'username_help_text': User._meta.get_field('username').help_text, 61 'root_path': self.admin_site.root_path,62 61 }, context_instance=template.RequestContext(request)) 63 62 64 63 admin.site.register(Group, GroupAdmin) -
django/core/urlresolvers.py
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index ff0bcbc..ba7908b 100644
a b class RegexURLResolver(object): 204 204 # urlconf_name is a string representing the module containing urlconfs. 205 205 self.regex = re.compile(regex, re.UNICODE) 206 206 self.urlconf_name = urlconf_name 207 if not isinstance(self.urlconf_name, basestring): 208 self._urlconf_module = self.urlconf_name 207 209 self.callback = None 208 210 self.default_kwargs = default_kwargs or {} 209 211 self._reverse_dict = {} -
tests/regressiontests/admin_views/tests.py
diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 98ccab6..c79f868 100644
a b class AdminViewPermissionsTest(TestCase): 84 84 '/test_admin/admin/admin_views/article/add' 85 85 ) 86 86 self.assertRedirects(request, 87 '/test_admin/admin/admin_views/article/add/' 87 '/test_admin/admin/admin_views/article/add/', status_code=301 88 88 ) 89 89 90 90 def testLogin(self): … … class AdminViewPermissionsTest(TestCase): 320 320 self.assertRedirects(post, '/test_admin/admin/') 321 321 self.failUnlessEqual(Article.objects.all().count(), 0) 322 322 self.client.get('/test_admin/admin/logout/') 323 324 No newline at end of file 323 -
tests/regressiontests/admin_views/urls.py
diff --git a/tests/regressiontests/admin_views/urls.py b/tests/regressiontests/admin_views/urls.py index e556812..4fdfeb4 100644
a b from django.contrib import admin 3 3 4 4 urlpatterns = patterns('', 5 5 (r'^admin/doc/', include('django.contrib.admindocs.urls')), 6 (r'^admin/ (.*)', admin.site.root),6 (r'^admin/', include(admin.site.urls)), 7 7 )