<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution	
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import os
import time
import netsvc

import tools
import print_xml
import render
from interface import report_int
import common
from osv.osv import except_osv
from osv.orm import browse_null
from osv.orm import browse_record_list
import pooler
from xml.dom import minidom
import libxml2
import libxslt
from pychart import *
import misc
import cStringIO

class external_pdf(render.render):
    def __init__(self, pdf):
        render.render.__init__(self)
        self.pdf = pdf
        self.output_type='pdf'
    def _render(self):
        return self.pdf

theme.use_color = 1


#TODO: devrait heriter de report_rml a la place de report_int 
# -> pourrait overrider que create_xml a la place de tout create
# heuu, ca marche pas ds tous les cas car graphs sont generes en pdf directment
# par pychart, et on passe donc pas par du rml
class report_custom(report_int):
    def __init__(self, name):
        report_int.__init__(self, name)
    #
    # PRE:
    #    fields = [['address','city'],['name'], ['zip']]
    #    conditions = [[('zip','==','3'),(,)],(,),(,)] #same structure as fields
    #    row_canvas = ['Rue', None, None]
    # POST:
    #    [ ['ville','name','zip'] ]
    #
    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:
                if cond and cond[0]:
                    c = cond[0]
                    temp = c[0](eval('obj.'+c[1]))
                    if not eval('\''+temp+'\''+' '+c[2]+' '+'\''+str(c[3])+'\''):
                        tobreak = True
            if tobreak:
                break
            levels = {}
            row = []
            for i in range(len(fields)):
                if not fields[i]:
                    row.append(row_canvas and row_canvas[i])
                    if row_canvas[i]:
                        row_canvas[i]=False
                elif len(fields[i])==1:
                    if not isinstance(obj, browse_null):
                        row.append(str(eval('obj.'+fields[i][0])))
                    else:
                        row.append(None)
                else:
                    row.append(None)
                    levels[fields[i][0]]=True
            if not levels:
                result.append(row)
            else:
                # Process group_by data first
                key = []
                if group_by != None and fields[group_by] != None:
                    if fields[group_by][0] in levels.keys():
                        key.append(fields[group_by][0])
                    for l in levels.keys():
                        if l != fields[group_by][0]:
                            key.append(l)
                else:
                    key = levels.keys()
                for l in key:
                    objs = eval('obj.'+l)
                    if not isinstance(objs, browse_record_list) and type(objs) <> type([]):
                        objs = [objs]
                    field_new = []
                    cond_new = []
                    for f in range(len(fields)):
                        if (fields[f] and fields[f][0])==l:
                            field_new.append(fields[f][1:])
                            cond_new.append(conditions[f][1:])
                        else:
                            field_new.append(None)
                            cond_new.append(None)
                    if len(objs):
                        result += self._row_get(cr, uid, objs, field_new, cond_new, row, group_by)
                    else:
                        result.append(row)
        return result 


    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]
        datas['model'] = report.model_id.model
        if report.menu_id:
            ids = self.pool.get(report.model_id.model).search(cr, uid, [])
            datas['ids'] = ids

        service = netsvc.LocalService("object_proxy")
        report_id = datas['report_id']
        report = service.execute(cr.dbname, uid, 'ir.report.custom', 'read', [report_id], context=context)[0]
        fields = service.execute(cr.dbname, uid, 'ir.report.custom.fields', 'read', report['fields_child0'], context=context)

        fields.sort(lambda x,y : x['sequence'] - y['sequence'])

        if report['field_parent']:
            parent_field = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [report['field_parent'][0]],['model'])
        model_name = service.execute(cr.dbname, uid, 'ir.model', 'read', [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(',')
        new_fields = []
        new_cond = []
        for f in fields:
            row = []
            cond = []
            for i in range(4):
                if f['field_child'+str(i)]:
                    row.append(f['field_child'+str(i)][1])
                    if f['fc'+str(i)+'_operande']:
                        fct_name = 'id'
                        cond_op =  f['fc'+str(i)+'_op']
                        if len(f['fc'+str(i)+'_op'].split(',')) == 2:
                            cond_op =  f['fc'+str(i)+'_op'].split(',')[1]
                            fct_name = f['fc'+str(i)+'_op'].split(',')[0]
                        cond.append((fct[fct_name], f['fc'+str(i)+'_operande'][1], cond_op, f['fc'+str(i)+'_condition']))
                    else:
                        cond.append(None)
            new_fields.append(row)
            new_cond.append(cond)
        objs = self.pool.get(model_name).browse(cr, uid, ids)

        # Group by
        groupby = None
        idx = 0
        for f in fields:
            if f['groupby']:
                groupby = idx
            idx += 1
<commit_msg>[FIX] Use the field name instead of the field label<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution	
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import os
import time
import netsvc

import tools
import print_xml
import render
from interface import report_int
import common
from osv.osv import except_osv
from osv.orm import browse_null
from osv.orm import browse_record_list
import pooler
from xml.dom import minidom
import libxml2
import libxslt
from pychart import *
import misc
import cStringIO

class external_pdf(render.render):
    def __init__(self, pdf):
        render.render.__init__(self)
        self.pdf = pdf
        self.output_type='pdf'
    def _render(self):
        return self.pdf

theme.use_color = 1


#TODO: devrait heriter de report_rml a la place de report_int 
# -> pourrait overrider que create_xml a la place de tout create
# heuu, ca marche pas ds tous les cas car graphs sont generes en pdf directment
# par pychart, et on passe donc pas par du rml
class report_custom(report_int):
    def __init__(self, name):
        report_int.__init__(self, name)
    #
    # PRE:
    #    fields = [['address','city'],['name'], ['zip']]
    #    conditions = [[('zip','==','3'),(,)],(,),(,)] #same structure as fields
    #    row_canvas = ['Rue', None, None]
    # POST:
    #    [ ['ville','name','zip'] ]
    #
    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:
                if cond and cond[0]:
                    c = cond[0]
                    temp = c[0](eval('obj.'+c[1]))
                    if not eval('\''+temp+'\''+' '+c[2]+' '+'\''+str(c[3])+'\''):
                        tobreak = True
            if tobreak:
                break
            levels = {}
            row = []
            for i in range(len(fields)):
                if not fields[i]:
                    row.append(row_canvas and row_canvas[i])
                    if row_canvas[i]:
                        row_canvas[i]=False
                elif len(fields[i])==1:
                    if not isinstance(obj, browse_null):
                        row.append(str(eval('obj.'+fields[i][0])))
                    else:
                        row.append(None)
                else:
                    row.append(None)
                    levels[fields[i][0]]=True
            if not levels:
                result.append(row)
            else:
                # Process group_by data first
                key = []
                if group_by != None and fields[group_by] != None:
                    if fields[group_by][0] in levels.keys():
                        key.append(fields[group_by][0])
                    for l in levels.keys():
                        if l != fields[group_by][0]:
                            key.append(l)
                else:
                    key = levels.keys()
                for l in key:
                    objs = eval('obj.'+l)
                    if not isinstance(objs, browse_record_list) and type(objs) <> type([]):
                        objs = [objs]
                    field_new = []
                    cond_new = []
                    for f in range(len(fields)):
                        if (fields[f] and fields[f][0])==l:
                            field_new.append(fields[f][1:])
                            cond_new.append(conditions[f][1:])
                        else:
                            field_new.append(None)
                            cond_new.append(None)
                    if len(objs):
                        result += self._row_get(cr, uid, objs, field_new, cond_new, row, group_by)
                    else:
                        result.append(row)
        return result 


    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]
        datas['model'] = report.model_id.model
        if report.menu_id:
            ids = self.pool.get(report.model_id.model).search(cr, uid, [])
            datas['ids'] = ids

        service = netsvc.LocalService("object_proxy")
        report_id = datas['report_id']
        report = service.execute(cr.dbname, uid, 'ir.report.custom', 'read', [report_id], context=context)[0]
        fields = service.execute(cr.dbname, uid, 'ir.report.custom.fields', 'read', report['fields_child0'], context=context)

        fields.sort(lambda x,y : x['sequence'] - y['sequence'])

        if report['field_parent']:
            parent_field = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [report['field_parent'][0]],['model'])
        model_name = service.execute(cr.dbname, uid, 'ir.model', 'read', [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(',')
        new_fields = []
        new_cond = []
        for f in fields:
            row = []
            cond = []
            for i in range(4):
                field_child = f['field_child'+str(i)]
                if field_child:
                    row.append(
                        service.execute(cr.dbname, uid, 
                                        'ir.model.fields', 'read', [field_child[0]],
                                        ['name'], context=context)[0]['name']
                    )
                    if f['fc'+str(i)+'_operande']:
                        fct_name = 'id'
                        cond_op =  f['fc'+str(i)+'_op']
                        if len(f['fc'+str(i)+'_op'].split(',')) == 2:
                            cond_op =  f['fc'+str(i)+'_op'].split(',')[1]
                            fct_name = f['fc'+str(i)+'_op'].split(',')[0]
                        cond.append((fct[fct_name], f['fc'+str(i)+'_operande'][1], cond_op, f['fc'+str(i)+'_condition']))
                    else:
                        cond.append(None)
            new_fields.append(row)
            new_cond.append(cond)
        objs = self.pool.get(model_name).browse(cr, uid, ids)

        # Group by
        groupby = None
        idx = 0
        for f in fields:
            if f['groupby']:
                groupby = idx
            idx += 1

{'fraction_non_alphanumeric': 0.1483665117569536, 'fraction_numerical': 0.006034542553929389, 'mean_word_length': 1.7081141998497371, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5731573', 'n_tokens_mistral': 4220, 'n_tokens_neox': 4022, 'n_words': 1060}