~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to magento_connect/wizard/wizard_product_product.py

  • Committer: Raimon Esteve
  • Date: 2012-04-11 15:38:03 UTC
  • Revision ID: resteve@zikzakmedia.com-20120411153803-3u1ls7h4h4gpaszu
[ADD] Mangento Connect. Mangento Products and orders sync in your OpenERP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
############################################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2012 Zikzakmedia S.L. (<http://www.zikzakmedia.com>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
############################################################################################
 
22
 
 
23
from osv import fields,osv
 
24
from tools.translate import _
 
25
 
 
26
import pooler
 
27
import threading
 
28
 
 
29
class magento_sync_product_wizard(osv.osv_memory):
 
30
    _name = 'magento.sync.product.wizard'
 
31
 
 
32
    def _magento_sale_shop(self, cr, uid, context=None):
 
33
        ids = self.pool.get('sale.shop').search(cr, uid, [('magento_shop', '=', True)], order='id')
 
34
        shops = self.pool.get('sale.shop').read(cr, uid, ids, ['id','name'], context=context)
 
35
        return [(a['id'], a['name']) for a in shops]
 
36
 
 
37
    _columns = {
 
38
        'magento_sale_shop': fields.selection(_magento_sale_shop, 'Sale Shop', required=True),
 
39
        'result': fields.text('Result', readonly=True),
 
40
        'state':fields.selection([
 
41
            ('first','First'),
 
42
            ('done','Done'),
 
43
        ],'State'),
 
44
    }
 
45
 
 
46
    _defaults = {
 
47
        'state': lambda *a: 'first',
 
48
    }
 
49
 
 
50
    def sync_product(self, cr, uid, ids, data, context={}):
 
51
        """Export sync products"""
 
52
 
 
53
        if len(data['active_ids']) == 0:
 
54
            raise osv.except_osv(_('Error!'), _('Select products to export'))
 
55
 
 
56
        form = self.browse(cr, uid, ids[0])
 
57
        shop = form.magento_sale_shop
 
58
        shop = self.pool.get('sale.shop').browse(cr, uid, shop)
 
59
        magento_app = shop.magento_website.magento_app_id
 
60
 
 
61
        product_ids = []
 
62
        for prod in self.pool.get('product.product').browse(cr, uid, data['active_ids']):
 
63
            if prod.magento_exportable:
 
64
                product_ids.append(prod.id)
 
65
 
 
66
        values = {
 
67
            'state':'done',
 
68
        }
 
69
        if len(product_ids) > 0:
 
70
            values['result'] = '%s' % (', '.join(str(x) for x in product_ids))
 
71
        else:
 
72
            values['result'] = _('Not available some Magento Products to export')
 
73
 
 
74
        self.write(cr, uid, ids, values)
 
75
 
 
76
        cr.commit()
 
77
 
 
78
        if shop.magento_default_language:
 
79
            context['lang'] = shop.magento_default_language.code
 
80
 
 
81
        if len(product_ids) > 0:
 
82
            thread1 = threading.Thread(target=self.pool.get('sale.shop').magento_export_products_stepbystep, args=(cr.dbname, uid, magento_app.id, [prod.id], context))
 
83
            thread1.start()
 
84
        return True
 
85
 
 
86
magento_sync_product_wizard()