[MERGE] upstream
[odoo/odoo.git] / openerp / report / custom.py
index b33065f..84634d1 100644 (file)
@@ -21,8 +21,8 @@
 
 import os
 import time
-import openerp.netsvc as netsvc
 
+import openerp
 import openerp.tools as tools
 from openerp.tools.safe_eval import safe_eval as eval
 import print_xml
@@ -32,7 +32,6 @@ import common
 from openerp.osv.osv import except_osv
 from openerp.osv.orm import browse_null
 from openerp.osv.orm import browse_record_list
-import openerp.pooler as pooler
 from pychart import *
 import misc
 import cStringIO
@@ -67,7 +66,6 @@ class report_custom(report_int):
     #
     def _row_get(self, cr, uid, objs, fields, conditions, row_canvas=None, group_by=None):
         result = []
-        tmp = []
         for obj in objs:
             tobreak = False
             for cond in conditions:
@@ -98,7 +96,7 @@ class report_custom(report_int):
             else:
                 # Process group_by data first
                 key = []
-                if group_by != None and fields[group_by] != None:
+                if group_by is not None and fields[group_by] is not None:
                     if fields[group_by][0] in levels.keys():
                         key.append(fields[group_by][0])
                     for l in levels.keys():
@@ -129,27 +127,28 @@ class report_custom(report_int):
     def create(self, cr, uid, ids, datas, context=None):
         if not context:
             context={}
-        self.pool = pooler.get_pool(cr.dbname)
-        report = self.pool.get('ir.report.custom').browse(cr, uid, [datas['report_id']])[0]
+        self.pool = openerp.registry(cr.dbname)
+        report = self.pool['ir.report.custom'].browse(cr, uid, [datas['report_id']])[0]
         datas['model'] = report.model_id.model
         if report.menu_id:
-            ids = self.pool.get(report.model_id.model).search(cr, uid, [])
+            ids = self.pool[report.model_id.model].search(cr, uid, [])
             datas['ids'] = ids
 
         report_id = datas['report_id']
-        report = self.pool.get('ir.report.custom').read(cr, uid, [report_id], context=context)[0]
-        fields = self.pool.get('ir.report.custom.fields').read(cr, uid, report['fields_child0'], context=context)
+        report = self.pool['ir.report.custom'].read(cr, uid, [report_id], context=context)[0]
+        fields = self.pool['ir.report.custom.fields'].read(cr, uid, report['fields_child0'], context=context)
 
         fields.sort(lambda x,y : x['sequence'] - y['sequence'])
 
         if report['field_parent']:
-            parent_field = self.pool.get('ir.model.fields').read(cr, uid, [report['field_parent'][0]], ['model'])
-        model_name = self.pool.get('ir.model').read(cr, uid, [report['model_id'][0]], ['model'], context=context)[0]['model']
+            parent_field = self.pool['ir.model.fields'].read(cr, uid, [report['field_parent'][0]], ['model'])
+        model_name = self.pool['ir.model'].read(cr, uid, [report['model_id'][0]], ['model'], context=context)[0]['model']
 
-        fct = {}
-        fct['id'] = lambda x : x
-        fct['gety'] = lambda x: x.split('-')[0]
-        fct['in'] = lambda x: x.split(',')
+        fct = {
+            'id': lambda x: x,
+            'gety': lambda x: x.split('-')[0],
+           'in': lambda x: x.split(',')
+        }
         new_fields = []
         new_cond = []
         for f in fields:
@@ -159,7 +158,7 @@ class report_custom(report_int):
                 field_child = f['field_child'+str(i)]
                 if field_child:
                     row.append(
-                        self.pool.get('ir.model.fields').read(cr, uid, [field_child[0]], ['name'], context=context)[0]['name']
+                        self.pool['ir.model.fields'].read(cr, uid, [field_child[0]], ['name'], context=context)[0]['name']
                     )
                     if f['fc'+str(i)+'_operande']:
                         fct_name = 'id'
@@ -172,7 +171,7 @@ class report_custom(report_int):
                         cond.append(None)
             new_fields.append(row)
             new_cond.append(cond)
-        objs = self.pool.get(model_name).browse(cr, uid, ids)
+        objs = self.pool[model_name].browse(cr, uid, ids)
 
         # Group by
         groupby = None
@@ -214,18 +213,16 @@ class report_custom(report_int):
         new_res = []
 
         prev = None
-        if groupby != None:
+        if groupby is not None:
             res_dic = {}
             for line in results:
                 if not line[groupby] and prev in res_dic:
                     res_dic[prev].append(line)
                 else:
                     prev = line[groupby]
-                    if line[groupby] in res_dic:
-                        res_dic[line[groupby]].append(line)
-                    else:
-                        res_dic[line[groupby]] = []
-                        res_dic[line[groupby]].append(line)
+                    res_dic.setdefault(line[groupby], [])
+                    res_dic[line[groupby]].append(line)
+
             #we use the keys in results since they are ordered, whereas in res_dic.heys() they aren't
             for key in filter(None, [x[groupby] for x in results]):
                 row = []
@@ -276,7 +273,7 @@ class report_custom(report_int):
                 res = self._create_bars(cr,uid, ids, report, fields, results2, context)
             elif report['type']=='line':
                 res = self._create_lines(cr,uid, ids, report, fields, results2, context)
-        return (self.obj.get(), 'pdf')
+        return self.obj.get(), 'pdf'
 
     def _create_tree(self, uid, ids, report, fields, level, results, context):
         pageSize=common.pageSize.get(report['print_format'], [210.0,297.0])
@@ -326,7 +323,7 @@ class report_custom(report_int):
                     col.attrib.update(para='yes',
                                       tree='yes',
                                       space=str(3*shift)+'mm')
