~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to currency_rate/wizard/currency_rate.py

bugfix in overlay creation system

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import wizard
 
2
import pooler
 
3
import time
 
4
import urllib,urllib2
 
5
from xml.dom import minidom
 
6
 
 
7
_currency_form = '''<?xml version="1.0"?>
 
8
<form string="Currency Rate">
 
9
    <field name="from_cur" />
 
10
    <field name="to_cur"/>
 
11
    <field name="amount"/>
 
12
</form>'''
 
13
 
 
14
_currency_fields = {
 
15
    'from_cur': {'string': 'Currency From', 'type': 'many2one','relation':'res.currency','required':True},
 
16
    'to_cur': {'string': 'Currency To', 'type': 'many2one','relation':'res.currency','required':True},
 
17
    'amount': {'string': 'Amount', 'type': 'float'},
 
18
}
 
19
 
 
20
_result_form = '''<?xml version="1.0"?>
 
21
<form string="Result">   
 
22
    <field name="final_amount"/>
 
23
    <field name="rate"/>
 
24
</form>'''
 
25
 
 
26
_result_fields = {   
 
27
    'final_amount': {'string': 'Amount', 'type': 'float'},
 
28
    'rate': {'string': 'Current Rate', 'type': 'float'},
 
29
}
 
30
 
 
31
 
 
32
class wizard_currency_rate(wizard.interface):
 
33
    
 
34
    def _convert(self, cr, uid, data, context):
 
35
        res={}
 
36
        try:
 
37
            cur_obj = pooler.get_pool(cr.dbname).get('res.currency')
 
38
            
 
39
            from_cur_id = data['form']['from_cur']
 
40
            to_cur_id = data['form']['to_cur']
 
41
            amount = data['form']['amount']
 
42
           
 
43
            from_code = cur_obj.browse(self.cr, self.uid, from_cur_id).code
 
44
            to_code = cur_obj.browse(self.cr, self.uid, to_cur_id).code
 
45
            
 
46
            urldata = {'FromCurrency':from_code, 'ToCurrency':to_code ,'method':'GET'}
 
47
            data = urllib.urlencode(urldata)
 
48
            req = urllib2.Request('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate',data)
 
49
            response = urllib2.urlopen(req)
 
50
            
 
51
            data = response.read()
 
52
            xmldoc = minidom.parseString(data)
 
53
            rate = xmldoc.documentElement.firstChild.nodeValue
 
54
        
 
55
            if rate:
 
56
                res['final_amount'] = (amount * rate) or 0.0
 
57
                res['rate'] = rate
 
58
        except:
 
59
            return res
 
60
        return res
 
61
        
 
62
    states = {
 
63
        'init': {
 
64
            'actions': [],
 
65
            'result': {'type': 'form', 'arch':_currency_form, 'fields':_currency_fields, 'state':[('end','Cancel'),('convert','Convert')]}
 
66
        },
 
67
         'convert': {
 
68
            'actions': [_convert],
 
69
            'result': {'type': 'form', 'arch':_result_form, 'fields':_result_fields, 'state':[('init','Back'),('end','Exit')]}
 
70
        }
 
71
    }
 
72
wizard_currency_rate('currency.rate')
 
 
b'\\ No newline at end of file'