web/views/primary.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Wed, 20 May 2009 16:36:02 +0200
branchstable
changeset 1885 c2011d238e98
parent 1752 4b0b912ff5b7
child 1977 606923dff11b
permissions -rw-r--r--
replace sideRelated with sideBox
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
"""The default primary view
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
:organization: Logilab
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
"""
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
     7
__docformat__ = "restructuredtext en"
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
     8
_ = unicode
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
from warnings import warn
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    12
from logilab.mtconverter import html_escape
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    13
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
from cubicweb import Unauthorized
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
from cubicweb.view import EntityView
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    16
from cubicweb.web import uicfg
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
class PrimaryView(EntityView):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
    """the full view of an non final entity"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
    id = 'primary'
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
    title = _('primary')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
    show_attr_label = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
    show_rel_label = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
    skip_none = True
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    27
    rsection = uicfg.primaryview_section
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
    28
    display_ctrl = uicfg.primaryview_display_ctrl
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
    main_related_section = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
    def html_headers(self):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
        """return a list of html headers (eg something to be inserted between
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
        <head> and </head> of the returned page
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
        by default primary views are indexed
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
        """
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
        return []
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
    def cell_call(self, row, col):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
        self.row = row
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
        # XXX move render_entity implementation here
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    42
        self.render_entity(self.complete_entity(row, col))
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    43
        self.maxrelated = self.req.property_value('navigation.related-limit')
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
    def render_entity(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
        """return html to display the given entity"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
        self.render_entity_title(entity)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
        self.render_entity_metadata(entity)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
        # entity's attributes and relations, excluding meta data
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
        # if the entity isn't meta itself
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    51
        boxes = self._prepare_side_boxes(entity)
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    52
        if boxes or hasattr(self, 'render_side_related'):
1558
f63d090eb34a some primary view fixes, need more (eg reledit doesn't work for non final relation
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    53
            self.w(u'<table width="100%"><tr><td style="width: 75%">')
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
        self.w(u'<div>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
        self.w(u'<div class="mainInfo">')
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    56
        try:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    57
            self.render_entity_attributes(entity)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    58
        except TypeError: # XXX bw compat
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    59
            warn('siderelations argument of render_entity_attributes is '
1558
f63d090eb34a some primary view fixes, need more (eg reledit doesn't work for non final relation
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
    60
                 'deprecated (%s)' % self.__class__)
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    61
            self.render_entity_attributes(entity, [])
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
        self.w(u'</div>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
        self.content_navigation_components('navcontenttop')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
        if self.main_related_section:
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    65
            try:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    66
                self.render_entity_relations(entity)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    67
            except TypeError: # XXX bw compat
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    68
                warn('siderelations argument of render_entity_relations is '
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    69
                     'deprecated')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    70
                self.render_entity_relations(entity, [])
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
        self.w(u'</div>')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    72
        if boxes or hasattr(self, 'render_side_related'):
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
            self.w(u'</td><td>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
            # side boxes
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
            self.w(u'<div class="primaryRight">')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    76
            if hasattr(self, 'render_side_related'):
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    77
                warn('render_side_related is deprecated')
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    78
                self.render_side_related(entity, [])
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
    79
            self.render_side_boxes(boxes)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
            self.w(u'</div>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
            self.w(u'</td></tr></table>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
        self.content_navigation_components('navcontentbottom')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    83
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
    def content_navigation_components(self, context):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
        self.w(u'<div class="%s">' % context)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
        for comp in self.vreg.possible_vobjects('contentnavigation',
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
                                                self.req, self.rset, row=self.row,
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
                                                view=self, context=context):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
            try:
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1721
diff changeset
    91
                comp.render(w=self.w, row=self.row, view=self)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
            except NotImplementedError:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
                warn('component %s doesnt implement cell_call, please update'
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
                     % comp.__class__, DeprecationWarning)
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1721
diff changeset
    95
                comp.render(w=self.w, view=self)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
        self.w(u'</div>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
    def render_entity_title(self, entity):
1720
14c9a0a1aca1 drop content_title
sylvain.thenault@logilab.fr
parents: 1632
diff changeset
    99
        """default implementation return dc_title"""
14c9a0a1aca1 drop content_title
sylvain.thenault@logilab.fr
parents: 1632
diff changeset
   100
        title = html_escape(entity.dc_title())
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        if title:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
            self.w(u'<h1><span class="etype">%s</span> %s</h1>'
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
                   % (entity.dc_type().capitalize(), title))
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
    def render_entity_metadata(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
        entity.view('metadata', w=self.w)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
        summary = self.summary(entity) # deprecate summary?
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        if summary:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
            self.w(u'<div class="summary">%s</div>' % summary)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
    def summary(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
        """default implementation return an empty string"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   113
        return u''
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   115
    def render_entity_attributes(self, entity, siderelations=None):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   116
        for rschema, tschemas, role, dispctrl in self._section_def(entity, 'attributes'):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   117
            vid =  dispctrl.get('vid', 'reledit')
1558
f63d090eb34a some primary view fixes, need more (eg reledit doesn't work for non final relation
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   118
            if rschema.is_final() or vid == 'reledit':
f63d090eb34a some primary view fixes, need more (eg reledit doesn't work for non final relation
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   119
                value = entity.view(vid, rtype=rschema.type, role=role)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
            else:
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   121
                rset = self._relation_rset(entity, rschema, role, dispctrl)
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   122
                if rset:
1558
f63d090eb34a some primary view fixes, need more (eg reledit doesn't work for non final relation
sylvain.thenault@logilab.fr
parents: 1554
diff changeset
   123
                    value = self.view(vid, rset)
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   124
                else:
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   125
                    value = None
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   126
            if self.skip_none and (value is None or value == ''):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
                continue
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   128
            self._render_attribute(rschema, value)
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   129
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   130
    def render_entity_relations(self, entity, siderelations=None):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   131
        for rschema, tschemas, role, dispctrl in self._section_def(entity, 'relations'):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   132
            rset = self._relation_rset(entity, rschema, role, dispctrl)
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   133
            if rset:
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   134
                self._render_relation(rset, dispctrl, 'autolimited',
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   135
                                      self.show_rel_label)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   137
    def render_side_boxes(self, boxes):
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
        """display side related relations:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
        non-meta in a first step, meta in a second step
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
        """
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   141
        for box in boxes:
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   142
            if isinstance(box, tuple):
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   143
                label, rset, vid  = box
1885
c2011d238e98 replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1752
diff changeset
   144
                self.w(u'<div class="sideBox">')
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   145
                self.wview(vid, rset, title=label)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
                self.w(u'</div>')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   147
            else:
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
                try:
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1721
diff changeset
   149
                    box.render(w=self.w, row=self.row)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
                except NotImplementedError:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   151
                    # much probably a context insensitive box, which only implements
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   152
                    # .call() and not cell_call()
1723
30c3a713ab61 View.dispatch -> View.render
sylvain.thenault@logilab.fr
parents: 1721
diff changeset
   153
                    box.render(w=self.w)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   154
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   155
    def _prepare_side_boxes(self, entity):
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   156
        sideboxes = []
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   157
        for rschema, tschemas, role, dispctrl in self._section_def(entity, 'sideboxes'):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   158
            rset = self._relation_rset(entity, rschema, role, dispctrl)
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   159
            if not rset:
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   160
                continue
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   161
            label = display_name(self.req, rschema.type, role)
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   162
            vid = dispctrl.get('vid', 'sidebox')
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   163
            sideboxes.append( (label, rset, vid) )
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   164
        sideboxes += self.vreg.possible_vobjects('boxes', self.req, self.rset,
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   165
                                                 row=self.row, view=self,
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   166
                                                 context='incontext')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   167
        return sideboxes
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   168
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   169
    def _section_def(self, entity, where):
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   170
        rdefs = []
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   171
        eschema = entity.e_schema
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   172
        for rschema, tschemas, role in eschema.relation_definitions(True):
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   173
            matchtschemas = []
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   174
            for tschema in tschemas:
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   175
                section = self.rsection.etype_get(eschema, rschema, role,
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   176
                                                  tschema)
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   177
                if section == where:
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   178
                    matchtschemas.append(tschema)
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   179
            if matchtschemas:
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   180
                # XXX pick the latest dispctrl
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   181
                dispctrl = self.display_ctrl.etype_get(eschema, rschema, role,
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   182
                                                       matchtschemas[-1])
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   183
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   184
                rdefs.append( (rschema, matchtschemas, role, dispctrl) )
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   185
        return sorted(rdefs, key=lambda x: x[-1]['order'])
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   186
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   187
    def _relation_rset(self, entity, rschema, role, dispctrl):
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   188
        try:
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   189
            if dispctrl.get('limit'):
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   190
                rset = entity.related(rschema.type, role,
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   191
                                      limit=self.maxrelated+1)
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   192
            else:
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   193
                rset = entity.related(rschema.type, role)
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   194
        except Unauthorized:
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   195
            return
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   196
        if 'filter' in dispctrl:
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   197
            rset = dispctrl['filter'](rset)
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   198
        return rset
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   199
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   200
    def _render_relation(self, rset, dispctrl, defaultvid, showlabel):
1583
1d6d0ea1cda1 needs unicode string
sylvain.thenault@logilab.fr
parents: 1558
diff changeset
   201
        self.w(u'<div class="section">')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   202
        if showlabel:
1739
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   203
            self.w(u'<h4>%s</h4>' % self.req._(dispctrl['label']))
78b0819162a8 final rtag api
sylvain.thenault@logilab.fr
parents: 1726
diff changeset
   204
        self.wview(dispctrl.get('vid', defaultvid), rset)
1583
1d6d0ea1cda1 needs unicode string
sylvain.thenault@logilab.fr
parents: 1558
diff changeset
   205
        self.w(u'</div>')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   206
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   207
    def _render_attribute(self, rschema, value, role='subject'):
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   208
        if rschema.is_final():
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   209
            show_label = self.show_attr_label
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   210
        else:
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   211
            show_label = self.show_rel_label
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   212
        label = display_name(self.req, rschema.type, role)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   213
        self.field(label, value, show_label=show_label, tr=False)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   214
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   215
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   216
class RelatedView(EntityView):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   217
    id = 'autolimited'
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   218
    def call(self, title=None, **kwargs):
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   219
        # if not too many entities, show them all in a list
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   220
        maxrelated = self.req.property_value('navigation.related-limit')
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   221
        if title:
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   222
            self.w(u'<div class="title"><span>%s</span></div>' % title)
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   223
        if self.rset.rowcount <= maxrelated:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   224
            if self.rset.rowcount == 1:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   225
                self.wview('incontext', self.rset, row=0)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   226
            elif 1 < self.rset.rowcount <= 5:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   227
                self.wview('csv', self.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   228
            else:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   229
                self.w(u'<div>')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   230
                self.wview('simplelist', self.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   231
                self.w(u'</div>')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   232
        # else show links to display related entities
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   233
        else:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   234
            rql = self.rset.printable_rql()
1554
3a3263df6cdd new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents: 1516
diff changeset
   235
            self.rset.limit(maxrelated)
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   236
            self.w(u'<div>')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   237
            self.wview('simplelist', self.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   238
            self.w(u'[<a href="%s">%s</a>]' % (self.build_url(rql=rql),
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   239
                                               self.req._('see them all')))
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   240
            self.w(u'</div>')