Opened 5 years ago

Closed 3 years ago

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

Reported by: Owned by: 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")

### comment:1 Changed 5 years ago by Volodymyr.Koval

Version of SimpleMultiProjects? plugin - 0.0.3

### comment:2 follow-ups:  4  7 Changed 5 years ago 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.

### comment:3 Changed 5 years ago by falkb

This ticket is related to #10078

### comment:4 in reply to:  2 Changed 5 years ago by Ryan J Ollos

Description: modified (diff)

### comment:5 follow-up:  6 Changed 5 years ago by falkb

rjollos wrote on trac-dev@…:

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:

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

### comment:6 in reply to:  5 Changed 5 years ago by Jun Omae

rjollos wrote on trac-dev@…:

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.

### comment:7 in reply to:  2 Changed 5 years ago by Chris Nelson

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:

• ## trac/web/chrome.py

index 20e89d5..2cdc2e3 100644
 a def add_script(req, filename, mimetype='text/javascript'): req.chrome.setdefault('scripts', []).append(script) scriptset.add(filename) # Backported from 0.12 def add_script_data(req, data={}, **kwargs): """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) def add_javascript(req, filename): """Deprecated: use add_script() instead.""" add_script(req, filename, mimetype='text/javascript')

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.

### comment:8 follow-up:  9 Changed 5 years ago by Ryan J Ollos

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.

### comment:9 in reply to:  8 ; follow-up:  11 Changed 5 years ago by Chris Nelson

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

### comment:10 Changed 5 years ago by falkb

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

### comment:11 in reply to:  9 ; follow-up:  12 Changed 5 years ago by Chris Nelson

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?

### comment:12 in reply to:  11 Changed 5 years ago by Ryan J Ollos

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.

### comment:13 Changed 5 years ago by Chris Nelson

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.

### comment:14 follow-up:  18 Changed 5 years ago by Chris Nelson

However, the page source includes:

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

### comment:15 follow-up:  16 Changed 5 years ago by Ryan J Ollos

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.

### comment:16 in reply to:  15 ; follow-up:  17 Changed 5 years ago by Chris Nelson

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?

### comment:17 in reply to:  16 Changed 5 years ago by Chris Nelson

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); } ### comment:18 in reply to: 14 ; follow-up: 19 Changed 5 years ago by Chris Nelson Replying to ChrisNelson: However, the page source includes: <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. ### comment:19 in reply to: 18 Changed 5 years ago by Ryan J Ollos 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. ### comment:20 follow-up: 22 Changed 5 years ago by Chris Nelson 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.

### comment:22 in reply to:  20 ; follow-up:  23 Changed 5 years ago by Ryan J Ollos

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.

### comment:23 in reply to:  22 Changed 5 years ago by Ryan J Ollos

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.

### comment:24 Changed 4 years ago by falkb

Owner: changed from Christopher Paredes to falkb

### comment:25 Changed 4 years ago 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)

### comment:26 Changed 3 years ago by falkb

Resolution: → wontfix new → closed

giving up support of Trac older than 0.12

### Modify Ticket

Change Properties