~openerp-dev/openobject-addons/6.1-opw-579908-msh

« back to all changes in this revision

Viewing changes to document_ftp/doc_conf_wizard.py

[IMP] document: remove ftp Integration from document and make seperate module for FTP Integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#    
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 
19
#
 
20
##############################################################################
 
21
 
 
22
import base64
 
23
 
 
24
from osv import osv, fields
 
25
from osv.orm import except_orm
 
26
import urlparse
 
27
 
 
28
import os
 
29
 
 
30
class document_configuration_wizard(osv.osv_memory):
 
31
    _name='document.configuration.wizard'
 
32
    _rec_name = 'Auto Directory configuration'
 
33
    _columns = {
 
34
        'host': fields.char('Server Address', size=64, help="Put here the server address or IP. " \
 
35
            "Keep localhost if you don't know what to write.", required=True)
 
36
    }
 
37
 
 
38
    def detect_ip_addr(self, cr, uid, context=None):
 
39
        def _detect_ip_addr(self, cr, uid, context=None):
 
40
            from array import array
 
41
            import socket
 
42
            from struct import pack, unpack
 
43
 
 
44
            try:
 
45
                import fcntl
 
46
            except ImportError:
 
47
                fcntl = None
 
48
 
 
49
            if not fcntl: # not UNIX:
 
50
                host = socket.gethostname()
 
51
                ip_addr = socket.gethostbyname(host)
 
52
            else: # UNIX:
 
53
                # get all interfaces:
 
54
                nbytes = 128 * 32
 
55
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
56
                names = array('B', '\0' * nbytes)
 
57
                outbytes = unpack('iL', fcntl.ioctl( s.fileno(), 0x8912, pack('iL', nbytes, names.buffer_info()[0])))[0]
 
58
                namestr = names.tostring()
 
59
                ifaces = [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)]
 
60
 
 
61
                for ifname in [iface for iface in ifaces if iface != 'lo']:
 
62
                    ip_addr = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, pack('256s', ifname[:15]))[20:24])
 
63
                    break
 
64
            return ip_addr
 
65
 
 
66
        try:
 
67
            ip_addr = _detect_ip_addr(self, cr, uid, context)
 
68
        except:
 
69
            ip_addr = 'localhost'
 
70
        return ip_addr
 
71
 
 
72
    _defaults = {
 
73
        'host': detect_ip_addr,
 
74
    }
 
75
 
 
76
    def action_cancel(self,cr,uid,ids,conect=None):
 
77
        return {
 
78
                'view_type': 'form',
 
79
                "view_mode": 'form',
 
80
                'res_model': 'ir.actions.configuration.wizard',
 
81
                'type': 'ir.actions.act_window',
 
82
                'target':'new',
 
83
         }
 
84
 
 
85
    def action_config(self, cr, uid, ids, context=None):
 
86
        conf = self.browse(cr, uid, ids[0], context)
 
87
        obj=self.pool.get('document.directory')
 
88
        objid=self.pool.get('ir.model.data')
 
89
 
 
90
        if self.pool.get('sale.order'):
 
91
            id = objid._get_id(cr, uid, 'document', 'dir_sale_order_all')
 
92
            id = objid.browse(cr, uid, id, context=context).res_id
 
93
            mid = self.pool.get('ir.model').search(cr, uid, [('model','=','sale.order')])
 
94
            obj.write(cr, uid, [id], {
 
95
                'type':'ressource',
 
96
                'ressource_type_id': mid[0],
 
97
                'domain': '[]',
 
98
            })
 
99
            aid = objid._get_id(cr, uid, 'sale', 'report_sale_order')
 
100
            aid = objid.browse(cr, uid, aid, context=context).res_id
 
101
 
 
102
            self.pool.get('document.directory.content').create(cr, uid, {
 
103
                'name': "Print Order",
 
104
                'suffix': "_print",
 
105
                'report_id': aid,
 
106
                'extension': '.pdf',
 
107
                'include_name': 1,
 
108
                'directory_id': id,
 
109
            })
 
110
            id = objid._get_id(cr, uid, 'document', 'dir_sale_order_quote')
 
111
            id = objid.browse(cr, uid, id, context=context).res_id
 
112
            obj.write(cr, uid, [id], {
 
113
                'type':'ressource',
 
114
                'ressource_type_id': mid[0],
 
115
                'domain': "[('state','=','draft')]",
 
116
            })
 
117
 
 
118
        if self.pool.get('product.product'):
 
119
            id = objid._get_id(cr, uid, 'document', 'dir_product')
 
120
            id = objid.browse(cr, uid, id, context=context).res_id
 
121
            mid = self.pool.get('ir.model').search(cr, uid, [('model','=','product.product')])
 
122
            obj.write(cr, uid, [id], {
 
123
                'type':'ressource',
 
124
                'ressource_type_id': mid[0],
 
125
            })
 
126
 
 
127
        if self.pool.get('stock.location'):
 
128
            aid = objid._get_id(cr, uid, 'stock', 'report_product_history')
 
129
            aid = objid.browse(cr, uid, aid, context=context).res_id
 
130
 
 
131
            self.pool.get('document.directory.content').create(cr, uid, {
 
132
                'name': "Product Stock",
 
133
                'suffix': "_stock_forecast",
 
134
                'report_id': aid,
 
135
                'extension': '.pdf',
 
136
                'include_name': 1,
 
137
                'directory_id': id,
 
138
            })
 
139
 
 
140
        if self.pool.get('account.analytic.account'):
 
141
            id = objid._get_id(cr, uid, 'document', 'dir_project')
 
142
            id = objid.browse(cr, uid, id, context=context).res_id
 
143
            mid = self.pool.get('ir.model').search(cr, uid, [('model','=','account.analytic.account')])
 
144
            obj.write(cr, uid, [id], {
 
145
                'type':'ressource',
 
146
                'ressource_type_id': mid[0],
 
147
                'domain': '[]',
 
148
                'ressource_tree': 1
 
149
        })
 
150
 
 
151
        # Update the action for FTP browse.
 
152
        aid = objid._get_id(cr, uid, 'document', 'action_document_browse')
 
153
        aid = objid.browse(cr, uid, aid, context=context).res_id
 
154
        self.pool.get('ir.actions.url').write(cr, uid, [aid], {'url': 'ftp://'+(conf.host or 'localhost')+':8021/'})
 
155
 
 
156
        return {
 
157
                'view_type': 'form',
 
158
                "view_mode": 'form',
 
159
                'res_model': 'ir.actions.configuration.wizard',
 
160
                'type': 'ir.actions.act_window',
 
161
                'target': 'new',
 
162
        }
 
163
document_configuration_wizard()