~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to cpi_be/cpi_be_schedulers.py

  • Committer: cth at technofluid
  • Date: 2013-08-01 07:00:41 UTC
  • Revision ID: cth@technofluid.be-20130801070041-umbk0u1zuck4jlcw
Ajout du module IPC

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from osv import osv
 
2
from datetime import datetime
 
3
import urllib2
 
4
from xml.dom.minidom import parse, parseString
 
5
 
 
6
class scheduler_check_landefeld(osv.osv):
 
7
    _name = 'scheduler.cpi_be'
 
8
    
 
9
    def import_cpi_be_all(self, cr, uid, ids=False, context=None):
 
10
        u1=urllib2.urlopen('http://ng3.economie.fgov.be/ni/indicators/XML/ConsumerPriceIndices.xml')
 
11
        dom=parse(u1)
 
12
        
 
13
        cpi_be_entry_pool = self.pool.get("cpi.be.entry")
 
14
        cpi_be_type_pool = self.pool.get("cpi.be.type")
 
15
        
 
16
        if dom.childNodes:
 
17
            for node_type in dom.childNodes[0].childNodes:
 
18
                if node_type.nodeType == node_type.ELEMENT_NODE and node_type.nodeName == 'INDEX':
 
19
                    
 
20
                    type_id = False
 
21
                    year = False
 
22
                    month = False
 
23
                    value = False
 
24
                    
 
25
                    #BROWSE TYPE AND FIND IT (OR CREATE IT IF NOT EXIST)                    
 
26
                    type_name = node_type.getAttribute("BASE")
 
27
                    type_description = node_type.getAttribute("DESCRIPTION")
 
28
                    type_ids = cpi_be_type_pool.search(cr, uid, [('name','=',type_name)], context=context)
 
29
                    if not type_ids:
 
30
                        type_id = cpi_be_type_pool.create(cr, uid, {'name':type_name, 'description':type_description}, context=context)
 
31
                    else:
 
32
                        type_id = type_ids[0]
 
33
                    
 
34
                    #BROWSE YEARS
 
35
                    for node_year in node_type.childNodes:
 
36
                        if node_year.nodeType == node_year.ELEMENT_NODE and node_year.nodeName == 'YEAR':
 
37
                            year = int(node_year.getAttribute("NAME"))
 
38
                            for values_node in node_year.childNodes:
 
39
                                if values_node.nodeType == values_node.ELEMENT_NODE and values_node.nodeName == 'VALUES':
 
40
                                    #BROWSE MONTHS
 
41
                                    for entry_node in values_node.childNodes:
 
42
                                        if entry_node.nodeType == entry_node.ELEMENT_NODE and entry_node.nodeName == 'ENTRY':
 
43
                                            month = entry_node.getAttribute("MONTH")
 
44
                                            if entry_node.childNodes and entry_node.childNodes[0].nodeType == entry_node.childNodes[0].TEXT_NODE:
 
45
                                                value = float(entry_node.childNodes[0].data)
 
46
                                            
 
47
                                            #CREATE OR UPDATE ENTRY
 
48
                                            if type_id and year and month and value:
 
49
                                                entries = cpi_be_entry_pool.search(cr, uid, [('type_id','=',type_id),('year','=',year),('month','=',month)], context=context)
 
50
                                                if entries:
 
51
                                                    cpi_be_entry_pool.write(cr, uid, entries, {'value':value}, context=context)
 
52
                                                else:
 
53
                                                    cpi_be_entry_pool.create(cr, uid, {'type_id':type_id, 'year':year, 'month':month, 'value':value}, context=context)
 
54
                    
 
55
                
 
56
        return True
 
57
    
 
58
scheduler_check_landefeld()
 
 
b'\\ No newline at end of file'