[FIX] share+portal: fix share wizard.
[odoo/odoo.git] / addons / share / web / editors.py
1 # -*- coding: utf-8 -*-
2 import openobject.templating
3
4 class FormEditor(openobject.templating.TemplateEditor):
5     templates = ['/openerp/controllers/templates/form.mako']
6     MAIN_FORM_BODY = u'id="main_form_body"'
7
8     def insert_share_button(self, output):
9         # Insert the share button on the form title H1 line, at the very end,
10         # but only if the user is a member of the sharing group 
11         share_opener_insertion = output.index('% endif', output.index(
12                 '/view_diagram/process',
13                 output.index(self.MAIN_FORM_BODY))) + len('% endif')
14         return output[:share_opener_insertion] + '''
15     <%
16         if 'has_share' not in cp.session:
17             cp.session['has_share'] = rpc.RPCProxy('share.wizard').has_share()
18     %>
19     % if cp.session['has_share'] and buttons.toolbar and not is_dashboard:
20         <a id="share-opener" href="#share" title="${_('Share this in 2 clicks...')}">
21             <img id="share-opener-img" src="/share/static/images/share.png"/>
22         </a>
23                <script type="text/javascript">
24                    jQuery(document).ready(function() {
25                        jQuery("#share-opener").click(function() {
26                            jQuery(this).attr(
27                                "href",
28                                openobject.http.getURL('/share', {
29                                    context: jQuery("#_terp_context").val(),
30                                    domain: jQuery("#_terp_domain").val(),
31                                    view_id: jQuery("#_terp_view_id").val(),
32                                    action_id: jQuery("#_terp_action_id").val(),
33                                    search_domain: jQuery("#_terp_view_type").val() == "form" ? 
34                                                          ("[('id','=',"+jQuery("#_terp_id").val()+")]") : 
35                                                             jQuery("#_terp_search_domain").val(),
36                            }));
37                        });
38                    });
39                </script>
40                \n
41     % endif
42 ''' + output[share_opener_insertion:]
43
44     def edit(self, template, template_text):
45         return self.insert_share_button(
46             super(FormEditor, self).edit(template, template_text))
47
48
49 class SidebarEditor(openobject.templating.TemplateEditor):
50     templates = ['/openerp/widgets/templates/sidebar.mako']
51     ADD_SHARE_SECTION = u'id="sidebar"'
52
53     def insert_share_link(self, output):
54         # Insert the link on the line, right after the link to open the
55         # attachment form, but only if the user is a member of the sharing group 
56         share_opener_insertion = output.index(
57                 '\n',
58                 output.index(self.ADD_SHARE_SECTION)) + 1
59         return output[:share_opener_insertion] + '''
60     <%
61         if 'has_share' not in cp.session:
62             cp.session['has_share'] = rpc.RPCProxy('share.wizard').has_share()
63     %>
64     % if cp.session['has_share']:
65         <div id="share-wizard" class="sideheader-a"><h2>${_("Sharing")}</h2></div>
66              <ul class="clean-a">
67                  <li>
68                      <a id="sharing" href="#share">${_("Share")}</a>
69                  </li>
70              </ul>
71                <script type="text/javascript">
72                    jQuery(document).ready(function() {
73                        jQuery("#sharing").click(function() {
74                            jQuery(this).attr(
75                                "href",
76                                openobject.http.getURL('/share', {
77                                    context: jQuery("#_terp_context").val(),
78                                    domain: jQuery("#_terp_domain").val(),
79                                    view_id: jQuery("#_terp_view_id").val(),
80                                    action_id: jQuery("#_terp_action_id").val(),
81                                    search_domain: jQuery("#_terp_view_type").val() == "form" ? 
82                                                          ("[('id','=',"+jQuery("#_terp_id").val()+")]") : 
83                                                             jQuery("#_terp_search_domain").val(),
84                            }));
85                        });
86                    });
87                </script>
88                \n
89     % endif
90 ''' + output[share_opener_insertion:]
91
92     def edit(self, template, template_text):
93         return self.insert_share_link(
94             super(SidebarEditor, self).edit(template, template_text))