如何在odoo中修复python的环境

时间:2017-05-24 17:35:50

标签: python openerp

我从2016年开始为我的公司开发代码。但是现在我不明白为什么会出现这个错误。 让我解释。 管理员用户可以创建对象。在技​​术用户也可以做到这一点之前,现在它无法创建“line.troncon”#39;宾语。我的意思是,技术用户可以创建除了' line.troncon'之外的所有对象。模型。 这里是代码

df$orientation1 <- factor(df$orientation, levels=c("west","north","east"))
summary(lme(x~orientation1, random=~1|site, data=df))
# ... again just looking at fixed effects ...
#                       Value Std.Error DF   t-value p-value
# (Intercept)       0.2449823 0.4729373 13 0.5180016  0.6132
# orientation1north 1.0150350 0.6688344 13 1.5176178  0.1530
# orientation1east  1.6855218 0.6688344 13 2.5200884  0.0256

所有的overriden方法都部署在我项目的每个模型上。

因此,当我的技术用户想要在line.troncon模型上添加插入时,这里出现错误

class gc_fibertroncon(models.Model):
"""Classe qui modelise un troncon"""
_name = 'line.troncon'

def getSelectionTypeofTroncon(self):
    """ """
    return TYPEDETRONCONS  

@api.one
@api.depends('node_origin_id','node_destination_id')
def compute_name(self):
    if self.node_origin_id and self.node_destination_id and self.origine and self.destination:
        self.name = str(self.node_origin_id.zone_id.numero_zone) + 'T' + NUM_NOEUD_DICT[self.origine] + NUM_NOEUD_DICT[self.destination] + '-' + str(self.id)

@api.one
def create_default_fourreau(self):
    """"""
    if not self.fourreau_ids:
        self.env['line.fourreau'].create({'name':self.name.replace('T','F')})

@api.one
def delete_default_fourreau(self):
    """"""
    if not self.fourreau_ids:
        fourreau = self.env['line.fourreau'].search({'name':self.name.replace('T','F')})
        fourreau.unlink()


@api.one
@api.depends('origine','destination','liaison_ids','fourreau_ids')  
def _compute_booleans(self):
    """"""
    self.b_liaison_ids = True if len(self.liaison_ids)>0 else False
    self.b_fourreau_ids = True if len(self.fourreau_ids)>0 else False

    self.b_node_destination_id = True if len(self.node_destination_id)>0 else False
    self.b_node_origin_id = True if len(self.node_origin_id)>0 else False

@api.one
def get_fibres_name(self):
    """"""
    res=[]
    if self.fourreau_ids:
        for fourreau in self.fourreau_ids:
            if fourreau.cable_ids:
                for cable in fourreau.cable_ids:
                    if cable.tube_ids:
                        for tube in cable.tube_ids:
                            res.extend(tube.get_fibres_name())
    return res

@api.multi
@api.onchange('origine')
def get_domain_origine(self):
    """"""
    return {
        'domain':{'node_origin_id':[('type','=',self.origine)]}
    }

@api.multi
@api.onchange('destination')
def get_domain_destination(self):
    """"""
    return {
        'domain':{'node_destination_id':[('type','=',self.destination)]}
    }
# @api.one
# @api.depends('node_origin_id')
# def _compute_parent_id(self):
#     """"""
#     if self.node_origin_id:
#         rec = self.env['line.troncon'].search([('node_destination_id.id','=',self.node_origin_id.id)])
#         if rec:
#             _logger.debug('\n' * 3 + str(rec))
#             rec.ensure_one()
#             self.parent_id = rec    
#         else:
#             self.parent_id = False
#     # TODO : Rajouter un controle sur le type!!
#     else:
#         self.parent_id = False


