~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to db_tools/wizard/db_tools.py

  • Committer: Jose Antonio
  • Date: 2012-06-13 22:24:45 UTC
  • mto: This revision was merged to the branch mainline in revision 312.
  • Revision ID: jose@vauxoo.com-20120613222445-ybndycdaaw2puqyz

[ADD] Add wizard to add incidences from xls field

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
###########################################################################
3
 
#    Module Writen to OpenERP, Open Source Management Solution
4
 
#
5
 
#    Copyright (c) 2010 Vauxoo - http://www.vauxoo.com/
6
 
#    All Rights Reserved.
7
 
#    info Vauxoo (info@vauxoo.com)
8
 
############################################################################
9
 
#    Coded by: Luis Torres (luis_t@vauxoo.com)
10
 
#              Julio (julio@vauxoo.com)
11
 
############################################################################
12
 
#
13
 
#    This program is free software: you can redistribute it and/or modify
14
 
#    it under the terms of the GNU Affero General Public License as
15
 
#    published by the Free Software Foundation, either version 3 of the
16
 
#    License, or (at your option) any later version.
17
 
#
18
 
#    This program is distributed in the hope that it will be useful,
19
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
#    GNU Affero General Public License for more details.
22
 
#
23
 
#    You should have received a copy of the GNU Affero General Public License
24
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 
#
26
 
##############################################################################
27
 
from openerp.osv import osv, fields
28
 
import xmlrpclib
29
 
import sys
30
 
import os
31
 
import time
32
 
import base64
33
 
import socket
34
 
from openerp.tools.translate import _
35
 
 
36
 
import service
37
 
import tempfile
38
 
 
39
 
waittime = 10
40
 
wait_count = 0
41
 
wait_limit = 12
42
 
 
43
 
 
44
 
class db_tools(osv.TransientModel):
45
 
    _name = 'db.tools'
46
 
 
47
 
    def db(self, cr, uid, context=None):
48
 
        ws_obj = service.web_services.db()
49
 
        db_list = ws_obj.exp_list()
50
 
        list_db = []
51
 
        for db in db_list:
52
 
            list_db.append((db.lower(), db))
53
 
        return list_db
54
 
 
55
 
    def _db_default(self, cr, uid, context=None):
56
 
        res = self.db(cr, uid, context)
57
 
        list_db = []
58
 
        for db in res:
59
 
            if ((cr.dbname, cr.dbname) == db):
60
 
                list_db.append(db)
61
 
        return list_db
62
 
 
63
 
    def db_default(self, cr, uid, context=None):
64
 
        return self._db_default(cr, uid, context)[0][0]
65
 
 
66
 
    _columns = {
67
 
        'filter': fields.selection([
68
 
            ('backup', 'Backup'),
69
 
            ('restore', 'Backup-Restore')], 'Filter',
70
 
            help='Backup creates a backup of the database and stored\
71
 
                in the path indicated, Restore-Backup creates a backup\
72
 
                of the database, and in turn restores the new name'),
73
 
        'password': fields.char('Password', size=64, required=True),
74
 
        'list_db': fields.selection(_db_default, 'Data Base Restore',
75
 
                                    required=True, readonly=True),
76
 
        'name_db': fields.char('Name DB', size=128, required=True),
77
 
        'path_save_db': fields.char('Path save DB', size=256, required=True)
78
 
    }
79
 
 
80
 
    _defaults = {
81
 
        'filter': 'restore',
82
 
        'list_db': db_default,
83
 
        'name_db': lambda self, cr, uid, context=None: cr.dbname +
84
 
        time.strftime('_%Y%m%d_%H%M%S.backup'),
85
 
        'path_save_db': tempfile.gettempdir()
86
 
    }
87
 
 
88
 
    def backup_db(self, cr, uid, ids, uri=False, dbname=''):
89
 
        ws_obj = service.web_services.db()
90
 
        data = self.browse(cr, uid, ids[0])
91
 
        filename = os.path.join(data.path_save_db, data.name_db)
92
 
        dump_db64 = ws_obj.exp_dump(dbname)
93
 
        dump = base64.decodestring(dump_db64)
94
 
        file_db = file(filename, 'wb')
95
 
        file_db.write(dump)
96
 
        file_db.close()
97
 
        return filename
98
 
 
99
 
    def backup_restore_db(self, cr, uid, ids, uri, dbname=''):
100
 
        res = self.backup_db(cr, uid, ids, uri, dbname)
101
 
        data = self.browse(cr, uid, ids[0])
102
 
        data_base = os.path.basename(res)
103
 
        name_db = data_base[:data_base.rfind(".")]
104
 
        print 'name_db', name_db
105
 
        f = file(res, 'r')
106
 
        data_b64 = base64.encodestring(f.read())
107
 
        f.close()
108
 
        password = data.password
109
 
        ws_obj = service.web_services.db()
110
 
        ws_obj.exp_restore(name_db, data_b64)
111
 
        return True
112
 
 
113
 
    def find_db(self, cr, uid, ids, context=None):
114
 
        return True
115
 
 
116
 
    def confirm_action(self, cr, uid, ids, context=None):
117
 
        uri = context.get('uri', False)
118
 
        for lin in self.browse(cr, uid, ids, context=context):
119
 
            if lin.filter == 'backup':
120
 
                self.backup_db(cr, uid, ids, context.get(
121
 
                    'uri', False), lin.list_db)
122
 
            if lin.filter == 'restore':
123
 
                self.backup_restore_db(cr, uid, ids, context.get(
124
 
                    'uri', False), lin.list_db)
125
 
        return {}
126
 
 
127
 
    def cancel_action(self, cr, uid, ids, context=None):
128
 
        return {}