# Ticket #10240 (new defect)

Opened 9 months ago

## Can't import "Import Error : cannot import name add_script_data"

Reported by: Assigned to: anonymous falkb normal SimpleMultiProjectPlugin blocker 0.11

The reason I'm reporting this issue is because I did not find any information regarding the version needed on the hacks page.

I have 0.11.7 installed.

After installation of SimpleMultiProjects plugin I see it only on the admin page.

This plugin is not visible on the roadmap page.

On trac logs I found this error:

Trac[loader] ERROR: Skipping "simplemultiproject = simplemultiproject":
(can't import "ImportError: cannot import name add_script_data")

## Change History

### 08/08/12 12:51:31 changed by vovanchik

Version of SimpleMultiProjects?? plugin - 0.0.3

### (follow-ups: ↓ 4 ↓ 7 ) 09/17/12 09:16:15 changed by falkb

Hmm... it seems add_script_data is a new function of 0.12 :-( Do you see a chance to backport it to 0.11? Unfortunately, I don't have 0.11 anymore.

### 09/17/12 09:17:31 changed by falkb

This ticket is related to #10078

### (in reply to: ↑ 2 ) 09/17/12 11:38:06 changed by rjollos

• description changed.

### (follow-up: ↓ 6 ) 09/21/12 09:28:41 changed by falkb

I'm not sure about backporting, but I can show you some examples of what developers did before the function was available. These are the changesets/patches in which I replaced the virtual script with add_script data: http://trac-hacks.org/changeset/11247 and http://trac-hacks.org/ticket/9764

Thanks a lot, stored here now. Need some time...

### (in reply to: ↑ 5 ) 09/21/12 14:19:29 changed by jun66j5

I'm not sure about backporting, but I can show you some examples of what developers did before the function was available. These are the changesets/patches in which I replaced the virtual script with add_script data:

Another solution is to add script element to head element on ITemplateStreamFilter.filter_stream.

### (in reply to: ↑ 2 ) 10/09/12 20:37:19 changed by ChrisNelson

Hmm... it seems add_script_data is a new function of 0.12 :-( Do you see a chance to backport it to 0.11? Unfortunately, I don't have 0.11 anymore.

This appears a trivial backport. Diffing to 0.11.6:

index 20e89d5..2cdc2e3 100644
--- a/trac/web/chrome.py
+++ b/trac/web/chrome.py
@@ -108,6 +108,18 @@ def add_script(req, filename, mimetype='text/javascript'):
req.chrome.setdefault('scripts', []).append(script)

+# Backported from 0.12
+    """Add data to be made available in javascript scripts as global variables.
+
+    The keys in data and the keyword argument names provide the names of the
+    global variables. The values are converted to JSON and assigned to the
+    corresponding variables.
+    """
+    script_data = req.chrome.setdefault('script_data', {})
+    script_data.update(data)
+    script_data.update(kwargs)
+
"""Deprecated: use add_script() instead."""


With that in place, I can load GroupTicketFieldsPlugin and it creates sections in my ticket for other fields but the configured fields don't show up in the new section. Still looking into that.

### (follow-up: ↓ 9 ) 10/10/12 00:40:04 changed by rjollos

It looks like you are on the right track, but you need the corresponding functionality to parse script_data in your template (see layout.html) and a to_json function.

### (in reply to: ↑ 8 ; follow-up: ↓ 11 ) 10/10/12 14:32:46 changed by ChrisNelson

It looks like you are on the right track, but you need the corresponding functionality to parse script_data in your template (see layout.html) and a to_json function.

The former is easy. The latter seems a slippery slope.

• to_json is in presentation.py
• It relies on dumps from json or to_js_string from util.text.
• In 0.11.6, there is no to_js_string in util.text
• I'm in Python 2.6.5 and that has dumps so that may work but I don't know how far back

### 10/10/12 14:47:47 changed by falkb

IIRC json.dumps is available since Python 2.6 (also read here). But look here for how to code a compat switch.

### (in reply to: ↑ 9 ; follow-up: ↓ 12 ) 10/10/12 14:48:53 changed by ChrisNelson

The former is easy. The latter seems a slippery slope. * to_json is in presentation.py * It relies on dumps from json or to_js_string from util.text. * In 0.11.6, there is no to_js_string in util.text * I'm in Python 2.6.5 and that has dumps so that may work but I don't know how far back

Now I'm stuck. I have to_json in presentation.py but how do I *import* it into layout.html?

### (in reply to: ↑ 11 ) 10/10/12 19:51:17 changed by rjollos

Now I'm stuck. I have to_json in presentation.py but how do I *import* it into layout.html?

I think that you need to add it to the data dictionary: data['to_json'] = to_json. Take a look at populate_data to see how this works.

### 10/10/12 22:22:39 changed by ChrisNelson

So that gets me further. The page source includes:

<script type="text/javascript">
var field_groups_order=["relationships","main"];
var field_groups=[{"fields":["parents","blockedby","blocking"],"label":"Relationships","name":"relationships"}];
</script>


which is consistent with my configuration:

[group-ticket-fields]
relationships = Relationships
relationships.fields = parents,blockedby,blocking
group_order = relationships,main


but in the ticket, the Relationships section is still empty.

I don't find anything in trac.log about any failure and I don't get a JavaScript? error.

FireBug doesn't list group_ticket_fields.js among the loaded script resources.

### (follow-up: ↓ 18 ) 10/10/12 22:25:44 changed by ChrisNelson

However, the page source includes:

<script type="text/javascript" src="/mytrac/chrome/groupticketfields/group_ticket_fields.js"></script>


### (follow-up: ↓ 16 ) 10/10/12 22:40:07 changed by rjollos

One potential problem is that the plugin is calling the function enableFolding, which is provided in Trac 0.12 by folding.js. This function doesn't exist in Trac 0.11 (see #8942). However, that doesn't seem to be your primary problem here.

### (in reply to: ↑ 15 ; follow-up: ↓ 17 ) 10/10/12 22:57:57 changed by ChrisNelson

One potential problem is that the plugin is calling the function enableFolding, which is provided in Trac 0.12 by folding.js. This function doesn't exist in Trac 0.11 (see #8942). However, that doesn't seem to be your primary problem here.

Yeah, I can live without folding. Hmmm, I wonder if that needs to be protected by a try?

### (in reply to: ↑ 16 ) 10/10/12 23:04:42 changed by ChrisNelson

One potential problem is that the plugin is calling the function enableFolding, which is provided in Trac 0.12 by folding.js. This function doesn't exist in Trac 0.11 (see #8942). However, that doesn't seem to be your primary problem here.

Yeah, I can live without folding. Hmmm, I wonder if that needs to be protected by a try?

No, that call seems safe:

if ($.inArray('foldable', group['properties']) != -1) {$('legend', fieldset).enableFolding($.inArray('collapsed', group['properties']) != -1, true); }  ### (in reply to: ↑ 14 ; follow-up: ↓ 19 ) 10/11/12 17:32:53 changed by ChrisNelson Replying to ChrisNelson: However, the page source includes: {{{ #!python <script type="text/javascript" src="/mytrac/chrome/groupticketfields/group_ticket_fields.js"></script> }}} I had not (re)deployed static resources. When I do that the script loads and executes but doesn't quite do what I expect. It moves one label but not the corresponding field and ignores the rest of the fields. Maybe it encountered an error. ### (in reply to: ↑ 18 ) 10/11/12 17:46:25 changed by rjollos Replying to ChrisNelson: I had not (re)deployed static resources. When I do that the script loads and executes but doesn't quite do what I expect. It moves one label but not the corresponding field and ignores the rest of the fields. Maybe it encountered an error. I'm reaching here, but I seem to remember several of the ticket form IDs being renamed between 0.11 and 0.12, and having to fix some problems in another plugin with the stream filter not operating as expected in 0.12 as a result. ### (follow-up: ↓ 22 ) 10/11/12 18:10:19 changed by ChrisNelson The Group Ticket Fields JavaScript seems to go off into the weeds at line 58: var value_td =$('td').has('#' + field_id); // get the <td>


When I set a breakpoint there and at 59 and continue from 58, I never get to 59.

I'm left with a partially modified ticket:

<legend>Relationships</legend><table id="table_relationships"><tbody><tr><th style="display: table-cell;" class="col1">
<label for="field-parents">Parent Ticket:</label>
</th></tr></tbody></table>


Given that piece of the page, I don't see what "td" it is supposed to get, there isn't one. Is it searching for the <td> where the field was originally populated? That I can find in the page source just fine.

This is getting into jQuery territory I'm not familiar with. Maybe I need to give up.

### (in reply to: ↑ 20 ; follow-up: ↓ 23 ) 10/11/12 19:28:51 changed by rjollos

This is getting into jQuery territory I'm not familiar with. Maybe I need to give up.

We've come too far to give up ;) I'm setting up to test in 0.11 and 0.12.

### (in reply to: ↑ 22 ) 10/11/12 20:16:22 changed by rjollos

We've come too far to give up ;) I'm setting up to test in 0.11 and 0.12.

Well, nevermind. I had meant to comment on #10468, and was going to take a crack at getting that working, but since the issue is solved now, I won't worry about it.

### 04/23/13 08:09:49 changed by falkb

• owner changed from crossroad to falkb.

### 04/23/13 08:55:32 changed by falkb

(In [12999]) blindly tried backport to Trac 0.11, I don't have it installed, please report back (refs #10515, refs #10240, refs #10078)

### Add/Change #10240 (Can't import "Import Error : cannot import name add_script_data")

Change Properties