Skip to content

Commit

Permalink
Fixed django#18035 -- Removed deprecated AdminMediaHandler, as per of…
Browse files Browse the repository at this point in the history
…ficial deprecation timeline. Thanks Jannis Leidel and Ramiro Morales for the review.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17879 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
claudep committed Apr 8, 2012
1 parent 4f62352 commit 5c53e30
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 164 deletions.
8 changes: 4 additions & 4 deletions django/contrib/admin/static/admin/js/SelectFilter2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function findForm(node) {
}

window.SelectFilter = {
init: function(field_id, field_name, is_stacked, admin_media_prefix) {
init: function(field_id, field_name, is_stacked, admin_static_prefix) {
if (field_id.match(/__prefix__/)){
// Don't intialize on empty forms.
return;
Expand Down Expand Up @@ -43,14 +43,14 @@ window.SelectFilter = {
var selector_available = quickElement('div', selector_div, '');
selector_available.className = 'selector-available';
var title_available = quickElement('h2', selector_available, interpolate(gettext('Available %s') + ' ', [field_name]));
quickElement('img', title_available, '', 'src', admin_media_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of available %s. You may choose some by selecting them in the box below and then clicking the "Choose" arrow between the two boxes.'), [field_name]));
quickElement('img', title_available, '', 'src', admin_static_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of available %s. You may choose some by selecting them in the box below and then clicking the "Choose" arrow between the two boxes.'), [field_name]));

var filter_p = quickElement('p', selector_available, '', 'id', field_id + '_filter');
filter_p.className = 'selector-filter';

var search_filter_label = quickElement('label', filter_p, '', 'for', field_id + "_input");

var search_selector_img = quickElement('img', search_filter_label, '', 'src', admin_media_prefix + 'img/selector-search.gif', 'class', 'help-tooltip', 'alt', '', 'title', interpolate(gettext("Type into this box to filter down the list of available %s."), [field_name]));
var search_selector_img = quickElement('img', search_filter_label, '', 'src', admin_static_prefix + 'img/selector-search.gif', 'class', 'help-tooltip', 'alt', '', 'title', interpolate(gettext("Type into this box to filter down the list of available %s."), [field_name]));

filter_p.appendChild(document.createTextNode(' '));

Expand All @@ -73,7 +73,7 @@ window.SelectFilter = {
var selector_chosen = quickElement('div', selector_div, '');
selector_chosen.className = 'selector-chosen';
var title_chosen = quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s') + ' ', [field_name]));
quickElement('img', title_chosen, '', 'src', admin_media_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of chosen %s. You may remove some by selecting them in the box below and then clicking the "Remove" arrow between the two boxes.'), [field_name]));
quickElement('img', title_chosen, '', 'src', admin_static_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of chosen %s. You may remove some by selecting them in the box below and then clicking the "Remove" arrow between the two boxes.'), [field_name]));

var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name'));
to_box.className = 'filtered';
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/gis/admin/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

# Creating a template context that contains Django settings
# values needed by admin map templates.
geo_context = Context({'ADMIN_MEDIA_PREFIX' : static('admin/'),
'LANGUAGE_BIDI' : translation.get_language_bidi()})
geo_context = Context({'LANGUAGE_BIDI' : translation.get_language_bidi()})

class OpenLayersWidget(Textarea):
"""
Expand Down
6 changes: 3 additions & 3 deletions django/contrib/staticfiles/management/commands/runserver.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from optparse import make_option

from django.conf import settings
from django.core.management.commands.runserver import BaseRunserverCommand
from django.core.management.commands.runserver import Command as RunserverCommand

from django.contrib.staticfiles.handlers import StaticFilesHandler

class Command(BaseRunserverCommand):
option_list = BaseRunserverCommand.option_list + (
class Command(RunserverCommand):
option_list = RunserverCommand.option_list + (
make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
make_option('--insecure', action="store_true", dest='insecure_serving', default=False,
Expand Down
17 changes: 4 additions & 13 deletions django/core/management/commands/runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import socket

from django.core.management.base import BaseCommand, CommandError
from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
from django.core.servers.basehttp import run, WSGIServerException, get_internal_wsgi_application
from django.utils import autoreload

naiveip_re = re.compile(r"""^(?:
Expand All @@ -17,7 +17,7 @@
DEFAULT_PORT = "8000"


class BaseRunserverCommand(BaseCommand):
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--ipv6', '-6', action='store_true', dest='use_ipv6', default=False,
help='Tells Django to use a IPv6 address.'),
Expand Down Expand Up @@ -128,15 +128,6 @@ def inner_run(self, *args, **options):
self.stdout.write("%s\n" % shutdown_message)
sys.exit(0)

class Command(BaseRunserverCommand):
option_list = BaseRunserverCommand.option_list + (
make_option('--adminmedia', dest='admin_media_path', default='',
help='Specifies the directory from which to serve admin media.'),
)

def get_handler(self, *args, **options):
"""
Serves admin media like old-school (deprecation pending).
"""
handler = super(Command, self).get_handler(*args, **options)
return AdminMediaHandler(handler, options.get('admin_media_path'))
# Kept for backward compatibility
BaseRunserverCommand = Command
50 changes: 2 additions & 48 deletions django/core/servers/basehttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
from django.core.management.color import color_style
from django.core.wsgi import get_wsgi_application
from django.utils.importlib import import_module
from django.utils._os import safe_join
from django.views import static

from django.contrib.staticfiles import handlers

__all__ = ['WSGIServer', 'WSGIRequestHandler']

Expand Down Expand Up @@ -131,7 +127,7 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):

def __init__(self, *args, **kwargs):
from django.conf import settings
self.admin_media_prefix = urlparse.urljoin(settings.STATIC_URL, 'admin/')
self.admin_static_prefix = urlparse.urljoin(settings.STATIC_URL, 'admin/')
# We set self.path to avoid crashes in log_message() on unsupported
# requests (like "OPTIONS").
self.path = ''
Expand Down Expand Up @@ -173,7 +169,7 @@ def get_environ(self):

def log_message(self, format, *args):
# Don't bother logging requests for admin images or the favicon.
if (self.path.startswith(self.admin_media_prefix)
if (self.path.startswith(self.admin_static_prefix)
or self.path == '/favicon.ico'):
return

Expand All @@ -200,48 +196,6 @@ def log_message(self, format, *args):
sys.stderr.write(msg)


class AdminMediaHandler(handlers.StaticFilesHandler):
"""
WSGI middleware that intercepts calls to the admin media directory, as
defined by the STATIC_URL setting, and serves those images.
Use this ONLY LOCALLY, for development! This hasn't been tested for
security and is not super efficient.
This is pending for deprecation since 1.3.
"""
def get_base_dir(self):
return os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin')

def get_base_url(self):
from django.conf import settings
return urlparse.urljoin(settings.STATIC_URL, 'admin/')

def file_path(self, url):
"""
Returns the path to the media file on disk for the given URL.
The passed URL is assumed to begin with ``self.base_url``. If the
resulting file path is outside the media directory, then a ValueError
is raised.
"""
relative_url = url[len(self.base_url[2]):]
relative_path = urllib.url2pathname(relative_url)
return safe_join(self.base_dir, relative_path)

def serve(self, request):
document_root, path = os.path.split(self.file_path(request.path))
return static.serve(request, path, document_root=document_root)

def _should_handle(self, path):
"""
Checks if the path should be handled. Ignores the path if:
* the host is provided as part of the base_url
* the request's path isn't under the base path
"""
return path.startswith(self.base_url[2]) and not self.base_url[1]


def run(addr, port, wsgi_handler, ipv6=False, threading=False):
server_address = (addr, port)
if threading:
Expand Down
2 changes: 1 addition & 1 deletion docs/howto/deployment/wsgi/gunicorn.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ This provides a few Django-specific niceties:
* validates installed models

* allows an ``--adminmedia`` option for passing in the location of the
admin media files, mimicing the behavior of runserver.
admin media files.

See Gunicorn's `deployment documentation`_ for additional tips on starting and
maintaining the Gunicorn server.
Expand Down
5 changes: 1 addition & 4 deletions docs/man/django-admin.1
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Runs this project as a FastCGI application. Requires flup. Use
.B runfcgi help
for help on the KEY=val pairs.
.TP
.BI "runserver [" "\-\-noreload" "] [" "\-\-nothreading" "] [" "\-\-nostatic" "] [" "\-\-insecure" "] [" "\-\-ipv6" "] [" "\-\-adminmedia=ADMIN_MEDIA_PATH" "] [" "port|ipaddr:port" "]"
.BI "runserver [" "\-\-noreload" "] [" "\-\-nothreading" "] [" "\-\-nostatic" "] [" "\-\-insecure" "] [" "\-\-ipv6" "] [" "port|ipaddr:port" "]"
Starts a lightweight Web server for development.
.TP
.BI "shell [" "\-\-plain" "]"
Expand Down Expand Up @@ -169,9 +169,6 @@ Enables IPv6 addresses.
.I \-\-verbosity=VERBOSITY
Verbosity level: 0=minimal output, 1=normal output, 2=all output.
.TP
.I \-\-adminmedia=ADMIN_MEDIA_PATH
Specifies the directory from which to serve admin media when using the development server.
.TP
.I \-\-traceback
By default, django-admin.py will show a simple error message whenever an
error occurs. If you specify this option, django-admin.py will
Expand Down
14 changes: 1 addition & 13 deletions docs/ref/django-admin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -655,23 +655,11 @@ You can provide an IPv6 address surrounded by brackets

A hostname containing ASCII-only characters can also be used.

.. django-admin-option:: --adminmedia

Use the ``--adminmedia`` option to tell Django where to find the various CSS
and JavaScript files for the Django admin interface. Normally, the development
server serves these files out of the Django source tree magically, but you'd
want to use this if you made any changes to those files for your own site.

Example usage::

django-admin.py runserver --adminmedia=/tmp/new-admin-style/

.. versionchanged:: 1.3

If the :doc:`staticfiles</ref/contrib/staticfiles>` contrib app is enabled
(default in new projects) the :djadmin:`runserver` command will be overriden
with an own :djadmin:`runserver<staticfiles-runserver>` command which doesn't
have the :djadminopt:`--adminmedia` option due to deprecation.
with an own :djadmin:`runserver<staticfiles-runserver>` command.

.. django-admin-option:: --noreload

Expand Down
4 changes: 2 additions & 2 deletions tests/regressiontests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,12 +1095,12 @@ def test_liveserver(self):

class ManageRunserver(AdminScriptTestCase):
def setUp(self):
from django.core.management.commands.runserver import BaseRunserverCommand
from django.core.management.commands.runserver import Command

def monkey_run(*args, **options):
return

self.cmd = BaseRunserverCommand()
self.cmd = Command()
self.cmd.run = monkey_run

def assertServerSettings(self, addr, port, ipv6=None, raw_ipv6=False):
Expand Down
20 changes: 10 additions & 10 deletions tests/regressiontests/admin_widgets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from .widgetadmin import site as widget_admin_site


admin_media_prefix = lambda: {
'ADMIN_MEDIA_PREFIX': "%sadmin/" % settings.STATIC_URL,
admin_static_prefix = lambda: {
'ADMIN_STATIC_PREFIX': "%sadmin/" % settings.STATIC_URL,
}

class AdminFormfieldForDBFieldTests(TestCase):
Expand Down Expand Up @@ -196,14 +196,14 @@ def test_render(self):
w = widgets.FilteredSelectMultiple('test', False)
self.assertHTMLEqual(
conditional_escape(w.render('test', 'test')),
'<select multiple="multiple" name="test" class="selectfilter">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % admin_media_prefix()
'<select multiple="multiple" name="test" class="selectfilter">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_STATIC_PREFIX)s"); });</script>\n' % admin_static_prefix()
)

def test_stacked_render(self):
w = widgets.FilteredSelectMultiple('test', True)
self.assertHTMLEqual(
conditional_escape(w.render('test', 'test')),
'<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % admin_media_prefix()
'<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_STATIC_PREFIX)s"); });</script>\n' % admin_static_prefix()
)

class AdminDateWidgetTest(DjangoTestCase):
Expand Down Expand Up @@ -292,7 +292,7 @@ def test_render(self):
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
self.assertHTMLEqual(
conditional_escape(w.render('test', band.pk, attrs={})),
'<input type="text" name="test" value="%(bandpk)s" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>' % dict(admin_media_prefix(), bandpk=band.pk)
'<input type="text" name="test" value="%(bandpk)s" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_STATIC_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>' % dict(admin_static_prefix(), bandpk=band.pk)
)

def test_relations_to_non_primary_key(self):
Expand All @@ -307,7 +307,7 @@ def test_relations_to_non_primary_key(self):
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
self.assertHTMLEqual(
w.render('test', core.parent_id, attrs={}),
'<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>' % admin_media_prefix()
'<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_STATIC_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>' % admin_static_prefix()
)

def test_fk_related_model_not_in_admin(self):
Expand Down Expand Up @@ -349,7 +349,7 @@ def test_proper_manager_for_label_lookup(self):
)
self.assertHTMLEqual(
w.render('test', child_of_hidden.parent_id, attrs={}),
'<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>' % admin_media_prefix()
'<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="/widget_admin/admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_STATIC_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>' % admin_static_prefix()
)


Expand All @@ -365,12 +365,12 @@ def test_render(self):
w = widgets.ManyToManyRawIdWidget(rel, widget_admin_site)
self.assertHTMLEqual(
conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={})),
'<input type="text" name="test" value="%(m1pk)s,%(m2pk)s" class="vManyToManyRawIdAdminField" /><a href="/widget_admin/admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="/static/admin/img/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % dict(admin_media_prefix(), m1pk=m1.pk, m2pk=m2.pk)
'<input type="text" name="test" value="%(m1pk)s,%(m2pk)s" class="vManyToManyRawIdAdminField" /><a href="/widget_admin/admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="/static/admin/img/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % dict(admin_static_prefix(), m1pk=m1.pk, m2pk=m2.pk)
)

self.assertHTMLEqual(
conditional_escape(w.render('test', [m1.pk])),
'<input type="text" name="test" value="%(m1pk)s" class="vManyToManyRawIdAdminField" /><a href="/widget_admin/admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % dict(admin_media_prefix(), m1pk=m1.pk)
'<input type="text" name="test" value="%(m1pk)s" class="vManyToManyRawIdAdminField" /><a href="/widget_admin/admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_STATIC_PREFIX)simg/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % dict(admin_static_prefix(), m1pk=m1.pk)
)

self.assertEqual(w._has_changed(None, None), False)
Expand Down Expand Up @@ -691,4 +691,4 @@ class HorizontalVerticalFilterSeleniumChromeTests(HorizontalVerticalFilterSeleni
webdriver_class = 'selenium.webdriver.chrome.webdriver.WebDriver'

class HorizontalVerticalFilterSeleniumIETests(HorizontalVerticalFilterSeleniumFirefoxTests):
webdriver_class = 'selenium.webdriver.ie.webdriver.WebDriver'
webdriver_class = 'selenium.webdriver.ie.webdriver.WebDriver'

0 comments on commit 5c53e30

Please sign in to comment.