Ticket #4912 (closed defect: fixed)

Opened 4 years ago

Last modified 3 years ago

Doesn't work

Reported by: jason.trahan@twtelecom.com Assigned to: richard
Priority: normal Component: TracTweakUiPlugin
Severity: major Keywords:
Cc: Trac Release: 0.11

Description

So I followed the instructions as followed.

1. Created new URL Reference of /newticket

2. Added editcc script with the following script

$(document).ready(function() {
	// edit cc
	alert("Testing");
});

3. Clicked on new ticket and it would never fire off the alert.

When I dug into this more I noticed that the script reference was coming out as


<script src="/tractweakui/tweakui_js/%2Fnewticket.js" type="text/javascript"></script>


however when I attempt to go that script Trac says it does not exist.

I have tested this in both firefox and IE

Attachments

Change History

04/10/09 21:45:17 changed by jason.trahan@telecom.com

So I think I found the problem. When it gets stored in the database the url path is /newticket. However when it gets rendered to the browser it comes out as %2Fnewticket. because of this when it looks in the database it is not properly matching up the links.

04/10/09 22:03:06 changed by jason.trahan@twtelecom.com

OK Found the problem. In process_question you have the code as followed

        if req.path_info.startswith(tweakui_js_path):
            path_pattern = req.path_info[len(tweakui_js_path) + 1 : -3]
            js_files = TracTweakUIModel.get_path_scripts(self.env, path_pattern)



It should be setup like this.

        if req.path_info.startswith(tweakui_js_path):
            path_pattern = urllib.unquote(req.path_info[len(tweakui_js_path) + 1 : -3])
            js_files = TracTweakUIModel.get_path_scripts(self.env, path_pattern)



Once this is in place then it starts working right.

04/13/09 03:01:29 changed by richard

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

[5514]

  • fixed.

(follow-up: ↓ 5 ) 04/16/09 18:11:57 changed by jason.trahan@twtelecom.com

  • status changed from closed to reopened.
  • resolution deleted.

One more change needed to be made to make it work right. Also in web_ui.py look for the line.


src= req.base_path + "/tractweakui/tweakui_js/" + urllib.quote(path_pattern, "") + ".js")()


and change it to


src= req.base_path + "/tractweakui/tweakui_js/" + urllib.quote(path_pattern, "/") + ".js")()


(in reply to: ↑ 4 ) 04/17/09 10:51:20 changed by richard

Replying to jason.trahan@twtelecom.com:

One more change needed to be made to make it work right. Also in web_ui.py look for the line. {{{ src= req.base_path + "/tractweakui/tweakui_js/" + urllib.quote(path_pattern, "") + ".js")() }}} and change it to {{{ src= req.base_path + "/tractweakui/tweakui_js/" + urllib.quote(path_pattern, "/") + ".js")() }}}

The purpose of urllib.quote(path_pattern, safe = "") is that we normally treat as a normal js file, like: '%2Fab%2Fcd.js'. If we use this urllib.quote(path_pattern, safe = "/") instead, this js file will look like this: '/ab/cd.js'. Also, in my test, when using safe = "", the result is ok.

>>> urllib.unquote(urllib.quote("/ab/cd.js", ""))
'/ab/cd.js'
>>> urllib.unquote(urllib.quote("/ab/cd.js", "/"))
'/ab/cd.js'

(follow-up: ↓ 10 ) 01/08/10 00:12:04 changed by bobbysmith007

I can confirm that this plugin, in its current state, does not work for me.

When I set the url regex to newticket it does not try to include the javascript at all.

When I try to set the url to /newticket it inserts the correct script tags, but doesn't actually serve the javascript (404 errors).

Both of these seem like errors

01/08/10 00:33:30 changed by bobbysmith007

The solution to newticket not matching was to change re.match to re.search in web_ui.py

