예제 #1
0
 def convertAttributes(result, relnode, prevRelation, omit):
     attributes = dict(relnode.getAttr())
     attributes['fieldpath'] = gnrstring.concat(prevRelation, relnode.label)
     if 'joiner' in attributes:
         joiner = attributes.pop('joiner')
         attributes.update(joiner[0])
         attributes['name_long'] = self.relationName(relnode.label)
         if attributes['mode'] == 'M':
             attributes['group'] = attributes.get('many_group') or 'zz'
             attributes['dtype'] = 'RM'
         else:
             attributes['group'] = attributes.get('one_group')
             attributes['dtype'] = 'RO'
     else:
         attributes['name_long'] = attributes.get('name_long') or relnode.label
     resultAppend(result, relnode.label, attributes, omit)
예제 #2
0
 def relationExplorer(self, omit='', prevRelation='', dosort=True, pyresolver=False, **kwargs):
     """add???
     
     :param omit: add???. Default value is ``''``
     :param prevRelation: add???. Default value is ``''``
     :param dosort: boolean. add???. Default value is ``True``
     :param pyresolver: boolean. add???. Default value is ``False``
     """
     def xvalue(attributes):
         if not pyresolver:
             return
         if attributes.get('one_relation'):
             if attributes['mode'] == 'O':
                 relpkg, reltbl, relfld = attributes['one_relation'].split('.')
             else:
                 relpkg, reltbl, relfld = attributes['many_relation'].split('.')
             targettbl = self.db.table('%s.%s' % (relpkg, reltbl))
             return BagCbResolver(targettbl.relationExplorer, omit=omit,
                                  prevRelation=attributes['fieldpath'], dosort=dosort,
                                  pyresolver=pyresolver, **kwargs)
                                  
     def resultAppend(result, label, attributes, omit):
         gr = attributes.get('group') or ' '
         grin = gr[0]
         if grin == '*' or grin == '_':
             attributes['group'] = gr[1:]
         if grin not in omit:
             result.setItem(label, xvalue(attributes), attributes)
             
     def convertAttributes(result, relnode, prevRelation, omit):
         attributes = dict(relnode.getAttr())
         attributes['fieldpath'] = gnrstring.concat(prevRelation, relnode.label)
         if 'joiner' in attributes:
             joiner = attributes.pop('joiner')
             attributes.update(joiner[0])
             attributes['name_long'] = self.relationName(relnode.label)
             if attributes['mode'] == 'M':
                 attributes['group'] = attributes.get('many_group') or 'zz'
                 attributes['dtype'] = 'RM'
             else:
                 attributes['group'] = attributes.get('one_group')
                 attributes['dtype'] = 'RO'
         else:
             attributes['name_long'] = attributes.get('name_long') or relnode.label
         resultAppend(result, relnode.label, attributes, omit)
         
     tblmodel = self.model
     result = Bag()
     for relnode in tblmodel.relations: # add columns relations
         convertAttributes(result, relnode, prevRelation, omit)
         
     for vcolname, vcol in tblmodel.virtual_columns.items():
         targetcol = self.column(vcolname)
         attributes = dict(targetcol.attributes)
         attributes.update(vcol.attributes)
         attributes['fieldpath'] = gnrstring.concat(prevRelation, vcolname)
         attributes['name_long'] = attributes.get('name_long') or vcolname
         if 'sql_formula' in attributes:
             attributes['dtype'] = attributes.get('dtype') or 'T'
         resultAppend(result, vcolname, attributes, omit)
         
     for aliastbl in tblmodel.table_aliases.values():
         relpath = tblmodel.resolveRelationPath(aliastbl.relation_path)
         attributes = dict(tblmodel.relations.getAttr(relpath))
         attributes['name_long'] = aliastbl.attributes.get('name_long') or self.relationName(relpath)
         attributes['group'] = aliastbl.attributes.get('group')
         attributes['fieldpath'] = gnrstring.concat(prevRelation, aliastbl.name)
         joiner = attributes.pop('joiner')
         attributes.update(joiner[0])
         mode = attributes.get('mode')
         if mode == 'O':
             attributes['dtype'] = 'RO'
         elif mode == 'M':
             attributes['dtype'] = 'RM'
         resultAppend(result, aliastbl.name, attributes, omit)
         
     if dosort:
         result.sort(lambda a, b: cmp(a.getAttr('group', '').split('.'), b.getAttr('group', '').split('.')))
         grdict = dict([(k[6:], v) for k, v in self.attributes.items() if k.startswith('group_')])
         if not grdict:
             return result
         newresult = Bag()
         for node in result:
             grk = (node.getAttr('group') or '').split('.')[0]
             if grk and grdict.get(grk):
                 if not grk in newresult:
                     newresult.setItem(grk, None, name_long=grdict.get(grk))
                 newresult.setItem('%s.%s' % (grk, node.label), node.getValue(), node.getAttr())
             else:
                 newresult.setItem(node.label, node.getValue(), node.getAttr())
         return newresult
     else:
         return result