~sergio-incaser/openerp-spain/openerp-spain

« back to all changes in this revision

Viewing changes to extra_addons/radiotv/wizard/export_table.py

  • Committer: Jordi Esteve
  • Date: 2009-12-14 17:53:50 UTC
  • mfrom: (81.1.90 lp-openerp-spain-5.0)
  • Revision ID: jesteve@zikzakmedia.com-20091214175350-6vzzt3avtsof25a2
Recuperación del estado actual del repositorio de localización española de OpenERP: Aplicación de todos los merges pendientes desde la versión 81 (15-11-2008) hasta la actual (02-12-2009)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##############################################################################
2
 
#
3
 
# Copyright (c) 2007 Zikzakmedia SL (http://www.zikzakmedia.com) 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 netsvc
29
 
import pooler
30
 
import time
31
 
import urllib
32
 
import base64
33
 
from osv import osv
34
 
 
35
 
def export_table(self, cr, uid, data, context, server, table, fields, filter = [], filterphp = ''):
36
 
        """Export (synchronize) the fields of the radiotv.table to Joomla PHP server.
37
 
           Only the records matching the filter are exported.
38
 
           filterphp is the same filter in SQL notation to used in the PHP code.
39
 
           New records are inserted, existing records are updated and removed records are deleted"""
40
 
        pool = pooler.get_pool(cr.dbname)
41
 
        obj = 'radiotv.'+table
42
 
        tbl = 'radiotv_'+table
43
 
        new = 0
44
 
        update = 0
45
 
        server.reset_table(tbl)
46
 
        elem_ids = pool.get(obj).search(cr, uid, filter)
47
 
        for elem in pool.get(obj).browse(cr, uid, elem_ids, context):
48
 
 
49
 
                vals = {}
50
 
                for field in fields:
51
 
                        if field[-3:] == "_id":
52
 
                                vals[field] = getattr(elem, field).id
53
 
                        elif field[-4:] == "_ids":
54
 
                                vals[field] = [c.id for c in getattr(elem, field)]
55
 
                        else:
56
 
                                vals[field] = getattr(elem, field)
57
 
 
58
 
                attach_ids = pool.get('ir.attachment').search(cr, uid, [('res_model','=',obj), ('res_id', '=',elem.id)])
59
 
                cont = 0
60
 
                for data in pool.get('ir.attachment').browse(cr, uid, attach_ids, context):
61
 
                        s = data['datas_fname'].split('.')
62
 
                        extension = s[-1].lower()
63
 
                        s.pop()
64
 
                        name = ".".join(s)
65
 
                        #print name + " " + extension
66
 
                        if extension in ['jpeg', 'jpe', 'jpg', 'gif', 'png']:
67
 
                                if extension in ['jpeg', 'jpe', 'jpg']:
68
 
                                        extension='jpeg'
69
 
                                if not data['link']:
70
 
                                        vals['picture'+str(cont)] = data['datas']
71
 
                                else:
72
 
                                        try:
73
 
                                                vals['picture'+str(cont)] = base64.encodestring(urllib.urlopen(data['link']).read())
74
 
                                        except:
75
 
                                                continue
76
 
                                vals['fname'+str(cont)] = name + '.' + extension
77
 
                                cont = cont + 1
78
 
                #print vals
79
 
 
80
 
                if server.set_table(tbl, vals):
81
 
                        new += 1
82
 
                else:
83
 
                        update += 1
84
 
 
85
 
        delete = server.delete_table(tbl, filterphp)
86
 
        return (new, update, delete)
87
 
 
88
 
 
89
 
def export_write(self, cr, uid, server, table, ids, vals, context):
90
 
        """Synchronize the fields defined in vals of the radiotv.table to Joomla PHP server.
91
 
           Only the records with ids are exported.
92
 
           New records are inserted, existing records are updated"""
93
 
        pool = pooler.get_pool(cr.dbname)
94
 
        obj = 'radiotv.'+table
95
 
        tbl = 'radiotv_'+table
96
 
        new = 0
97
 
        update = 0
98
 
        for field in vals.keys():
99
 
                if field[-4:] == "_ids":
100
 
                        vals[field] = vals[field][0][2]
101
 
        for id in ids:
102
 
                vals['id'] = id
103
 
 
104
 
                attach_ids = pool.get('ir.attachment').search(cr, uid, [('res_model','=',obj), ('res_id', '=',id)])
105
 
                cont = 0
106
 
                for data in pool.get('ir.attachment').browse(cr, uid, attach_ids, context):
107
 
                        s = data['datas_fname'].split('.')
108
 
                        extension = s[-1].lower()
109
 
                        s.pop()
110
 
                        name = ".".join(s)
111
 
                        #print name + " " + extension
112
 
                        if extension in ['jpeg', 'jpe', 'jpg', 'gif', 'png']:
113
 
                                if extension in ['jpeg', 'jpe', 'jpg']:
114
 
                                        extension='jpeg'
115
 
                                if not data['link']:
116
 
                                        vals['picture'+str(cont)] = data['datas']
117
 
                                else:
118
 
                                        try:
119
 
                                                vals['picture'+str(cont)] = base64.encodestring(urllib.urlopen(data['link']).read())
120
 
                                        except:
121
 
                                                continue
122
 
                                vals['fname'+str(cont)] = name + '.' + extension
123
 
                                cont = cont + 1
124
 
                #print vals
125
 
 
126
 
                if server.set_table(tbl, vals):
127
 
                        new += 1
128
 
                else:
129
 
                        update += 1
130
 
 
131
 
        return (new, update)
132
 
 
133
 
 
134
 
def export_ulink(self, cr, uid, server, table, ids, table_rel=None, field_rel=None):
135
 
        """Synchronize the radiotv.table to Joomla PHP server.
136
 
           Only the records with ids are deleted.
137
 
           If table_rel and field_rel are defined, also deletes the records in the table_rel"""
138
 
        tbl = 'radiotv_'+table
139
 
        delete = server.delete_items(tbl, ids, "id")
140
 
        if table_rel != None:
141
 
                tbl = 'radiotv_'+table_rel
142
 
                server.delete_items(tbl, ids, field_rel)
143
 
        return delete