Opened 19 years ago

Closed 18 years ago

#914 closed defect (fixed)

[patch] Admin js option does not honor absolute urls

Reported by: eugene@… Owned by: Adrian Holovaty
Component: contrib.admin Version: dev
Severity: normal Keywords: patch
Cc: gandalf@…, nesh@…, mhf@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Previously js option produced script blocks using verbatim path. Example:

class Document(meta.Model):
    # fields
    class META:
        admin = meta.Admin(
            js = (
                '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php',
            ),
        )

produced:

<script type="text/javascript" src="/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php"></script>

New admin produces different string:

<script type="text/javascript" src="/media//tinymce/jscripts/tiny_mce/tiny_mce_gzip.php"></script>

Essentially it is the same js path but it is now prefixed by /media/. It is easy to correct in my case: change symlink, and remove leading '/' in js.

Anyway we have a backward-incompatible difference. We have to decide how to handle it: old way or new way. The final decision should be documented.

New way is not bad. You can argue that it reduces number of entries in .htaccess or whatever non-Django mechanism is used for file serving. But it forces to change existing model files, which use extra JavaScript for Admin interface.

Attachments (4)

admin_modify.py.patch (912 bytes ) - added by gandalf@… 19 years ago.
add checking for / and url's for js in admin media
admin_modify-mr.py.patch (888 bytes ) - added by gandalf@… 19 years ago.
updated patch for magic-removal
admin_modify.diff (1.4 KB ) - added by nesh <nesh [at] studioquattro [dot] co [dot] yu> 18 years ago.
diff against [3411]
absolute_include_admin_script-r4185.diff (1.1 KB ) - added by mhf@… 18 years ago.
Patch (against r4185), which uses urljoin

Download all attachments as: .zip

Change History (16)

comment:1 by edgars@…, 19 years ago

+1

comment:2 by Joeboy, 19 years ago

I think the new behaviour is really quite irritating. If I want to use a single js file that varies from site to site (eg. a tinymce config file) I have to make a copy of the whole admin media folder for each site, whereas previously I could just stick it in /media. Am I missing something or is this just gratuitously annoying?

I'd have thought the sensible system would be to not prefix if the url begins with a slash.

comment:3 by gandalf@…, 19 years ago

Cc: gandalf@… added
Keywords: patch added
Version: SVN

Here is a patch that modifies this behaviour in two ways:

a) It checks for / at the beginning of the lines, and if it finds it, it doesn't include ADMIN_MEDIA_URL before it
b) It checks with regular expression if it is url (starting with http, ftp or https) and also doesn't include ADMIN_MEDIA_URL in this case.

otherwise, it inserts ADMIN_MEDIA_URL at the beginning

by gandalf@…, 19 years ago

Attachment: admin_modify.py.patch added

add checking for / and url's for js in admin media

by gandalf@…, 19 years ago

Attachment: admin_modify-mr.py.patch added

updated patch for magic-removal

comment:4 by gandalf@…, 19 years ago

Summary: Behavior of META js option[patch] Behavior of META js option
Version: SVNmagic-removal

comment:5 by gandalf@…, 18 years ago

Any comment on why this patch is not acceptable for inclusion into svn?

comment:6 by xale, 18 years ago

priority: normalhigh

It's been six months since this ticked was created and the patch still isn't in trunk as far as I can see. Is there any readon why it's not commited yet?

comment:7 by xale, 18 years ago

I've looked at the patch and it's pretty horible.

What we really need here is either remove this prefix at all and change all admin code to use full path or change templates and template tags to include user defined JS URLs without prefix.

comment:8 by nesh <nesh [at] studioquattro [dot] co [dot] yu>, 18 years ago

Cc: nesh@… added

What is the status of this patch? I really need this because I don't put admin media within my media folder and I hate to use patched django source.

I'm definitely against prepending ADMIN_MEDIA_PREFIX to custom JS.

IMHO this patch is OK.

Just a suggestion, remove regexp from the function like this (small optimization).

_URL_PATTERN = re.compile(r'''(?x)((http|https|ftp)://(\w+[:.]?){2,}(/?|[^ \n\r"']+[\w/!?.=#])(?=[\s\.,>)"'\]]))''')
def include_admin_script(script_path):
   if _URL_PATTERN.match(script_path) or script_path[0] == '/':
       return '<script type="text/javascript" src="%s"></script>' % (script_path)
   else:
       return '<script type="text/javascript" src="%s%s"></script>' % (settings.ADMIN_MEDIA_PREFIX, script_path)
include_admin_script = register.simple_tag(include_admin_script)

by nesh <nesh [at] studioquattro [dot] co [dot] yu>, 18 years ago

Attachment: admin_modify.diff added

diff against [3411]

comment:9 by mhf@…, 18 years ago

Cc: mhf@… added

This problem can be solved with urlparse.urljoin:

>>> from urlparse import urljoin
>>> urljoin('/media', 'somescript.js')
'/somescript.js'
>>> urljoin('/media', '/anotherpath/somescript.js')
'/anotherpath/somescript.js'
>>> urljoin('/media', 'http://example.org/somescript.js')
'http://example.org/somescript.js'

by mhf@…, 18 years ago

Patch (against r4185), which uses urljoin

comment:10 by mhf@…, 18 years ago

Summary: [patch] Behavior of META js option[patch] Admin js option does not honor absolute urls
Version: magic-removalSVN

Attached a patch against r4185, which uses urljoin instead of a regular expression.

Should there be a regression test for this?

comment:11 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedReady for checkin

Thanks mhf, I'll let the core decide if we need a regression test and mark as ready for the moment.

comment:12 by Adrian Holovaty, 18 years ago

Resolution: fixed
Status: newclosed

Marking this as fixed because, as of [4382] in the newforms-admin branch, you can implement the javascript() method on your class Admin to specify arbitrary JavaScript.

Note: See TracTickets for help on using tickets.
Back to Top