web/box.py
branchtls-sprint
changeset 1149 1e19b6ef53a1
parent 838 f2c56312b03a
child 1181 620ec8e6ae19
equal deleted inserted replaced
1148:55a8238f8f7c 1149:1e19b6ef53a1
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from logilab.common.decorators import cached
       
    10 from logilab.mtconverter import html_escape
     9 from logilab.mtconverter import html_escape
    11 
    10 
    12 from cubicweb import Unauthorized, role as get_role
    11 from cubicweb import Unauthorized, role as get_role, target as get_target
    13 from cubicweb.selectors import (one_line_rset,  primary_view,
    12 from cubicweb.selectors import (one_line_rset,  primary_view,
    14                                 match_context_prop, partial_has_related_entities,
    13                                 match_context_prop, partial_has_related_entities,
    15                                 accepts_compat, has_relation_compat,
    14                                 accepts_compat, has_relation_compat,
    16                                 condition_compat, require_group_compat)
    15                                 condition_compat, require_group_compat)
    17 from cubicweb.view import View, ReloadableMixIn
    16 from cubicweb.view import View, ReloadableMixIn
   177         self.w_unrelated(box, entity)
   176         self.w_unrelated(box, entity)
   178         box.render(self.w)
   177         box.render(self.w)
   179 
   178 
   180     def div_id(self):
   179     def div_id(self):
   181         return self.id
   180         return self.id
   182 
       
   183     @cached
       
   184     def xtarget(self):
       
   185         if self.target == 'subject':
       
   186             return 'object', 'subject'
       
   187         return 'subject', 'object'
       
   188         
   181         
   189     def box_item(self, entity, etarget, rql, label):
   182     def box_item(self, entity, etarget, rql, label):
   190         """builds HTML link to edit relation between `entity` and `etarget`
   183         """builds HTML link to edit relation between `entity` and `etarget`
   191         """
   184         """
   192         x, target = self.xtarget()
   185         role, target = get_role(self), get_target(self)
   193         args = {x[0] : entity.eid, target[0] : etarget.eid}
   186         args = {role[0] : entity.eid, target[0] : etarget.eid}
   194         url = self.user_rql_callback((rql, args))
   187         url = self.user_rql_callback((rql, args))
   195         # for each target, provide a link to edit the relation
   188         # for each target, provide a link to edit the relation
   196         label = u'[<a href="%s">%s</a>] %s' % (url, label,
   189         label = u'[<a href="%s">%s</a>] %s' % (url, label,
   197                                                etarget.view('incontext'))
   190                                                etarget.view('incontext'))
   198         return RawBoxItem(label, liclass=u'invisible')
   191         return RawBoxItem(label, liclass=u'invisible')
   215         """returns the list of unrelated entities
   208         """returns the list of unrelated entities
   216 
   209 
   217         if etype is not defined on the Box's class, the default
   210         if etype is not defined on the Box's class, the default
   218         behaviour is to use the entity's appropraite vocabulary function
   211         behaviour is to use the entity's appropraite vocabulary function
   219         """
   212         """
   220         x, target = self.xtarget()
       
   221         # use entity.unrelated if we've been asked for a particular etype
   213         # use entity.unrelated if we've been asked for a particular etype
   222         if hasattr(self, 'etype'):
   214         if hasattr(self, 'etype'):
   223             return entity.unrelated(self.rtype, self.etype, x).entities()
   215             return entity.unrelated(self.rtype, self.etype, get_role(self)).entities()
   224         # in other cases, use vocabulary functions
   216         # in other cases, use vocabulary functions
   225         entities = []
   217         entities = []
   226         for _, eid in entity.vocabulary(self.rtype, x):
   218         for _, eid in entity.vocabulary(self.rtype, x):
   227             if eid is not None:
   219             if eid is not None:
   228                 rset = self.req.eid_rset(eid)
   220                 rset = self.req.eid_rset(eid)
   229                 entities.append(rset.get_entity(0, 0))
   221                 entities.append(rset.get_entity(0, 0))
   230         return entities
   222         return entities
   231         
   223         
   232     def related_entities(self, entity):
   224     def related_entities(self, entity):
   233         x, target = self.xtarget()
   225         return entity.related(self.rtype, get_role(self), entities=True)
   234         return entity.related(self.rtype, x, entities=True)
   226 
   235