~camptocamp/openerp-connector-magento/7.0-copy-quotation-move-binding

« back to all changes in this revision

Viewing changes to magentoerpconnect/related_action.py

  • Committer: Guewen Baconnier
  • Date: 2014-05-26 09:37:00 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20140526093700-o0i186du7k39724i
Implements Related Actions

A related action can be attached to a job. It is shown to the user as a button on the form view of the jobs.
When the button is used, the related action is called and must return an OpenERP "client action".

Related action in this connector:
* Open the form view of the record that is concerned by the job on export jobs
* Open a browser on Magento's admin page in order to view/edit the record on import jobs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Guewen Baconnier
 
5
#    Copyright 2014 Camptocamp SA
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
"""
 
23
Related Actions for Magento:
 
24
 
 
25
Related actions are associated with jobs.
 
26
When called on a job, they will return an action to the client.
 
27
 
 
28
"""
 
29
 
 
30
import  functools
 
31
from openerp.osv import orm
 
32
from openerp.tools.translate import _
 
33
from openerp.addons.connector import related_action
 
34
from .connector import get_environment
 
35
from .unit.backend_adapter import GenericAdapter
 
36
from .unit.binder import MagentoBinder
 
37
 
 
38
unwrap_binding = functools.partial(related_action.unwrap_binding,
 
39
                                   binder_class=MagentoBinder)
 
40
 
 
41
 
 
42
def link(session, job, backend_id_pos=2, magento_id_pos=3):
 
43
    """ Open a Magento URL on the admin page to view/edit the record
 
44
    related to the job.
 
45
    """
 
46
    binding_model = job.args[0]
 
47
    # shift one to the left because session is not in job.args
 
48
    backend_id = job.args[backend_id_pos - 1]
 
49
    magento_id = job.args[magento_id_pos - 1]
 
50
    env = get_environment(session, binding_model, backend_id)
 
51
    adapter = env.get_connector_unit(GenericAdapter)
 
52
    try:
 
53
        url = adapter.admin_url(magento_id)
 
54
    except ValueError:
 
55
        raise orm.except_orm(
 
56
            _('Error'),
 
57
            _('No admin URL configured on the backend or '
 
58
              'no admin path is defined for this record.'))
 
59
 
 
60
    action = {
 
61
        'type': 'ir.actions.act_url',
 
62
        'target': 'new',
 
63
        'url': url,
 
64
    }
 
65
    return action