~anna-g/micronaet/anna

« back to all changes in this revision

Viewing changes to extra_search/wizard/search_wizard.py

  • Committer: Anna Micronaet
  • Date: 2013-07-18 09:08:36 UTC
  • Revision ID: anna@micronaet.it-20130718090836-ssmst48rrnvcd69w
Tolti tutti i moduli

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) 2004-2009 Tiny SPRL (<http://tiny.be>). 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
 
 
25
 
class search_database_wizard(osv.osv_memory):
26
 
    """
27
 
    Search a string in database setting up un listed database and return result 
28
 
    and the possibility to go in search in actual DB (res.partner)
29
 
    """
30
 
    
31
 
    _name = 'search.database.wizard'
32
 
    
33
 
    def return_view(self, cr, uid, name, res_id):
34
 
        '''General purpose function that return dict action for next step of 
35
 
           the wizard
36
 
        '''
37
 
        return {
38
 
            'name': 'Searched partners',
39
 
            'view_type': 'form',
40
 
            'view_mode': 'tree,form',
41
 
            'res_model': 'res.partner',
42
 
            'type': 'ir.actions.act_window',
43
 
            'target': 'current',
44
 
            'domain': str([('id', 'in', res_id)]),
45
 
        }        
46
 
 
47
 
 
48
 
    def search_text_in_other_database(self,cr, uid, ids, context=None):
49
 
        ''' Search button in wizard windows, search in other DB list and 
50
 
            comunicate existence in other DB res.partner finded in list box
51
 
        '''
52
 
        import xmlrpclib
53
 
 
54
 
        # TODO parametrizzare:
55
 
        server='localhost'
56
 
        port='8069'
57
 
        user='admin'
58
 
        pwd='cgp.fmsp6'
59
 
        
60
 
        if context is None:
61
 
           context = {}
62
 
 
63
 
        # Read data in wizard form:   
64
 
        wiz_proxy=self.browse(cr, uid, ids, context=context)
65
 
        search_string=wiz_proxy[0].name 
66
 
        
67
 
        db_ids = self.pool.get('search.database').search(cr, uid, [('search','=', True)])
68
 
        finded_result=""
69
 
        if db_ids: # if there is some DB in list:
70
 
           for db in self.pool.get('search.database').browse(cr, uid, db_ids, context=context):
71
 
               dbname=db.name     
72
 
               sock = xmlrpclib.ServerProxy('http://' + server + ':' + port + '/xmlrpc/common', allow_none=True)
73
 
               uid_extra = sock.login(dbname ,user ,pwd)
74
 
               sock = xmlrpclib.ServerProxy('http://' + server + ':' + port + '/xmlrpc/object', allow_none=True)               
75
 
 
76
 
               partner_ids = sock.execute(dbname, uid_extra, pwd, 'res.partner', 'search', [('name', 'ilike', search_string),])
77
 
               if partner_ids:
78
 
                  partner_read = sock.execute(dbname, uid_extra, pwd, 'res.partner', 'read', partner_ids)
79
 
                  for partner in partner_read:
80
 
                      finded_result += "[%s] %s - (%s - %s) %s - %s\n"%(dbname, partner['name'], partner['city'] or "", partner['country'] and partner['country'][1], partner['phone'] or "", partner['email'] or "")
81
 
        
82
 
        partner_current_ids=len(self.pool.get('res.partner').search(cr, uid, [('name', 'ilike', search_string)], context=context))
83
 
        # Change fields (status) of the wizard:
84
 
        wiz_result=self.write(cr, uid, ids, {'state': 'find',
85
 
                                             'find': finded_result,  
86
 
                                             'current': partner_current_ids,
87
 
                                             })
88
 
        return True
89
 
 
90
 
    def search_text_in_database(self,cr, uid, ids, context=None):
91
 
        ''' Search button in wizard windows, search in current DB and 
92
 
            comunicate existence in other DB res.partner finded
93
 
        '''
94
 
        
95
 
        if context is None:
96
 
           context = {}
97
 
        
98
 
        # Read data in wizard form:   
99
 
        wiz_proxy=self.browse(cr, uid, ids, context=context)
100
 
        search_string=wiz_proxy[0].name 
101
 
        
102
 
        # Cerco nell'attuale DB i partner che possono andare bene:
103
 
        partner_ids=self.pool.get('res.partner').search(cr, uid, [('name', 'ilike', search_string)], context=context)
104
 
        
105
 
        return self.return_view(cr, uid, '', partner_ids) 
106
 
     
107
 
    _columns = {
108
 
                'name':fields.char('Text to search', size=60, required=True, readonly=False),
109
 
                'find': fields.text('Finded in other DB'),
110
 
                'current': fields.integer('Find current DB'),
111
 
                'state':fields.selection([
112
 
                    ('search','Search'),
113
 
                    ('find','Find in other DB'),                    
114
 
                ],'State', select=True, readonly=True),
115
 
               }
116
 
               
117
 
    _defaults = {
118
 
                 'state': lambda *a: 'search',
119
 
                 'current': lambda *a: 0,
120
 
                }           
121
 
search_database_wizard()
122
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
123