Example #1
0
 def commandmenubags(cls):
     basecommands = Bag()
     dfcommands = Bag()
     for m in [getattr(cls,c) for c in dir(cls) if c.startswith('cmd_')]:
         b = basecommands if getattr(m,'basecmd',None) else dfcommands
         b.setItem('r_%s' %m.order,None,default_kw=dict(command=m.__name__[4:],dfname='=#ANCHOR.dataframes.selectedDataframe'),caption=m.name)
     basecommands.sort('#k')
     dfcommands.sort('#k')
     return basecommands,dfcommands
Example #2
0
 def commandmenubags(cls):
     basecommands = Bag()
     dfcommands = Bag()
     for m in [getattr(cls, c) for c in dir(cls) if c.startswith('cmd_')]:
         b = basecommands if getattr(m, 'basecmd', None) else dfcommands
         b.setItem('r_%s' % m.order,
                   None,
                   default_kw=dict(
                       command=m.__name__[4:],
                       dfname='=#ANCHOR.dataframes.selectedDataframe'),
                   caption=m.name)
     basecommands.sort('#k')
     dfcommands.sort('#k')
     return basecommands, dfcommands
Example #3
0
    def loadLocalizationFile(self,
                             path=None,
                             currentLocalizationBlock=None,
                             enabledLanguages=None,
                             **kwargs):
        localizationpath = os.path.join(path, 'localization.xml')
        localization = Bag(localizationpath) if os.path.exists(
            localizationpath) else Bag()
        localization.setBackRef()
        enabledLanguages = enabledLanguages.split(
            ',') if enabledLanguages else []
        griddata = Bag()
        treedata = Bag()
        treedata.setItem('root', Bag(), caption=currentLocalizationBlock)
        treeroot = treedata['root']

        def cb(n):
            if n.attr.get('path'):
                treeroot.setItem(n.fullpath or n.label,
                                 None,
                                 path=n.attr['path'],
                                 ext=n.attr['ext'],
                                 caption=n.label)
            elif not n.value:
                row = griddata[n.label]
                parentNode = n.parentNode
                parentAttr = parentNode.attr
                module = parentAttr.get('path')
                if not row:
                    row = Bag()
                    griddata[n.label] = row
                    row['base'] = n.attr.get('base')
                    row['ext'] = parentAttr.get('ext')
                    row['_lockey'] = n.label
                    row['path'] = []
                    row['_pkey'] = n.label
                row['path'].append(module)

                #row['_treepath'] = '%s.%s' %((parentNode.fullpath or parentNode.label),parentAttr.get('ext'))

                for lang in enabledLanguages:
                    row[lang] = row[lang] or n.attr.get(lang)

        localization.walk(cb)
        griddata.sort('base')
        return Bag(dict(content=Bag(griddata=griddata), treedata=treedata))
Example #4
0
 def test_sort(self):
     b = Bag({'d': 1, 'z': 2, 'r': 3, 'a': 4})
     b.sort()
     assert b['#0'] == 4
     b.sort('#k:d')
     assert b['#0'] == 2
     b.sort('#v:a')
     assert b['#0'] == 1
     b.sort('#v:d')
     assert b['#0'] == 4
Example #5
0
 def test_sort(self):
     b = Bag({'d': 1, 'z': 2, 'r': 3, 'a': 4})
     b.sort()
     assert b['#0'] == 4
     b.sort('#k:d')
     assert b['#0'] == 2
     b.sort('#v:a')
     assert b['#0'] == 1
     b.sort('#v:d')
     assert b['#0'] == 4
Example #6
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
Example #7
0
    def load(self):
        if self.rootpath and self.rootpath.startswith("*related*"):
            return self.loadRelated(self.rootpath.split(":")[1])

        db = self._page.db
        tblobj = db.table(self.table)
        columns = "$code,$parent_code,$description,$child_code,$child_count,$rec_type"
        if self.extra_columns:
            columns = "%s,%s" % (columns, self.extra_columns)

        rows = tblobj.query(
            columns, where="COALESCE($parent_code,'')=:rootpath", rootpath=self.rootpath or "", order_by="$child_code"
        ).fetch()
        children = Bag()
        if self.related_table:
            self.setRelatedChildren(children)
        for row in rows:
            child_count = row["child_count"]
            if self.limit_rec_type:
                child_count = child_count if row["rec_type"] != self.limit_rec_type else 0
            if child_count:
                value = HTableResolver(
                    table=self.table,
                    rootpath=row["code"],
                    rootpkey=row["pkey"],
                    relation_path=self.relation_path,
                    related_table=self.related_table,
                    limit_rec_type=self.limit_rec_type,
                    extra_columns=self.extra_columns,
                    child_count=child_count,
                    _page=self._page,
                )
            elif self.related_table:
                value = HTableResolver(
                    table=self.table,
                    rootpath="*related*:%s" % row["pkey"],
                    relation_path=self.relation_path,
                    related_table=self.related_table,
                    _page=self._page,
                )
                child_count = 1

            else:
                value = None
            description = row["description"]
            if description:
                get_tree_row_caption = _getTreeRowCaption
            else:
                get_tree_row_caption = _getTreeRowCaption2
            caption = tblobj.recordCaption(row, rowcaption=get_tree_row_caption(tblobj))
            children.setItem(
                row["child_code"],
                value,
                caption=caption,
                rec_type=row["rec_type"],
                pkey=row["pkey"],
                code=row["code"],
                child_count=child_count,
                checked=False,
                parent_code=row["parent_code"],
                description=row["description"],
                _record=dict(row),
            )  # _attributes=dict(row),
            #

        children.sort("#a.caption")
        return children