-                if line[f] != None:
+                if line[f] is not None:
                     col.text = prefix+str(line[f]) or ''
                 else:
                     col.text = '/'
@@ -343,7 +340,7 @@ class report_custom(report_int):
 
 
     def _create_lines(self, cr, uid, ids, report, fields, results, context):
-        pool = pooler.get_pool(cr.dbname)
+        pool = openerp.registry(cr.dbname)
         pdf_string = cStringIO.StringIO()
         can = canvas.init(fname=pdf_string, format='pdf')
         
@@ -354,18 +351,19 @@ class report_custom(report_int):
         x_axis = axis.X(label = fields[0]['name'], format="/a-30{}%s"),
         y_axis = axis.Y(label = ', '.join(map(lambda x : x['name'], fields[1:]))))
         
-        process_date = {}
-        process_date['D'] = lambda x : reduce(lambda xx,yy : xx+'-'+yy,x.split('-')[1:3])
-        process_date['M'] = lambda x : x.split('-')[1]
-        process_date['Y'] = lambda x : x.split('-')[0]
+        process_date = {
+            'D': lambda x: reduce(lambda xx, yy: xx + '-' + yy, x.split('-')[1:3]),
+            'M': lambda x: x.split('-')[1],
+            'Y': lambda x: x.split('-')[0]
+        }
 
-        order_date = {}
-        order_date['D'] = lambda x : time.mktime((2005,int(x.split('-')[0]), int(x.split('-')[1]),0,0,0,0,0,0))
-        order_date['M'] = lambda x : x
-        order_date['Y'] = lambda x : x
+        order_date = {
+            'D': lambda x: time.mktime((2005, int(x.split('-')[0]), int(x.split('-')[1]), 0, 0, 0, 0, 0, 0)),
+            'M': lambda x: x,
+            'Y': lambda x: x
+        }
 
         abscissa = []
-        tmp = {}
         
         idx = 0 
         date_idx = None
@@ -373,7 +371,7 @@ class report_custom(report_int):
         for f in fields:
             field_id = (f['field_child3'] and f['field_child3'][0]) or (f['field_child2'] and f['field_child2'][0]) or (f['field_child1'] and f['field_child1'][0]) or (f['field_child0'] and f['field_child0'][0])
             if field_id:
-                type = pool.get('ir.model.fields').read(cr, uid, [field_id],['ttype'])
+                type = pool['ir.model.fields'].read(cr, uid, [field_id],['ttype'])
                 if type[0]['ttype'] == 'date':
                     date_idx = idx
                     fct[idx] = process_date[report['frequency']] 
@@ -386,7 +384,7 @@ class report_custom(report_int):
         # plots are usually displayed year by year
         # so we do so if the first field is a date
         data_by_year = {}
-        if date_idx != None:
+        if date_idx is not None:
             for r in results:
                 key = process_date['Y'](r[date_idx])
                 if key not in data_by_year:
@@ -446,21 +444,23 @@ class report_custom(report_int):
 
 
     def _create_bars(self, cr, uid, ids, report, fields, results, context):
-        pool = pooler.get_pool(cr.dbname)
+        pool = openerp.registry(cr.dbname)
         pdf_string = cStringIO.StringIO()
         can = canvas.init(fname=pdf_string, format='pdf')
         
         can.show(80,380,'/16/H'+report['title'])
         
-        process_date = {}
-        process_date['D'] = lambda x : reduce(lambda xx,yy : xx+'-'+yy,x.split('-')[1:3])
-        process_date['M'] = lambda x : x.split('-')[1]
-        process_date['Y'] = lambda x : x.split('-')[0]
+        process_date = {
+            'D': lambda x: reduce(lambda xx, yy: xx + '-' + yy, x.split('-')[1:3]),
+            'M': lambda x: x.split('-')[1],
+            'Y': lambda x: x.split('-')[0]
+        }
 
-        order_date = {}
-        order_date['D'] = lambda x : time.mktime((2005,int(x.split('-')[0]), int(x.split('-')[1]),0,0,0,0,0,0))
-        order_date['M'] = lambda x : x
-        order_date['Y'] = lambda x : x
+        order_date = {
+            'D': lambda x: time.mktime((2005, int(x.split('-')[0]), int(x.split('-')[1]), 0, 0, 0, 0, 0, 0)),
+            'M': lambda x: x,
+            'Y': lambda x: x
+        }
 
         ar = area.T(size=(350,350),
             x_axis = axis.X(label = fields[0]['name'], format="/a-30{}%s"),
@@ -472,7 +472,7 @@ class report_custom(report_int):
         for f in fields:
             field_id = (f['field_child3'] and f['field_child3'][0]) or (f['field_child2'] and f['field_child2'][0]) or (f['field_child1'] and f['field_child1'][0]) or (f['field_child0'] and f['field_child0'][0])
             if field_id:
-                type = pool.get('ir.model.fields').read(cr, uid, [field_id],['ttype'])
+                type = pool['ir.model.fields'].read(cr, uid, [field_id],['ttype'])
                 if type[0]['ttype'] == 'date':
                     date_idx = idx
                     fct[idx] = process_date[report['frequency']] 
@@ -485,7 +485,7 @@ class report_custom(report_int):
         # plot are usually displayed year by year
         # so we do so if the first field is a date
         data_by_year = {}
-        if date_idx != None:
+        if date_idx is not None:
             for r in results:
                 key = process_date['Y'](r[date_idx])
                 if key not in data_by_year:
@@ -607,7 +607,7 @@ class report_custom(report_int):
             node_line = etree.SubElement(lines, 'row')
             for f in range(len(fields)):
                 col = etree.SubElement(node_line, 'col', tree='no')
-                if line[f] != None:
+                if line[f] is not None:
                     col.text = line[f] or ''
                 else:
                     col.text = '/'