Django

Code

Ticket #914 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

[patch] Admin js option does not honor absolute urls

Reported by: eugene@lazutkin.com Assigned to: adrian
Component: Admin interface Version: SVN
Keywords: patch Cc: gandalf@owca.info, nesh@studioquattro.co.yu, mhf@hex.no
Triage Stage: Ready for checkin Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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

admin_modify.py.patch (0.9 kB) - added by gandalf@owca.info on 04/22/06 13:29:44.
add checking for / and url's for js in admin media
admin_modify-mr.py.patch (0.9 kB) - added by gandalf@owca.info on 04/29/06 04:16:13.
updated patch for magic-removal
admin_modify.diff (1.4 kB) - added by nesh <nesh [at] studioquattro [dot] co [dot] yu> on 07/26/06 06:49:50.
diff against [3411]
absolute_include_admin_script-r4185.diff (1.1 kB) - added by mhf@hex.no on 12/08/06 06:17:15.
Patch (against r4185), which uses urljoin

Change History

01/11/06 14:26:08 changed by edgars@way.lv

+1

02/21/06 06:42:41 changed by Joeboy

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.

04/22/06 13:28:17 changed by gandalf@owca.info

  • cc set to gandalf@owca.info.
  • keywords set to patch.
  • version set to 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

04/22/06 13:29:44 changed by gandalf@owca.info

  • attachment admin_modify.py.patch added.

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

04/29/06 04:16:13 changed by gandalf@owca.info

  • attachment admin_modify-mr.py.patch added.

updated patch for magic-removal

04/29/06 04:17:12 changed by gandalf@owca.info

  • version changed from SVN to magic-removal.
  • summary changed from Behavior of META js option to [patch] Behavior of META js option.

06/05/06 06:59:59 changed by gandalf@owca.info

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

06/14/06 04:30:42 changed by xale

  • priority changed from normal to high.

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?

06/14/06 04:46:35 changed by xale

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.

07/26/06 05:54:18 changed by nesh <nesh [at] studioquattro [dot] co [dot] yu>

  • cc changed from gandalf@owca.info to gandalf@owca.info, nesh@studioquattro.co.yu.

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)

07/26/06 06:49:50 changed by nesh <nesh [at] studioquattro [dot] co [dot] yu>

  • attachment admin_modify.diff added.

diff against [3411]

12/08/06 06:03:37 changed by mhf@hex.no

  • cc changed from gandalf@owca.info, nesh@studioquattro.co.yu to gandalf@owca.info, nesh@studioquattro.co.yu, mhf@hex.no.

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'

12/08/06 06:17:15 changed by mhf@hex.no

  • attachment absolute_include_admin_script-r4185.diff added.

Patch (against r4185), which uses urljoin

12/08/06 06:20:46 changed by mhf@hex.no

  • version changed from magic-removal to SVN.
  • summary changed from [patch] Behavior of META js option to [patch] Admin js option does not honor absolute urls.

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

Should there be a regression test for this?

01/17/07 23:24:55 changed by SmileyChris

  • stage changed from Unreviewed to Ready for checkin.

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

01/22/07 19:58:55 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

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?.


Add/Change #914 ([patch] Admin js option does not honor absolute urls)




Change Properties
Action