@api.one
@api.depends('name','type','liaison_ids','fourreau_ids','node_origin_id','node_destination_id','geolocate')#,'parent_id','child_ids')
def compute_geojson(self):
    def set_geo_linestring(gs):
        geo_coord_pool = self.env['geo.coord']         
        geo_coord_search = geo_coord_pool.search([('model_id','=',self.id),('model_type','=',self.type),('model_geolocate_json','=',gs)],limit=1)
        if geo_coord_search:
            self.geo_id = geo_coord_search.id                
        else:
            geo_coord = geo_coord_pool.create({'model_geolocate_json':gs})
            self.geo_id = geo_coord.id

    if self.geolocate:
        gs = geojson.loads(self.geolocate)
        gs['type'] = self.name
        for feature in gs['features']:
            feature['id'] = self.id or False
            feature['properties'] = {'NodeType':self.type,'Model':self._name, 'display_name':self.name}
        self.geolocate_json = geojson.dumps(gs)
        set_geo_linestring(geojson.dumps(gs))
    elif self.node_origin_id and self.node_destination_id:
        gs = FeatureCollection([
            Feature(
                geometry=LineString((
                    [self.node_origin_id.longitude,self.node_origin_id.latitude],
                    [self.node_destination_id.longitude,self.node_destination_id.latitude]
                ))
            )
        ])
        gs['type'] = self.name
        for feature in gs['features']:
            feature['id'] = self.id or False
            feature['properties'] = {'NodeType':self.type,'Model':self._name}
        self.geolocate_json = geojson.dumps(gs)
        set_geo_linestring(geojson.dumps(gs))
    else:
        self.geolocate_json = ''
        self.geo_id = False

def populate_features(self, gs):
    gs['type'] = self.name
    for feature in gs['features']:
        feature['id'] = self.id or False
        feature['properties'] = {'NodeType':self.type,'Model':self._name}
    self.geolocate_json = geojson.dumps(gs)

@api.onchange('geolocate_json')
def onchange_geolocate_json(self):
    self.inverse_geojson()

@api.depends('name')
def inverse_geojson(self):
    self.geolocate = self.geolocate_json if self.geolocate_json else ''

def countEqpts(self):
    return{
        "Troncon"               : self.countInside(),
        "Noeud d'origine"       : self.node_origin_id.countInside() if self.node_origin_id else "Aucun",
        "Noeud de destination"  : self.node_destination_id.countInside() if self.node_destination_id else "Aucun",
    }

# Return a dict 
def countInside(self):
    my_point = {
        0 : self.id,
        1 : self.name,
        2 : [ 'line.fourreau', len(self.fourreau_ids), 'fourreaux' ],
        3 : [ 'line.cable', 0, 'cables' ],
        4 : [ 'line.tube', 0, 'tubes' ],
        5 : [ 'line.fibre', 0, 'fibres' ],
        6 : [ 'link.link', 0, 'liaisons']
    }

    for fourreau_id in self.fourreau_ids:
        my_point[3][1] += len(fourreau_id.cable_ids)
        for cable_id in fourreau_id.cable_ids:
            my_point[4][1] += len(cable_id.tube_ids)
            for tube_id in cable_id.tube_ids:
                my_point[5][1] += len(tube_id.fo_ids)
    return my_point

def getDetails(self):
    return{
        "name" : self.display_name,
        "properties" : {
            "diametre" : self.diametre,
            "longueur" : self.longueur,
            "origine" : self.origine,
            "destination" : self.destination
        }
    }

def getList(self, name_model ):
    if( name_model == 'line.fibre' ):
        return self.getFibres()
    elif( name_model == 'line.cable' ):
        return self.getCables()
    elif( name_model == 'line.tube' ):
        return self.getTubes()
    elif( name_model == 'line.fourreau' ):
        return self.getFourreau()

def getFibres(self):
    fibres = {}
    fibre_ids = self.env['line.fibre']

    for fourreau in self.fourreau_ids:
        for cable in fourreau.cable_ids:
            for tube in cable.tube_ids:
                fibre_ids = fibre_ids | tube.fo_ids

    for fibre in fibre_ids:
        fibres[ fibre.id ] = [ fibre.numero_fibre, 'line.fibre', fibre.id, fibre.display_name ]
    return sorted( fibres.values() )
    # self.env.cr.execute( "SELECT id, display_name FROM public.line_fibre WHERE id IN %s LIMIT %s OFFSET %s ", (tuple([x.id for x in fibre_ids]), 12, 0) )
    # _logger.error( self.env.cr.fetchall() )

