Ticket #7537: dj-nfa-urlresolver.diff

File dj-nfa-urlresolver.diff, 5.2 KB (added by Kenneth Arnold, 16 years ago)

Example nfa change (Example Only)

  • sites.py

     
    3939        raise SuspiciousOperation, "User may have tampered with session cookie."
    4040    return pickle.loads(pickled)
    4141
    42 class AdminSite(object):
     42class instance_decorator(object):
     43    '''
     44    Applies one or more decorators to the given instance method.
     45
     46    Normal decorators do not work well within class definitions,
     47    because the decorated function will be passed the `self`
     48    parameter. This descriptor avoids that problem by decorating the
     49    function after it has been bound as an instance method.
     50    '''
     51    def __init__(self, *decorators):
     52        self._decorators = decorators
     53
     54    def __get__(self, instance, owner):
     55        f = new.instancemethod(self.f, instance, owner)
     56        for dec in self._decorators:
     57            f = dec(f)
     58        return f
     59
     60    def __call__(self, f):
     61        self.f = f
     62        return self
     63
     64
     65def _has_permission(request):
    4366    """
     67    Returns True if the given HttpRequest has permission to view
     68    *at least one* page in the admin site.
     69    """
     70    return request.user.is_authenticated() and request.user.is_staff
     71
     72   
     73class AdminSite(BaseRegexURLResolver):
     74    """
    4475    An AdminSite object encapsulates an instance of the Django admin application, ready
    4576    to be hooked in to your URLConf. Models are registered with the AdminSite using the
    4677    register() method, and the root() method can then be used as a Django view function
     
    5081    index_template = None
    5182    login_template = None
    5283   
    53     def __init__(self):
     84    def __init__(self, regex):
     85        super(AdminSite, self).__init__(regex)
    5486        self._registry = {} # model_class class -> admin_class instance
    5587
    5688    def register(self, model_or_iterable, admin_class=None, **options):
     
    87119                raise NotRegistered('The model %s is not registered' % model.__name__)
    88120            del self._registry[model]
    89121
    90     def has_permission(self, request):
    91         """
    92         Returns True if the given HttpRequest has permission to view
    93         *at least one* page in the admin site.
    94         """
    95         return request.user.is_authenticated() and request.user.is_staff
     122    def get_url_patterns(self):
     123        # FIXME: I don't know how to handle the "show in web" links
     124        return [
     125            url(r'^logout/$', self.logout),
     126            url(r'^$', self.index),
     127            url(r'^password_change/$', self.password_change),
     128            url(r'^password_change/done/$', self.password_change_done),
     129            url(r'jsi18n/$', self.i18n_javascript)
     130            ]
    96131
    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         # Figure out the admin base URL path and stash it for later use
    104         self.root_path = re.sub(re.escape(url) + '$', '', request.path)
    105        
    106         url = url.rstrip('/') # Trim trailing slash, if it exists.
    107 
    108         # The 'logout' view doesn't require that the person is logged in.
    109         if url == 'logout':
    110             return self.logout(request)
    111 
    112         if not self.has_permission(request):
    113             response = self.login(request)
    114             if response:
    115                 # make sure that there is a response before returning
    116                 # this addresses any post data that might persist from
    117                 # expired sessions and continue through (#5999)
    118                 return response
    119 
    120         if url == '':
    121             return self.index(request)
    122         elif url == 'password_change':
    123             return self.password_change(request)
    124         elif url == 'password_change/done':
    125             return self.password_change_done(request)
    126         elif url == 'jsi18n':
    127             return self.i18n_javascript(request)
    128         # urls starting with 'r/' are for the "show in web" links
    129         elif url.startswith('r/'):
    130             from django.views.defaults import shortcut
    131             return shortcut(request, *url.split('/')[1:])
    132         else:
    133             match = USER_CHANGE_PASSWORD_URL_RE.match(url)
    134             if match:
    135                 return self.user_change_password(request, match.group(1))
    136                
    137             if '/' in url:
    138                 return self.model_page(request, *url.split('/', 2))
    139 
    140         raise http.Http404('The requested admin page does not exist.')
    141 
    142132    def model_page(self, request, app_label, model_name, rest_of_url=None):
    143133        """
    144134        Handles the model-specific functionality of the admin site, delegating
     
    161151        """
    162152        from django.contrib.auth.views import password_change
    163153        return password_change(request)
     154    password_change = instance_decorator(password_change, _has_permission)
    164155
    165156    def password_change_done(self, request):
    166157        """
     
    168159        """
    169160        from django.contrib.auth.views import password_change_done
    170161        return password_change_done(request)
     162    password_change_done = instance_decorator(password_change_done, _has_permission)
    171163
    172164    def user_change_password(self, request, id):
    173165        """
Back to Top