Ticket #2557: basehttp-GenerellMediaHandler.diff

File basehttp-GenerellMediaHandler.diff, 2.0 KB (added by dummy@…, 18 years ago)
  • django/core/servers/basehttp.py

     
    587587            return
    588588        sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), format % args))
    589589
     590# At compile time, cache the directories to search.
     591from django.conf import settings
     592app_media_dirs = []
     593for app in settings.INSTALLED_APPS:
     594    i = app.rfind('.')
     595    if i == -1:
     596        m, a = app, None
     597    else:
     598        m, a = app[:i], app[i+1:]
     599    try:
     600        if a is None:
     601            mod = __import__(m, '', '', [])
     602        else:
     603            mod = getattr(__import__(m, '', '', [a]), a)
     604    except ImportError, e:
     605        raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0])
     606    media_dir = os.path.join(os.path.dirname(mod.__file__), 'media')
     607    if os.path.isdir(media_dir):
     608        app_media_dirs.append(media_dir)
     609
     610# It won't change, so convert it to a tuple to save memory.
     611app_media_dirs = tuple(app_media_dirs)
     612
    590613class AdminMediaHandler(object):
    591614    """
    592615    WSGI middleware that intercepts calls to the admin media directory, as
     
    598621        from django.conf import settings
    599622        import django
    600623        self.application = application
    601         self.media_dir = django.__path__[0] + '/contrib/admin/media'
    602624        self.media_url = settings.ADMIN_MEDIA_PREFIX
    603625
    604626    def __call__(self, environ, start_response):
     
    612634
    613635        # Find the admin file and serve it up, if it exists and is readable.
    614636        relative_url = environ['PATH_INFO'][len(self.media_url):]
    615         file_path = os.path.join(self.media_dir, relative_url)
     637        for media_dir in app_media_dirs:
     638            file_path = os.path.join(media_dir, relative_url)
     639            if os.path.exists(file_path):
     640                break
    616641        if not os.path.exists(file_path):
    617642            status = '404 NOT FOUND'
    618643            headers = {'Content-type': 'text/plain'}
Back to Top