Django

Code

Changeset 185

Show
Ignore:
Timestamp:
07/18/05 17:35:04 (3 years ago)
Author:
adrian
Message:

Moved default admin from media to django/conf/admin_media, so Django is able to introspect their location, in preparation for #76

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/bin/django-admin.py

    r174 r185  
    366366    "Starts a lightweight Web server for development." 
    367367    from django.core.servers.basehttp import run, WSGIServerException 
    368     from django.core.handlers.wsgi import WSGIHandler 
     368    from django.core.handlers.wsgi import AdminMediaHandler, WSGIHandler 
    369369    if not port.isdigit(): 
    370370        sys.stderr.write("Error: %r is not a valid port number.\n" % port) 
     
    372372    print "Starting server on port %s. Go to http://127.0.0.1:%s/ for Django." % (port, port) 
    373373    try: 
    374         run(int(port), WSGIHandler()) 
     374        run(int(port), AdminMediaHandler(WSGIHandler())) 
    375375    except WSGIServerException, e: 
    376376        # Use helpful error messages instead of ugly tracebacks. 
  • django/trunk/django/core/handlers/wsgi.py

    r169 r185  
    241241        import sys, traceback 
    242242        return '\n'.join(traceback.format_exception(*sys.exc_info())) 
     243 
     244class AdminMediaHandler: 
     245    """ 
     246    WSGI middleware that intercepts calls to the admin media directory, as 
     247    defined by the ADMIN_MEDIA_PREFIX setting, and serves those images. 
     248    Use this ONLY LOCALLY, for development! This hasn't been tested for 
     249    security and is not super efficient. 
     250    """ 
     251    def __init__(self, application): 
     252        from django.conf import settings 
     253        import django 
     254        self.application = application 
     255        self.media_dir = django.__path__[0] + '/conf/admin_templates' 
     256        self.media_url = settings.ADMIN_MEDIA_PREFIX 
     257 
     258    def __call__(self, environ, start_response): 
     259        import os.path 
     260 
     261        # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. 
     262        if not environ['PATH_INFO'].startswith(self.media_url): 
     263            return self.application(environ, start_response) 
     264 
     265        # Find the admin file and serve it up, if it exists and is readable. 
     266        file_path = os.path.join(self.media_dir, environ['PATH_INFO'][1:]) 
     267        if not os.path.exists(file_path): 
     268            status = '404 NOT FOUND' 
     269            headers = {'Content-type': 'text/plain'} 
     270            output = ['Page not found: %s' % file_path] 
     271        else: 
     272            try: 
     273                fp = open(file_path, 'r') 
     274            except IOError: 
     275                status = '401 UNAUTHORIZED' 
     276                headers = {'Content-type': 'text/plain'} 
     277                output = ['Permission denied: %s' % file_path] 
     278            else: 
     279                status = '200 OK' 
     280                headers = {} 
     281                output = [fp.read()] 
     282                fp.close() 
     283        start_response(status, headers.items()) 
     284        return output 
  • django/trunk/setup.py

    r155 r185  
    1414    packages = find_packages(), 
    1515    package_data = { 
    16         'django.conf': ['admin_templates/*.html', 'admin_templates/doc/*.html'], 
     16        'django.conf': ['admin_templates/*.html', 'admin_templates/doc/*.html', 
     17                        'admin_media/css/*.css', 'admin_media/img/admin/*.gif', 
     18                        'admin_media/img/admin/*.png', 'admin_media/js/*.js', 
     19                        'admin_media/js/admin/*js'], 
    1720    }, 
    1821    scripts = ['django/bin/django-admin.py'],