Django

Code

Changeset 4942

Show
Ignore:
Timestamp:
04/06/07 21:28:17 (2 years ago)
Author:
adrian
Message:

newforms-admin: Implemented the rest of the admin site (model-specific pages) in the new AdminSite? class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/contrib/admin/sites.py

    r4643 r4942  
    5454class AdminSite(object): 
    5555    def __init__(self): 
    56         self._registry = {} # model_class -> admin_class 
     56        self._registry = {} # model_class class -> admin_class instance 
    5757 
    5858    def register(self, model_or_iterable, admin_class=None, **options): 
    5959        """ 
    6060        Registers the given model(s) with the given admin class. 
     61 
     62        The model(s) should be Model classes, not instances. 
    6163 
    6264        If an admin class isn't given, it will use ModelAdmin (the default 
     
    7375            if model in self._registry: 
    7476                raise AlreadyRegistered('The model %s is already registered' % model.__class__.__name__) 
    75             self._registry[model] = admin_class 
     77            self._registry[model] = admin_class(model) 
    7678 
    7779    def unregister(self, model_or_iterable): 
     
    101103        `url` is the remainder of the URL -- e.g. 'comments/comment/'. 
    102104        """ 
     105        url = url.rstrip('/') # Trim trailing slash, if it exists. 
     106 
     107        # The 'logout' view doesn't require that the person is logged in. 
     108        if url == 'logout': 
     109            return self.logout(request) 
     110 
    103111        if not self.has_permission(request): 
    104112            return self.login(request) 
    105         url = url.rstrip('/') # Trim trailing slash, if it exists. 
     113 
    106114        if url == '': 
    107115            return self.index(request) 
     
    110118        elif url == 'password_change/done': 
    111119            return self.password_change_done(request) 
    112         raise NotImplementedError('Only the admin index page is implemented.') 
     120        elif url == 'jsi18n': 
     121            return self.i18n_javascript(request) 
     122        elif '/' in url: 
     123            return self.model_page(request, *url.split('/', 2)) 
     124 
     125        raise http.Http404('The requested admin page does not exist.') 
     126 
     127    def model_page(self, request, app_label, model_name, rest_of_url=None): 
     128        """ 
     129        Handles the model-specific functionality of the admin site, delegating 
     130        to the appropriate ModelAdmin class. 
     131        """ 
     132        from django.db import models 
     133        model = models.get_model(app_label, model_name) 
     134        if model is None: 
     135            raise http.Http404("App %r, model %r, not found." % (app_label, model_name)) 
     136        try: 
     137            admin_obj = self._registry[model] 
     138        except KeyError: 
     139            raise http.Http404("This model exists but has not been registered with the admin site.") 
     140        return admin_obj(request, rest_of_url) 
    113141 
    114142    def password_change(self, request): 
     143        """ 
     144        Handles the "change password" task -- both form display and validation. 
     145        """ 
    115146        from django.contrib.auth.views import password_change 
    116147        return password_change(request) 
    117148 
    118149    def password_change_done(self, request): 
     150        """ 
     151        Displays the "success" page after a password change. 
     152        """ 
    119153        from django.contrib.auth.views import password_change_done 
    120154        return password_change_done(request) 
     155 
     156    def i18n_javascript(self, request): 
     157        """ 
     158        Displays the i18n JavaScript that the Django admin requires. 
     159 
     160        This takes into account the USE_I18N setting. If it's set to False, the 
     161        generated JavaScript will be leaner and faster. 
     162        """ 
     163        from django.conf import settings 
     164        if settings.USE_I18N: 
     165            from django.views.i18n import javascript_catalog 
     166        else: 
     167            from django.views.i18n import null_javascript_catalog as javascript_catalog 
     168        return javascript_catalog(request, packages='django.conf') 
     169 
     170    def logout(self, request): 
     171        """ 
     172        Logs out the user for the given HttpRequest. 
     173 
     174        This should *not* assume the user is already logged in. 
     175        """ 
     176        from django.contrib.auth.views import logout 
     177        return logout(request) 
    121178 
    122179    def login(self, request): 
     
    174231 
    175232    def index(self, request): 
     233        """ 
     234        Displays the main admin index page, which lists all of the installed 
     235        apps that have been registered in this site. 
     236        """ 
    176237        app_list = [] 
    177238        user = request.user