web/views/primary.py
author sylvain.thenault@logilab.fr
Tue, 28 Apr 2009 18:32:53 +0200
branchtls-sprint
changeset 1516 288d55a7c5e2
parent 1491 742aff97dbf7
child 1554 3a3263df6cdd
permissions -rw-r--r--
refactor side boxes handling of primary view
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
"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
from warnings import warn
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 cubicweb import Unauthorized
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
from cubicweb.view import EntityView
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
_ = unicode
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
PRIMARY_SKIP_RELS = set(['is', 'is_instance_of', 'identity',
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
                         'owned_by', 'created_by',
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
                         'in_state', 'wf_info_for', 'require_permission',
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
                         'from_entity', 'to_entity',
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
                         'see_also'])
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
class PrimaryView(EntityView):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
    """the full view of an non final entity"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
    id = 'primary'
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
    title = _('primary')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
    show_attr_label = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
    show_rel_label = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
    skip_none = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
    skip_attrs = ('eid', 'creation_date', 'modification_date')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
    skip_rels = ()
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
    main_related_section = True
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
    def html_headers(self):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
        """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
    34
        <head> and </head> of the returned page
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
        by default primary views are indexed
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
        """
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
        return []
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
    def cell_call(self, row, col):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
        self.row = row
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    42
        # XXX move render_entity implementation here
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
        self.render_entity(self.complete_entity(row, col))
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
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    51
        boxes = self._preinit_side_related(entity)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
        if boxes:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
            self.w(u'<table width="100%"><tr><td width="75%">')
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 '
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    60
                 'deprecated')
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>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
        if boxes:
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">')
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    76
            try:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    77
                self.render_side_related(entity)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    78
            except TypeError: # XXX bw compat
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    79
                warn('siderelations argument of render_entity_relations is '
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    80
                     'deprecated')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
    81
                self.render_entity_relations(entity, [])
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
            self.w(u'</div>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    83
            self.w(u'</td></tr></table>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
        self.content_navigation_components('navcontentbottom')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
    def content_navigation_components(self, context):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
        self.w(u'<div class="%s">' % context)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
        for comp in self.vreg.possible_vobjects('contentnavigation',
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
                                                self.req, self.rset, row=self.row,
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
                                                view=self, context=context):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
            try:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
                comp.dispatch(w=self.w, row=self.row, view=self)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
            except NotImplementedError:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
                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
    96
                     % comp.__class__, DeprecationWarning)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
                comp.dispatch(w=self.w, view=self)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
        self.w(u'</div>')
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
    def iter_attributes(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        for rschema, targetschema in entity.e_schema.attribute_definitions():
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
            if rschema.type in self.skip_attrs:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
                continue
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
            yield rschema, targetschema
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
    def iter_relations(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
        skip = set(self.skip_rels)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        skip.update(PRIMARY_SKIP_RELS)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
        for rschema, targetschemas, x in entity.e_schema.relation_definitions():
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
            if rschema.type in skip:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
                continue
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
            yield rschema, targetschemas, x
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   113
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
    def render_entity_title(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
        title = self.content_title(entity) # deprecate content_title?
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   116
        if title:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   117
            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
   118
                   % (entity.dc_type().capitalize(), title))
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
    def content_title(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   121
        """default implementation return an empty string"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   122
        return u''
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
    def render_entity_metadata(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
        entity.view('metadata', w=self.w)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   126
        summary = self.summary(entity) # deprecate summary?
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
        if summary:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   128
            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
   129
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
    def summary(self, entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   131
        """default implementation return an empty string"""
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   132
        return u''
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   134
    def render_entity_attributes(self, entity, siderelations=None):
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   135
        for rschema, targetschema in self.iter_attributes(entity):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
            attr = rschema.type
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   137
            if targetschema.type in ('Password', 'Bytes'):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
                continue
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
            try:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
                wdg = entity.get_widget(attr)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
            except Exception, ex:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   142
                value = entity.printable_value(attr, entity[attr], targetschema.type)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
            else:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
                value = wdg.render(entity)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
            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
   146
                continue
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
            if rschema.meta:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
                continue
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   149
            self._render_related_entities(entity, rschema, value)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   151
    def _preinit_side_related(self, entity):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   152
        self._sideboxes = []
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   153
        if hasattr(self, 'get_side_boxes_defs'):
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   154
            self._sideboxes = [(label, rset, 'sidebox') for label, rset in self.get_side_boxes_defs(entity)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
                               if rset]
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
        else:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   157
            eschema = entity.e_schema
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
            maxrelated = self.req.property_value('navigation.related-limit')
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   159
            for rschema, targetschemas, role in self.iter_relations(entity):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   160
                if self.is_side_related(rschema, eschema):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   161
                    try:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   162
                        related = entity.related(rschema.type, role, limit=maxrelated+1)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   163
                    except Unauthorized:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   164
                        continue
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   165
                    if not related:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   166
                        continue
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   167
                    label = display_name(self.req, rschema.type, role)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   168
                    self._sideboxes.append((label, related, 'autolimited'))
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   169
        self._contextboxes = list(self.vreg.possible_vobjects('boxes', self.req, self.rset,
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   170
                                                                  row=self.row, view=self,
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   171
                                                                  context='incontext'))
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   172
        return self._sideboxes or self._contextboxes
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   173
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   174
    def render_entity_relations(self, entity, siderelations=None):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   175
        eschema = entity.e_schema
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   176
        for rschema, targetschemas, x in self.iter_relations(entity):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   177
            if not self.is_side_related(rschema, eschema):
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   178
                try:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   179
                    related = entity.related(rschema.type, x, limit=maxrelated+1)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   180
                except Unauthorized:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
                    continue
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
                self._render_related_entities(entity, rschema, related, x)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   183
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   184
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   185
    def render_side_related(self, entity, siderelations=None):
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   186
        """display side related relations:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   187
        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
   188
        """
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   189
        if self._sideboxes:
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   190
            for label, rset, vid in self._sideboxes:
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   191
                self.w(u'<div class="sideRelated">')
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   192
                self.wview(vid, rset, title=label)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   193
                self.w(u'</div>')
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   194
        if self._contextboxes:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   195
            for box in self._contextboxes:
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   196
                try:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   197
                    box.dispatch(w=self.w, row=self.row)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
                except NotImplementedError:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   199
                    # 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
   200
                    # .call() and not cell_call()
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   201
                    box.dispatch(w=self.w)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   202
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   203
    def is_side_related(self, rschema, eschema):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   204
        return rschema.meta and \
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   205
               not rschema.schema_relation() == eschema.schema_entity()
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   207
    def _render_related_entities(self, entity, rschema, related,
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   208
                                 role='subject'):
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   209
        if rschema.is_final():
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   210
            value = related
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   211
            show_label = self.show_attr_label
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   212
        else:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   213
            if not related:
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   214
                return
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   215
            value = self.view('autolimited', related)
1491
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   216
        label = display_name(self.req, rschema.type, role)
742aff97dbf7 move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   217
        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
   218
1516
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   219
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   220
class RelatedView(EntityView):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   221
    id = 'autolimited'
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   222
    def call(self):
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   223
        # 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
   224
        maxrelated = self.req.property_value('navigation.related-limit')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   225
        if self.rset.rowcount <= maxrelated:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   226
            if self.rset.rowcount == 1:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   227
                self.wview('incontext', self.rset, row=0)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   228
            elif 1 < self.rset.rowcount <= 5:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   229
                self.wview('csv', self.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   230
            else:
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
                self.wview('simplelist', self.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   233
                self.w(u'</div>')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   234
        # else show links to display related entities
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   235
        else:
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   236
            rql = self.rset.printable_rql()
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   237
            self.rset.limit(maxself.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   238
            self.w(u'<div>')
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   239
            self.wview('simplelist', self.rset)
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   240
            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
   241
                                               self.req._('see them all')))
288d55a7c5e2 refactor side boxes handling of primary view
sylvain.thenault@logilab.fr
parents: 1491
diff changeset
   242
            self.w(u'</div>')