~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to esale_joomla/wizard/wizard_product_export.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
import ir
 
29
import time
 
30
import os
 
31
import netsvc
 
32
import xmlrpclib
 
33
import netsvc
 
34
import pooler
 
35
 
 
36
import wizard
 
37
from osv import osv
 
38
 
 
39
_export_done_form = '''<?xml version="1.0"?>
 
40
<form string="Initial import">
 
41
        <separator string="Products exported" colspan="4" />
 
42
        <field name="prod_new"/>
 
43
        <newline/>
 
44
        <field name="prod_update"/>
 
45
</form>'''
 
46
 
 
47
_export_done_fields = {
 
48
        'prod_new': {'string':'New products', 'type':'float', 'readonly': True},
 
49
        'prod_update': {'string':'Updated products', 'type':'float', 'readonly': True},
 
50
}
 
51
 
 
52
def _do_export(self, cr, uid, data, context):
 
53
        self.pool = pooler.get_pool(cr.dbname)
 
54
        ids = self.pool.get('esale_joomla.web').search(cr, uid, [])
 
55
        for website in self.pool.get('esale_joomla.web').browse(cr, uid, ids):
 
56
                pricelist = website.shop_id.pricelist_id.id
 
57
                if not pricelist:
 
58
                        raise wizard.except_wizard('UserError', 'You must define a pricelist in your shop !')
 
59
                server = xmlrpclib.ServerProxy("%s/tinyerp-synchro.php" % website.url)
 
60
                print 'SERVER', "%s/tinyerp-synchro.php" % website.url
 
61
 
 
62
                prod_new = 0
 
63
                prod_update = 0
 
64
 
 
65
                for categ in website.category_ids:
 
66
                        if not categ.category_id:
 
67
                                print 'Skipping Category', categ.name, categ.id
 
68
                                continue
 
69
                        cat_ids = [categ.category_id.id]
 
70
                        if categ.include_childs:
 
71
                                pass
 
72
                        #
 
73
                        # Use cat_ids and compute for childs
 
74
                        #
 
75
                        prod_ids = self.pool.get('product.product').search(cr, uid, [('categ_id','=',categ.category_id.id)])
 
76
                        for product in self.pool.get('product.product').browse(cr, uid, prod_ids):
 
77
 
 
78
                                category_id=categ.id
 
79
 
 
80
                                esale_joomla_id2 = self.pool.get('esale_joomla.product').search(cr, uid, [('web_id','=',website.id),('product_id','=',product.id)])
 
81
                                esale_joomla_id = 0
 
82
                                if esale_joomla_id2:
 
83
                                        esale_joomla_id = self.pool.get('esale_joomla.product').browse(cr, uid, esale_joomla_id2[0]).esale_joomla_id
 
84
 
 
85
                                tax_class_id = 1
 
86
                                print [pricelist], product.id, 1, 'list'
 
87
                                webproduct={
 
88
                                        'esale_joomla_id'       : esale_joomla_id or 0,
 
89
                                        'quantity'              : self.pool.get('product.product')._product_virtual_available(cr, uid, [product.id], '', False, {'shop':website.shop_id.id})[product.id],
 
90
                                        'model'                 : product.code or '',
 
91
                                        'price'                 : 10.0, #self.pool.get('product.pricelist').price_get(cr, uid, [pricelist], product.id, 1, 'list')[pricelist],
 
92
                                        'weight'                : float(product.weight),
 
93
                                        'tax_class_id'  : tax_class_id,
 
94
                                        'category_id'   : category_id,
 
95
                                }
 
96
 
 
97
                                attach_ids = self.pool.get('ir.attachment').search(cr, uid, [('res_model','=','product.product'), ('res_id', '=',product.id)])
 
98
                                data = self.pool.get('ir.attachment').read(cr, uid, attach_ids)
 
99
                                if len(data):
 
100
                                        webproduct['haspic'] = 1
 
101
                                        webproduct['picture'] = data[0]['datas']
 
102
                                        webproduct['fname'] = data[0]['datas_fname']
 
103
                                else:
 
104
                                        webproduct['haspic'] =0
 
105
                                
 
106
                                langs={}
 
107
                                products_pool=pooler.get_pool(cr.dbname).get('product.product')
 
108
                                for lang in website.language_ids:
 
109
                                        if lang.language_id and lang.language_id.translatable:
 
110
                                                langs[str(lang.esale_joomla_id)] = {
 
111
                                                        'name': products_pool.read(cr, uid, [osc_product.product_id.id], ['name'], {'lang': lang.language_id.code})[0]['name'] or '',
 
112
                                                        'description': products_pool.read(cr, uid, [osc_product.product_id.id], ['description_sale'], {'lang': lang.language_id.code})[0]['description_sale'] or ''
 
113
                                                }
 
114
 
 
115
                                webproduct['langs'] = langs
 
116
                                webproduct['name'] = str(product.name or '')
 
117
                                webproduct['description'] = str(product.description_sale or '')
 
118
 
 
119
                                print webproduct
 
120
 
 
121
                                osc_id=server.set_product(webproduct)
 
122
 
 
123
                                print osc_id
 
124
                                if osc_id!=webproduct['esale_joomla_id']:
 
125
                                        if esale_joomla_id2:
 
126
                                                self.pool.get('esale_joomla.product').write(cr, uid, [esale_joomla_id2[0]], {'esale_joomla_id': osc_id})
 
127
                                                print 'Changing', webproduct['esale_joomla_id'], 'to', osc_id
 
128
                                                prod_update += 1
 
129
                                        else:
 
130
                                                self.pool.get('esale_joomla.product').create(cr, uid, {
 
131
                                                        'product_id': product.id,
 
132
                                                        'web_id': website.id,
 
133
                                                        'esale_joomla_id': osc_id,
 
134
                                                        'name': product.name
 
135
                                                })
 
136
                                                prod_new += 1
 
137
                                else:
 
138
                                        prod_update += 1
 
139
        return {'prod_new':prod_new, 'prod_update':prod_update}
 
140
 
 
141
class wiz_esale_joomla_products(wizard.interface):
 
142
        states = {
 
143
                'init': {
 
144
                        'actions': [_do_export],
 
145
                        'result': {'type': 'form', 'arch': _export_done_form, 'fields': _export_done_fields, 'state': [('end', 'End')] }
 
146
                }
 
147
        }
 
148
wiz_esale_joomla_products('esale_joomla.products');