def getCables(self):
    cables = {}
    cable_ids = self.env['line.cable']

    for fourreau in self.fourreau_ids:
        cable_ids = cable_ids | fourreau.cable_ids

    for cable in cable_ids:
        cables[ cable.id ] = [ cable.numero_cable, 'line.cable', cable.id, cable.display_name ]
    return sorted( cables.values() )
    # self.env.cr.execute( "SELECT id, display_name FROM public.line_cable WHERE id IN %s LIMIT %s OFFSET %s ", (tuple([x.id for x in cable_ids]), limit, offset) )
    # return self.env.cr.fetchall()


def getTubes(self):
    tubes = {}
    tube_ids = self.env['line.tube']

    for fourreau in self.fourreau_ids:
        for cable in fourreau.cable_ids:
            tube_ids = tube_ids | cable.tube_ids

    for tube in tube_ids:
        tubes[ tube.id ] = [ tube.numero_tube, 'line.tube', tube.id, tube.display_name ]
    return sorted( tubes.values() )
    # self.env.cr.execute( "SELECT id, display_name FROM public.line_tube WHERE id IN %s LIMIT %s OFFSET %s ", (tuple([x.id for x in tube_ids]), limit, offset) )
    # return self.env.cr.fetchall()


def getFourreau(self):        
    fourreaux = {}
    # fourreau_ids = self.env['line.fourreau']
    # fourreau_ids = fourreau_ids | self.fourreau_ids

    for fourreau in self.fourreau_ids:
        fourreaux[ fourreau.id ] = [ '', 'line.fourreau', fourreau.id, fourreau.display_name ]
    return sorted( fourreaux.values() )

    # self.env.cr.execute( "SELECT id, name FROM public.line_fourreau WHERE id IN %s LIMIT %s OFFSET %s ", (tuple([x.id for x in fourreau_ids]), limit, offset) )
    # return self.env.cr.fetchall()

def getInfos(self):
    return json.dumps({
        "Nom":str(self.name),
        "Origine":str(self.node_origin_id.name),
        "Destination":str(self.node_destination_id.name),
    })


def create_rel(self,origine=None,destination=None):
    rel = self.env['node.rel']
    rid = rel.create({'nO':origine,'nD':destination})
    return rid or False

def check_rel(self,origine=None,destination=None,rid=None):
    rel = self.env['node.rel']
    rel_search = rel.search([('nO','=',origine),('nD','=',destination)])
    if rel_search:
        troncon = self.search([('rel_id','=',rel_search.id)])
        if self._origin:
            if troncon and troncon.id != self._origin.id:
                raise ValidationError('Le troncon {} possede déjà la meme origine et la meme destination. Veuillez modifier'.format(troncon.name or 'ErrorDB'))
        else:
            raise ValidationError('Le troncon {} possede déjà la meme origine et la meme destination. Veuillez modifier'.format(troncon.name or 'ErrorDB'))
        return True
    else:
        return True

@api.onchange('node_origin_id','node_destination_id')
def onchange_origin_dest(self):
    if self.node_origin_id and self.node_destination_id:
        self.check_rel(self.node_origin_id.id,self.node_destination_id.id)

name = fields.Char(default="Troncon")
type = fields.Selection(selection=getSelectionTypeofTroncon)
diametre= fields.Float(string='Diametre', 
                                           digits=(5, 2))
description=fields.Text()
geolocate_json = fields.Text(string="Geo JSON",             compute=compute_geojson,inverse=inverse_geojson,store=True)
geolocate = fields.Text()
longueur=fields.Float(string='Longueur')

b_liaison_ids = fields.Boolean(compute=_compute_booleans, 
                                                       store=True)
b_fourreau_ids = fields.Boolean(compute=_compute_booleans, 
                                                            store=True)

b_node_destination_id = fields.Boolean(compute=_compute_booleans,
                                                                                 store=True)
