Django

Code

Changeset 491

Show
Ignore:
Timestamp:
08/11/05 23:16:29 (3 years ago)
Author:
adrian
Message:

Moved django.core.handlers.wsgi.AdminMediaHandler? to django.core.servers.basehttp. Makes more sense to have it in there, because its only use is for the development server.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/handlers/wsgi.py

    r478 r491  
    145145        start_response(status, response_headers.items()) 
    146146        return output 
    147  
    148 class AdminMediaHandler: 
    149     """ 
    150     WSGI middleware that intercepts calls to the admin media directory, as 
    151     defined by the ADMIN_MEDIA_PREFIX setting, and serves those images. 
    152     Use this ONLY LOCALLY, for development! This hasn't been tested for 
    153     security and is not super efficient. 
    154     """ 
    155     def __init__(self, application): 
    156         from django.conf import settings 
    157         import django 
    158         self.application = application 
    159         self.media_dir = django.__path__[0] + '/conf/admin_media' 
    160         self.media_url = settings.ADMIN_MEDIA_PREFIX 
    161  
    162     def __call__(self, environ, start_response): 
    163         import os.path 
    164  
    165         # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore 
    166         # all requests if ADMIN_MEDIA_PREFIX isn't a relative URL. 
    167         if self.media_url.startswith('http://') or self.media_url.startswith('https://') \ 
    168             or not environ['PATH_INFO'].startswith(self.media_url): 
    169             return self.application(environ, start_response) 
    170  
    171         # Find the admin file and serve it up, if it exists and is readable. 
    172         relative_url = environ['PATH_INFO'][len(self.media_url):] 
    173         file_path = os.path.join(self.media_dir, relative_url) 
    174         if not os.path.exists(file_path): 
    175             status = '404 NOT FOUND' 
    176             headers = {'Content-type': 'text/plain'} 
    177             output = ['Page not found: %s' % file_path] 
    178         else: 
    179             try: 
    180                 fp = open(file_path, 'r') 
    181             except IOError: 
    182                 status = '401 UNAUTHORIZED' 
    183                 headers = {'Content-type': 'text/plain'} 
    184                 output = ['Permission denied: %s' % file_path] 
    185             else: 
    186                 status = '200 OK' 
    187                 headers = {} 
    188                 output = [fp.read()] 
    189                 fp.close() 
    190         start_response(status, headers.items()) 
    191         return output 
  • django/trunk/django/core/management.py

    r470 r491  
    486486def runserver(port): 
    487487    "Starts a lightweight Web server for development." 
    488     from django.core.servers.basehttp import run, WSGIServerException 
    489     from django.core.handlers.wsgi import AdminMediaHandler, WSGIHandler 
     488    from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException 
     489    from django.core.handlers.wsgi import WSGIHandler 
    490490    if not port.isdigit(): 
    491491        sys.stderr.write("Error: %r is not a valid port number.\n" % port) 
  • django/trunk/django/core/servers/basehttp.py

    r320 r491  
    592592        sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), format % args)) 
    593593 
     594class AdminMediaHandler: 
     595    """ 
     596    WSGI middleware that intercepts calls to the admin media directory, as 
     597    defined by the ADMIN_MEDIA_PREFIX setting, and serves those images. 
     598    Use this ONLY LOCALLY, for development! This hasn't been tested for 
     599    security and is not super efficient. 
     600    """ 
     601    def __init__(self, application): 
     602        from django.conf import settings 
     603        import django 
     604        self.application = application 
     605        self.media_dir = django.__path__[0] + '/conf/admin_media' 
     606        self.media_url = settings.ADMIN_MEDIA_PREFIX 
     607 
     608    def __call__(self, environ, start_response): 
     609        import os.path 
     610 
     611        # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore 
     612        # all requests if ADMIN_MEDIA_PREFIX isn't a relative URL. 
     613        if self.media_url.startswith('http://') or self.media_url.startswith('https://') \ 
     614            or not environ['PATH_INFO'].startswith(self.media_url): 
     615            return self.application(environ, start_response) 
     616 
     617        # Find the admin file and serve it up, if it exists and is readable. 
     618        relative_url = environ['PATH_INFO'][len(self.media_url):] 
     619        file_path = os.path.join(self.media_dir, relative_url) 
     620        if not os.path.exists(file_path): 
     621            status = '404 NOT FOUND' 
     622            headers = {'Content-type': 'text/plain'} 
     623            output = ['Page not found: %s' % file_path] 
     624        else: 
     625            try: 
     626                fp = open(file_path, 'r') 
     627            except IOError: 
     628                status = '401 UNAUTHORIZED' 
     629                headers = {'Content-type': 'text/plain'} 
     630                output = ['Permission denied: %s' % file_path] 
     631            else: 
     632                status = '200 OK' 
     633                headers = {} 
     634                output = [fp.read()] 
     635                fp.close() 
     636        start_response(status, headers.items()) 
     637        return output 
     638 
    594639def run(port, wsgi_handler): 
    595640    server_address = ('', port)