web/box.py
changeset 2757 c8e28e1754f0
parent 2710 40789c3044f3
child 2789 39712da6f397
child 3220 11b6016e3970
equal deleted inserted replaced
2754:1db51bb55b45 2757:c8e28e1754f0
   169 
   169 
   170     def cell_call(self, row, col, view=None, **kwargs):
   170     def cell_call(self, row, col, view=None, **kwargs):
   171         self.req.add_js('cubicweb.ajax.js')
   171         self.req.add_js('cubicweb.ajax.js')
   172         entity = self.entity(row, col)
   172         entity = self.entity(row, col)
   173         box = SideBoxWidget(display_name(self.req, self.rtype), self.id)
   173         box = SideBoxWidget(display_name(self.req, self.rtype), self.id)
   174         count = self.w_related(box, entity)
   174         related = self.related_boxitems(entity)
   175         if count:
   175         unrelated = self.unrelated_boxitems(entity)
       
   176         box.extend(related)
       
   177         if related and unrelated:
   176             box.append(BoxSeparator())
   178             box.append(BoxSeparator())
   177         if not self.w_unrelated(box, entity):
   179         box.extend(unrelated)
   178             del box.items[-1] # remove useless separator
       
   179         box.render(self.w)
   180         box.render(self.w)
   180 
   181 
   181     def div_id(self):
   182     def div_id(self):
   182         return self.id
   183         return self.id
   183 
   184 
   190         # for each target, provide a link to edit the relation
   191         # for each target, provide a link to edit the relation
   191         label = u'[<a href="%s">%s</a>] %s' % (url, label,
   192         label = u'[<a href="%s">%s</a>] %s' % (url, label,
   192                                                etarget.view('incontext'))
   193                                                etarget.view('incontext'))
   193         return RawBoxItem(label, liclass=u'invisible')
   194         return RawBoxItem(label, liclass=u'invisible')
   194 
   195 
   195     def w_related(self, box, entity):
   196     def related_boxitems(self, entity):
   196         """appends existing relations to the `box`"""
       
   197         rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype
   197         rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype
   198         related = self.related_entities(entity)
   198         related = []
   199         for etarget in related:
   199         for etarget in self.related_entities(entity):
   200             box.append(self.box_item(entity, etarget, rql, u'-'))
   200             related.append(self.box_item(entity, etarget, rql, u'-'))
   201         return len(related)
   201         return related
   202 
   202 
   203     def w_unrelated(self, box, entity):
   203     def unrelated_boxitems(self, entity):
   204         """appends unrelated entities to the `box`"""
       
   205         rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype
   204         rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype
   206         i = 0
   205         unrelated = []
   207         for etarget in self.unrelated_entities(entity):
   206         for etarget in self.unrelated_entities(entity):
   208             box.append(self.box_item(entity, etarget, rql, u'+'))
   207             unrelated.append(self.box_item(entity, etarget, rql, u'+'))
   209             i += 1
   208         return unrelated
   210         return i
   209 
       
   210     def related_entities(self, entity):
       
   211         return entity.related(self.rtype, get_role(self), entities=True)
   211 
   212 
   212     def unrelated_entities(self, entity):
   213     def unrelated_entities(self, entity):
   213         """returns the list of unrelated entities
   214         """returns the list of unrelated entities
   214 
   215 
   215         if etype is not defined on the Box's class, the default
   216         if etype is not defined on the Box's class, the default
   227             if eid is not None:
   228             if eid is not None:
   228                 rset = self.req.eid_rset(eid)
   229                 rset = self.req.eid_rset(eid)
   229                 entities.append(rset.get_entity(0, 0))
   230                 entities.append(rset.get_entity(0, 0))
   230         return entities
   231         return entities
   231 
   232 
   232     def related_entities(self, entity):
       
   233         return entity.related(self.rtype, get_role(self), entities=True)
       
   234