Index: tractweakui/web_ui.py
===================================================================
--- tractweakui/web_ui.py       (revision 7357)
+++ tractweakui/web_ui.py       (working copy)
@@ -203,7 +203,7 @@
         path_patterns = TracTweakUIModel.get_path_patterns(self.env)
         # try to match pattern
         for path_pattern in path_patterns:
-            if re.match(path_pattern, req.path_info):
+            if re.search(path_pattern, req.path_info):
                 break
         else:
             return stream

With this patch in place I can successfully use the plugin. This doesnt solve the above mentioned "/" problem, but its one step closer. The reason is because search searches the whole string while match on checks for a match at the beginning (implicit start anchor).

http://docs.python.org/library/re.html#re.search

HTH, Russ

01/08/10 01:30:25 changed by bobbysmith007

Whew!, I spent rather more time on this plugin this evening than expected :)

I tried to look into why "/newticket" was failing but trac doesnt even seem to be trying to handle requests to urls like .../tractweakui/tweakui_js/%2Fnewticket.js (I dont get log messages from match_request, which I do if the extra %2F is not in the url).

As such it seems like perhaps embedding the regex that matches the url as part of a url *might* not be the best idea. Is there any reason not to just output the javascript directly into the script tag rather than outputting a script tag and automatically handling that request?

The patch below does just that and seems to work for me. Alternatively I would suggest giving each pattern a unique ID (which seems to be happening in the database) and then look up the javascript to serve based on ID rather than pattern (which can contain characters which screw up urls.)

Index: tractweakui/web_ui.py
===================================================================
--- tractweakui/web_ui.py       (revision 7357)
+++ tractweakui/web_ui.py       (working copy)
@@ -22,6 +22,7 @@

 from pkg_resources import resource_filename
 from genshi.filters.transform import Transformer
+from trac.util import Markup

 import sys, os
 import re
@@ -211,16 +212,22 @@
         filter_names = TracTweakUIModel.get_path_filters(self.env, path_pattern)
         for filter_name in filter_names:
             stream = self._apply_filter(req, stream, path_pattern, filter_name)
+        js_files = TracTweakUIModel.get_path_scripts(self.env, path_pattern)
+        if js_files:
+            script = ";\n".join(js_files)
+        else:
+            script = ""
+
         stream = stream | Transformer('head').append(
-            tag.script(type="text/javascript",
-            src= req.base_path + "/tractweakui/tweakui_js/" + urllib.quote(path_pattern, "") + ".js")()
-            )
+            # Markup allows us to output javascript without it being weirdly escaped (which can mess up JQuery
+            tag.script(Markup(script), type="text/javascript")())

         return stream

     # IRequestHandler methods

     def match_request(self, req):
+        return False # Dont handle dynamic requests right now
         return req.path_info.startswith('/tractweakui/tweakui_js')

     def process_request(self, req):
@@ -245,8 +253,8 @@
         if not os.path.exists(filter_path):
             return stream

-        css_files = self._find_filter_files(filter_path, "css")
-        js_files = self._find_filter_files(filter_path, "js")
+        css_files = self._find_filter_files(filter_path, ".css")
+        js_files = self._find_filter_files(filter_path, ".js")

         for css_file in css_files:
             stream = self._add_css(req, stream, filter_name, css_file)

Thanks for the work you put in on this plugin, I hope this patch helps get it to a more stable usable point.

Cheers, Russ

01/18/10 10:27:15 changed by richard

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

(In [7413]) fixed #4912, output the javascript directly into the script tag rather than in a script tag.

(in reply to: ↑ 6 ) 01/18/10 10:34:11 changed by richard

Replying to bobbysmith007:

I can confirm that this plugin, in its current state, does not work for me. When I set the url regex to newticket it does not try to include the javascript at all. When I try to set the url to /newticket it inserts the correct script tags, but doesn't actually serve the javascript (404 errors). Both of these seem like errors

This seems occur publishing by apache. tracd or lighttd works as expected. I am not sure if apache do some automatic URL convertion.


Add/Change #4912 (Doesn't work)




Change Properties
Action