[Cubicweb] determine if an entity is part of a composite

Aurélien Campeas aurelien.campeas at logilab.fr
Tue Dec 22 15:58:11 CET 2009


Le lundi 21 décembre 2009 à 11:28 +0100, Sylvain Thénault a écrit :
> On 18 décembre 20:27, Aurélien Campeas wrote:
> > Hello,
> > 
> > Maybe I'm just dense or too tired but I do not find a way to determine
> > if a specific entity is part of a composite. One use case is to
> > automatically derive: 
> > 
> > * what to link to a creation time
> > * the parent entity (for breadcrumbs)
> 
> all we've currently is .e_schema.is_subobject which will return True/False.
> 

for the record I'm currently using this:

def composite_rschema(self):
    """yield all composite relations, self.role and composite role"""
    for rschema, role, crole in _composite_rschema(self.e_schema):
        yield rschema, role, crole

def _composite_rschema(eschema):
    for rschema, _types, role in eschema.relation_definitions():
        if rschema.meta or rschema.final:
            continue
        crole = eschema.role_rproperty(role, rschema, 'composite')
        if crole:
            yield rschema, role, crole

@monkeypatch(Entity)
def container(self):
    for rschema, role, crole in composite_rschema(self):
        if role != crole:
            yield rschema, role

@monkeypatch(Entity)
def parent(self):
    for rschema, role in self.container():
        parent = self.related(str(rschema), role)
        if parent:
            return parent.get_entity(0, 0)





More information about the Cubicweb mailing list