web/views/primary.py
changeset 3720 5376aaadd16b
parent 3451 6b46d73823f5
parent 3705 5cd35123866b
child 3725 55e57070a969
equal deleted inserted replaced
3678:29f74716fd70 3720:5376aaadd16b
   103         return u''
   103         return u''
   104 
   104 
   105     def render_entity_attributes(self, entity, siderelations=None):
   105     def render_entity_attributes(self, entity, siderelations=None):
   106         for rschema, tschemas, role, dispctrl in self._section_def(entity, 'attributes'):
   106         for rschema, tschemas, role, dispctrl in self._section_def(entity, 'attributes'):
   107             vid = dispctrl.get('vid', 'reledit')
   107             vid = dispctrl.get('vid', 'reledit')
   108             if rschema.is_final() or vid == 'reledit':
   108             if rschema.final or vid == 'reledit':
   109                 value = entity.view(vid, rtype=rschema.type, role=role)
   109                 value = entity.view(vid, rtype=rschema.type, role=role)
   110             else:
   110             else:
   111                 rset = self._relation_rset(entity, rschema, role, dispctrl)
   111                 rset = self._relation_rset(entity, rschema, role, dispctrl)
   112                 if rset:
   112                 if rset:
   113                     value = self.view(vid, rset)
   113                     value = self.view(vid, rset)
   174                 rdefs.append( (rschema, matchtschemas, role, dispctrl) )
   174                 rdefs.append( (rschema, matchtschemas, role, dispctrl) )
   175         return sorted(rdefs, key=lambda x: x[-1]['order'])
   175         return sorted(rdefs, key=lambda x: x[-1]['order'])
   176 
   176 
   177     def _relation_rset(self, entity, rschema, role, dispctrl):
   177     def _relation_rset(self, entity, rschema, role, dispctrl):
   178         try:
   178         try:
   179             if dispctrl.get('limit'):
   179             dispctrl.setdefault('limit', self.maxrelated)
   180                 rset = entity.related(rschema.type, role,
   180             rset = entity.related(rschema.type, role, limit=dispctrl['limit']+1)
   181                                       limit=self.maxrelated+1)
       
   182             else:
       
   183                 rset = entity.related(rschema.type, role)
       
   184         except Unauthorized:
   181         except Unauthorized:
   185             return
   182             return
   186         if 'filter' in dispctrl:
   183         if 'filter' in dispctrl:
   187             rset = dispctrl['filter'](rset)
   184             rset = dispctrl['filter'](rset)
   188         return rset
   185         return rset
   189 
   186 
   190     def _render_relation(self, rset, dispctrl, defaultvid, showlabel):
   187     def _render_relation(self, rset, dispctrl, defaultvid, showlabel):
   191         self.w(u'<div class="section">')
   188         self.w(u'<div class="section">')
   192         if showlabel:
   189         if showlabel:
   193             self.w(u'<h4>%s</h4>' % self._cw._(dispctrl['label']))
   190             self.w(u'<h4>%s</h4>' % self._cw._(dispctrl['label']))
   194         self.wview(dispctrl.get('vid', defaultvid), rset)
   191                    initargs={'dispctrl': dispctrl})
   195         self.w(u'</div>')
   192         self.w(u'</div>')
   196 
   193 
   197     def _render_attribute(self, rschema, value, role='subject'):
   194     def _render_attribute(self, rschema, value, role='subject'):
   198         if rschema.is_final():
   195         if rschema.final:
   199             show_label = self.show_attr_label
   196             show_label = self.show_attr_label
   200         else:
   197         else:
   201             show_label = self.show_rel_label
   198             show_label = self.show_rel_label
   202         label = display_name(self._cw, rschema.type, role)
   199         label = display_name(self._cw, rschema.type, role)
   203         self.field(label, value, show_label=show_label, tr=False)
   200         self.field(label, value, show_label=show_label, tr=False)
   204 
   201 
   205 
   202 
   206 class RelatedView(EntityView):
   203 class RelatedView(EntityView):
   207     __regid__ = 'autolimited'
   204     __regid__ = 'autolimited'
   208     def call(self, title=None, **kwargs):
   205 
   209         # if not too many entities, show them all in a list
   206     def call(self, **kwargs):
   210         maxrelated = self._cw.property_value('navigation.related-limit')
   207         # nb: rset retreived using entity.related with limit + 1 if any
   211         if title:
   208         # because of that, we known that rset.printable_rql() will return
   212             self.w(u'<div class="title"><span>%s</span></div>' % title)
   209         # rql with no limit set anyway (since it's handled manually)
   213         if self.cw_rset.rowcount <= maxrelated:
   210         if 'dispctrl' in self.extra_kwargs:
       
   211             limit = self.extra_kwargs['dispctrl'].get('limit')
       
   212         else:
       
   213             limit = None
       
   214         if limit is None or self.cw_rset.rowcount <= limit:
   214             if self.cw_rset.rowcount == 1:
   215             if self.cw_rset.rowcount == 1:
   215                 self.wview('incontext', self.cw_rset, row=0)
   216                 self.wview('incontext', self.cw_rset, row=0)
   216             elif 1 < self.cw_rset.rowcount <= 5:
   217             elif 1 < self.cw_rset.rowcount <= 5:
   217                 self.wview('csv', self.cw_rset)
   218                 self.wview('csv', self.cw_rset)
   218             else:
   219             else:
   220                 self.wview('simplelist', self.cw_rset)
   221                 self.wview('simplelist', self.cw_rset)
   221                 self.w(u'</div>')
   222                 self.w(u'</div>')
   222         # else show links to display related entities
   223         # else show links to display related entities
   223         else:
   224         else:
   224             rql = self.cw_rset.printable_rql()
   225             rql = self.cw_rset.printable_rql()
   225             self.cw_rset.limit(maxrelated)
   226             self.cw_rset.limit(maxrelated) # remove extra entity
   226             self.w(u'<div>')
   227             self.w(u'<div>')
   227             self.wview('simplelist', self.cw_rset)
   228             self.wview('simplelist', self.cw_rset)
   228             self.w(u'[<a href="%s">%s</a>]' % (self._cw.build_url(rql=rql),
   229             self.w(u'[<a href="%s">%s</a>]' % (self._cw.build_url(rql=rql),
   229                                                self._cw._('see them all')))
   230                                                self._cw._('see them all')))
   230             self.w(u'</div>')
   231             self.w(u'</div>')