[REF]
[odoo/odoo.git] / addons / auction / wizard / wizard_aie_send_result.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 #
23 # Does not properly work concurently !!!
24 #
25
26 import wizard
27 import netsvc
28 from tools.translate import _
29
30 login_form = '''<?xml version="1.0"?>
31 <form title="Login">
32     <field name="uname"></field>
33     <newline/>
34     <field name="password"></field>
35 </form>'''
36
37
38 send_form = '''<?xml version="1.0"?>
39 <form title="Selection">
40     <field name="uname"></field>
41     <field name="password"></field>
42     <newline/>
43     <field name="objects"></field>
44     <newline/>
45     <field name="dates" colspan="3"></field>
46 </form>'''
47
48 login_fields = {
49     'uname': {'string':'Login', 'type':'char'},
50     'password': {'string':'Password', 'type':'char'},
51     'dates': {'string':'Auction Date', 'type':'selection', 'selection':[]}
52 }
53
54 send_fields = {
55     'uname': {'string':'Login', 'type':'char', 'readonly':True},
56     'password': {'string':'Password', 'type':'char', 'readonly':True},
57     'objects': {'string':'# of objects', 'type':'integer', 'readonly':True},
58     'dates': {'string':'Auction Date', 'type':'selection', 'selection':[]}
59 }
60
61 def _catalog_send(uname, passwd, did, catalog):
62     def post_multipart(host, selector, fields, files):
63         def encode_multipart_formdata(fields, files):
64             BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
65             CRLF = '\r\n'
66             L = []
67             for (key, value) in fields:
68                 L.append('--' + BOUNDARY)
69                 L.append('Content-Disposition: form-data; name="%s"' % key)
70                 L.append('')
71                 L.append(value)
72             for (key,value) in files:
73                 L.append('--' + BOUNDARY)
74                 L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, key+'.pickle'))
75                 L.append('Content-Type: application/octet-stream')
76                 L.append('')
77                 L.append(value)
78             L.append('--' + BOUNDARY + '--')
79             L.append('')
80             body = CRLF.join(L)
81             content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
82             return content_type, body
83         content_type, body = encode_multipart_formdata(fields, files)
84         import httplib
85
86         headers = {"Content-type": content_type, "Accept": "*/*"}
87         conn = httplib.HTTPConnection(host)
88         conn.request("POST", '/bin/catalog_result.cgi', body, headers = headers)
89         response = conn.getresponse()
90         val = response.status
91         conn.close()
92         return val
93     return post_multipart('auction-in-europe.com', "/bin/catalog_result.cgi", (('uname',uname),('password',passwd),('did',did)),(('file',catalog),))
94
95 def _get_dates(self,cr,uid, datas, context):
96     global send_fields
97     import httplib
98     conn = httplib.HTTPConnection('www.auction-in-europe.com')
99     conn.request("GET", "/aie_upload/dates_get_result.php?uname=%s&passwd=%s" % (datas['form']['uname'], datas['form']['password']))
100     response = conn.getresponse()
101     if response.status == 200:
102         def _date_decode(x):
103             return (x.split(' - ')[0], (' - '.join(x.split(' - ')[1:]).decode('latin1').encode('utf-8')))
104         send_fields['dates']['selection'] = map(_date_decode, response.read().split('\n'))
105     else:
106         raise wizard.except_wizard(_('Error'),
107                                    _("Connection to WWW.Auction-in-Europe.com failed !"))
108     return {'objects':len(datas['ids'])}
109
110 def _send(self,cr,uid, datas, context):
111     import pickle
112     service = netsvc.LocalService("object_proxy")
113     lots = service.execute(cr.dbname,uid, 'auction.lots', 'read', datas['ids'],  ['obj_num','obj_price'])
114     args = pickle.dumps(lots)
115     _catalog_send(datas['form']['uname'],datas['form']['password'], datas['form']['dates'], args)
116     return {}
117
118 class wiz_auc_lots_pay(wizard.interface):
119     states = {
120         'init': {
121             'actions': [],
122             'result': {'type': 'form', 'arch':login_form, 'fields': login_fields, 'state':[('date_ask','Continue'),('end','Cancel')]}
123         },
124         'date_ask': {
125             'actions': [_get_dates],
126             'result': {'type': 'form', 'arch':send_form, 'fields': send_fields, 'state':[('send','Send on your website'),('end','Cancel')]}
127         },
128         'send': {
129             'actions': [_send],
130             'result': {'type': 'state', 'state':'end'}
131         }
132     }
133 wiz_auc_lots_pay('auction.lots.send.aie.results');
134
135
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
137