b_node_origin_id = fields.Boolean(compute=_compute_booleans,
                                                                                  store=True)
origine = fields.Selection(string="Origine", selection=[('armoire','Armoire'),('chambre','Chambre'),('shelter','Local Technique')])
destination = fields.Selection(string="Destination", selection=    [('armoire','Armoire'),('chambre','Chambre'),('abonne','Abonné'),('boitier','PBO/PTO')])


node_origin_id = fields.Many2one('node.node', string="Noeud - Origine")
node_destination_id = fields.Many2one('node.node', string="Noeud - Destination")

liaison_ids= fields.Many2many(comodel_name='link.link',
                                                          column1='troncon_id',
                                                      column2='link_id',
                                                      string='Liaisons')
fourreau_ids = fields.Many2many(comodel_name='line.fourreau',
                                                            column1='troncon_id',
                                                            column2='fourreau_id',
                                                            string="Fourreau")
rel_id = fields.Many2one('node.rel',string='Relation')
geo_id = fields.Many2one('geo.coord',
    'Geometry',
    compute=compute_geojson,
    ondelete='restrict',
    store=True
)
geo_linestring = geo_fields.GeoLine(readonly=True, store=True, related='geo_id.geo_linestring')
@api.multi
def write(self,vals):
    if 'node_origin_id' in vals and vals.get('node_origin_id') and 'node_destination_id' in vals and vals.get('node_destination_id'):
            if vals.get('node_origin_id') and vals.get('node_destination_id'):
                    self.rel_id.unlink()
                    vals['rel_id'] = self.create_rel(vals.get('node_origin_id'),vals.get('node_destination_id')).id or False
            else:
                    vals['rel_id'] = False

    elif 'node_origin_id' in vals and vals.get('node_origin_id') and self.node_destination_id:
            if vals.get('node_origin_id'):
                    self.rel_id.unlink()
                    vals['rel_id'] = self.create_rel(vals.get('node_origin_id'),self.node_destination_id.id).id or False
            else:
                    vals['rel_id'] = False

    elif 'node_destination_id' in vals and vals.get('node_destination_id') and self.node_origin_id:
            if vals.get('node_destination_id'):
                    self.rel_id.unlink()
                    vals['rel_id'] = self.create_rel(self.node_origin_id.id,vals.get('node_destination_id')).id or False
            else:
                    vals['rel_id'] = False

    # type your code here
    today = datetime.today()
    for rec in self:
            log_pool = self.env['res.log'].create({'date':today,'action':'write','model_name':self._name,'model_id':rec.id,'values':vals})

    return super(gc_fibertroncon,self).write(vals) 

@api.model
def create(self,vals):
    # if 'node_origin_id' in vals and vals.get('node_origin_id') and 'node_destination_id' in vals and vals.get('node_destination_id'):
    #         if values.get('node_origin_id') and values.get('node_destination_id'):
    #                 values.['rel_id'] = self.create_rel(values.get('node_origin_id'),values.get('node_destination_id'))
    #         else:
    #                 values['rel_id'] = False

    res = super(gc_fibertroncon,self).create(vals)
    if res.node_origin_id and res.node_destination_id:
            res.rel_id = self.create_rel(res.node_origin_id,res.node_destination_id)

    # type your code here
    today = datetime.today()
    log_pool = self.env['res.log'].create({'date':today,'action':'create','model_name':self._name,'model_id':res.id,'values':vals})
    return res 

@api.one
def copy(self, default=None):
    res=super(gc_fibertroncon, self).copy(default)
    # type your code here
    today = datetime.today()
    log_pool = self.env['res.log'].create({'date':today,'action':'copy','model_name':self._name,'model_id':self.id,'values':''})
    return res 

@api.multi
def unlink(self):
    # type your code here
    today = datetime.today()
    log_pool = self.env['res.log'].create({'date':today,'action':'unlink','model_name':self._name,'model_id':self.id,'values':''})
    return models.Model.unlink(self)

似乎&#39; env&#39;变量是不同的。

我该如何解决?

我很感谢你们的任何建议。

0 个答案:

没有答案