web/views/editviews.py
changeset 3451 6b46d73823f5
parent 3377 dd9d292b6a6d
child 3460 e4843535db25
equal deleted inserted replaced
3448:495862266785 3451:6b46d73823f5
    33 
    33 
    34     title = _('search for association')
    34     title = _('search for association')
    35 
    35 
    36     def cell_call(self, row, col):
    36     def cell_call(self, row, col):
    37         rset, vid, divid, paginate = self.filter_box_context_info()
    37         rset, vid, divid, paginate = self.filter_box_context_info()
    38         self.rset = rset
    38         self.cw_rset = rset
    39         self.w(u'<div id="%s">' % divid)
    39         self.w(u'<div id="%s">' % divid)
    40         self.paginate()
    40         self.paginate()
    41         self.wview(vid, rset, 'noresult')
    41         self.wview(vid, rset, 'noresult')
    42         self.w(u'</div>')
    42         self.w(u'</div>')
    43 
    43 
    44     @cached
    44     @cached
    45     def filter_box_context_info(self):
    45     def filter_box_context_info(self):
    46         entity = self.rset.get_entity(0, 0)
    46         entity = self.cw_rset.get_entity(0, 0)
    47         role, eid, rtype, etype = self.req.search_state[1]
    47         role, eid, rtype, etype = self._cw.search_state[1]
    48         assert entity.eid == typed_eid(eid)
    48         assert entity.eid == typed_eid(eid)
    49         # the default behaviour is to fetch all unrelated entities and display
    49         # the default behaviour is to fetch all unrelated entities and display
    50         # them. Use fetch_order and not fetch_unrelated_order as sort method
    50         # them. Use fetch_order and not fetch_unrelated_order as sort method
    51         # since the latter is mainly there to select relevant items in the combo
    51         # since the latter is mainly there to select relevant items in the combo
    52         # box, it doesn't give interesting result in this context
    52         # box, it doesn't give interesting result in this context
    53         rql, args = entity.unrelated_rql(rtype, etype, role,
    53         rql, args = entity.unrelated_rql(rtype, etype, role,
    54                                    ordermethod='fetch_order',
    54                                    ordermethod='fetch_order',
    55                                    vocabconstraints=False)
    55                                    vocabconstraints=False)
    56         rset = self.req.execute(rql, args, tuple(args))
    56         rset = self._cw.execute(rql, args, tuple(args))
    57         return rset, 'list', "search-associate-content", True
    57         return rset, 'list', "search-associate-content", True
    58 
    58 
    59 
    59 
    60 class OutOfContextSearch(EntityView):
    60 class OutOfContextSearch(EntityView):
    61     __regid__ = 'outofcontext-search'
    61     __regid__ = 'outofcontext-search'
    62     def cell_call(self, row, col):
    62     def cell_call(self, row, col):
    63         entity = self.rset.get_entity(row, col)
    63         entity = self.cw_rset.get_entity(row, col)
    64         erset = entity.as_rset()
    64         erset = entity.as_rset()
    65         if self.req.match_search_state(erset):
    65         if self._cw.match_search_state(erset):
    66             self.w(u'<a href="%s" title="%s">%s</a>&#160;<a href="%s" title="%s">[...]</a>' % (
    66             self.w(u'<a href="%s" title="%s">%s</a>&#160;<a href="%s" title="%s">[...]</a>' % (
    67                 xml_escape(linksearch_select_url(self.req, erset)),
    67                 xml_escape(linksearch_select_url(self._cw, erset)),
    68                 self.req._('select this entity'),
    68                 self._cw._('select this entity'),
    69                 xml_escape(entity.view('textoutofcontext')),
    69                 xml_escape(entity.view('textoutofcontext')),
    70                 xml_escape(entity.absolute_url(vid='primary')),
    70                 xml_escape(entity.absolute_url(vid='primary')),
    71                 self.req._('view detail for this entity')))
    71                 self._cw._('view detail for this entity')))
    72         else:
    72         else:
    73             entity.view('outofcontext', w=self.w)
    73             entity.view('outofcontext', w=self.w)
    74 
    74 
    75 
    75 
    76 class UnrelatedDivs(EntityView):
    76 class UnrelatedDivs(EntityView):
    77     __regid__ = 'unrelateddivs'
    77     __regid__ = 'unrelateddivs'
    78     __select__ = match_form_params('relation')
    78     __select__ = match_form_params('relation')
    79 
    79 
    80     def cell_call(self, row, col):
    80     def cell_call(self, row, col):
    81         entity = self.rset.get_entity(row, col)
    81         entity = self.cw_rset.get_entity(row, col)
    82         relname, target = self.req.form.get('relation').rsplit('_', 1)
    82         relname, target = self._cw.form.get('relation').rsplit('_', 1)
    83         rschema = self.schema.rschema(relname)
    83         rschema = self._cw.schema.rschema(relname)
    84         hidden = 'hidden' in self.req.form
    84         hidden = 'hidden' in self._cw.form
    85         is_cell = 'is_cell' in self.req.form
    85         is_cell = 'is_cell' in self._cw.form
    86         self.w(self.build_unrelated_select_div(entity, rschema, target,
    86         self.w(self.build_unrelated_select_div(entity, rschema, target,
    87                                                is_cell=is_cell, hidden=hidden))
    87                                                is_cell=is_cell, hidden=hidden))
    88 
    88 
    89     def build_unrelated_select_div(self, entity, rschema, target,
    89     def build_unrelated_select_div(self, entity, rschema, target,
    90                                    is_cell=False, hidden=True):
    90                                    is_cell=False, hidden=True):
    91         options = []
    91         options = []
    92         divid = 'div%s_%s_%s' % (rschema.type, target, entity.eid)
    92         divid = 'div%s_%s_%s' % (rschema.type, target, entity.eid)
    93         selectid = 'select%s_%s_%s' % (rschema.type, target, entity.eid)
    93         selectid = 'select%s_%s_%s' % (rschema.type, target, entity.eid)
    94         if rschema.symetric or target == 'subject':
    94         if rschema.symetric or target == 'subject':
    95             targettypes = rschema.objects(entity.e_schema)
    95             targettypes = rschema.objects(entity.e_schema)
    96             etypes = '/'.join(sorted(etype.display_name(self.req) for etype in targettypes))
    96             etypes = '/'.join(sorted(etype.display_name(self._cw) for etype in targettypes))
    97         else:
    97         else:
    98             targettypes = rschema.subjects(entity.e_schema)
    98             targettypes = rschema.subjects(entity.e_schema)
    99             etypes = '/'.join(sorted(etype.display_name(self.req) for etype in targettypes))
    99             etypes = '/'.join(sorted(etype.display_name(self._cw) for etype in targettypes))
   100         etypes = cut(etypes, self.req.property_value('navigation.short-line-size'))
   100         etypes = cut(etypes, self._cw.property_value('navigation.short-line-size'))
   101         options.append('<option>%s %s</option>' % (self.req._('select a'), etypes))
   101         options.append('<option>%s %s</option>' % (self._cw._('select a'), etypes))
   102         options += self._get_select_options(entity, rschema, target)
   102         options += self._get_select_options(entity, rschema, target)
   103         options += self._get_search_options(entity, rschema, target, targettypes)
   103         options += self._get_search_options(entity, rschema, target, targettypes)
   104         if 'Basket' in self.schema: # XXX
   104         if 'Basket' in self._cw.schema: # XXX
   105             options += self._get_basket_options(entity, rschema, target, targettypes)
   105             options += self._get_basket_options(entity, rschema, target, targettypes)
   106         relname, target = self.req.form.get('relation').rsplit('_', 1)
   106         relname, target = self._cw.form.get('relation').rsplit('_', 1)
   107         return u"""\
   107         return u"""\
   108 <div class="%s" id="%s">
   108 <div class="%s" id="%s">
   109   <select id="%s" onchange="javascript: addPendingInsert(this.options[this.selectedIndex], %s, %s, '%s');">
   109   <select id="%s" onchange="javascript: addPendingInsert(this.options[this.selectedIndex], %s, %s, '%s');">
   110     %s
   110     %s
   111   </select>
   111   </select>
   116 
   116 
   117     def _get_select_options(self, entity, rschema, target):
   117     def _get_select_options(self, entity, rschema, target):
   118         """add options to search among all entities of each possible type"""
   118         """add options to search among all entities of each possible type"""
   119         options = []
   119         options = []
   120         eid = entity.eid
   120         eid = entity.eid
   121         pending_inserts = self.req.get_pending_inserts(eid)
   121         pending_inserts = self._cw.get_pending_inserts(eid)
   122         rtype = rschema.type
   122         rtype = rschema.type
   123         form = self.vreg['forms'].select('edition', self.req, entity=entity)
   123         form = self._cw.vreg['forms'].select('edition', self._cw, entity=entity)
   124         field = form.field_by_name(rschema, target, entity.e_schema)
   124         field = form.field_by_name(rschema, target, entity.e_schema)
   125         limit = self.req.property_value('navigation.combobox-limit')
   125         limit = self._cw.property_value('navigation.combobox-limit')
   126         for eview, reid in form.form_field_vocabulary(field, limit):
   126         for eview, reid in form.form_field_vocabulary(field, limit):
   127             if reid is None:
   127             if reid is None:
   128                 options.append('<option class="separator">-- %s --</option>'
   128                 options.append('<option class="separator">-- %s --</option>'
   129                                % xml_escape(eview))
   129                                % xml_escape(eview))
   130             else:
   130             else:
   136         return options
   136         return options
   137 
   137 
   138     def _get_search_options(self, entity, rschema, target, targettypes):
   138     def _get_search_options(self, entity, rschema, target, targettypes):
   139         """add options to search among all entities of each possible type"""
   139         """add options to search among all entities of each possible type"""
   140         options = []
   140         options = []
   141         _ = self.req._
   141         _ = self._cw._
   142         for eschema in targettypes:
   142         for eschema in targettypes:
   143             mode = '%s:%s:%s:%s' % (target, entity.eid, rschema.type, eschema)
   143             mode = '%s:%s:%s:%s' % (target, entity.eid, rschema.type, eschema)
   144             url = self.build_url(entity.rest_path(), vid='search-associate',
   144             url = self.build_url(entity.rest_path(), vid='search-associate',
   145                                  __mode=mode)
   145                                  __mode=mode)
   146             options.append((eschema.display_name(self.req),
   146             options.append((eschema.display_name(self._cw),
   147                             '<option value="%s">%s %s</option>' % (
   147                             '<option value="%s">%s %s</option>' % (
   148                 xml_escape(url), _('Search for'), eschema.display_name(self.req))))
   148                 xml_escape(url), _('Search for'), eschema.display_name(self._cw))))
   149         return [o for l, o in sorted(options)]
   149         return [o for l, o in sorted(options)]
   150 
   150 
   151     def _get_basket_options(self, entity, rschema, target, targettypes):
   151     def _get_basket_options(self, entity, rschema, target, targettypes):
   152         options = []
   152         options = []
   153         rtype = rschema.type
   153         rtype = rschema.type
   154         _ = self.req._
   154         _ = self._cw._
   155         for basketeid, basketname in self._get_basket_links(self.req.user.eid,
   155         for basketeid, basketname in self._get_basket_links(self._cw.user.eid,
   156                                                             target, targettypes):
   156                                                             target, targettypes):
   157             optionid = relation_id(entity.eid, rtype, target, basketeid)
   157             optionid = relation_id(entity.eid, rtype, target, basketeid)
   158             options.append('<option id="%s" value="%s">%s %s</option>' % (
   158             options.append('<option id="%s" value="%s">%s %s</option>' % (
   159                 optionid, basketeid, _('link to each item in'), xml_escape(basketname)))
   159                 optionid, basketeid, _('link to each item in'), xml_escape(basketname)))
   160         return options
   160         return options
   169                 yield basketeid, basketname
   169                 yield basketeid, basketname
   170 
   170 
   171     def _get_basket_info(self, ueid):
   171     def _get_basket_info(self, ueid):
   172         basketref = []
   172         basketref = []
   173         basketrql = 'Any B,N WHERE B is Basket, B owned_by U, U eid %(x)s, B name N'
   173         basketrql = 'Any B,N WHERE B is Basket, B owned_by U, U eid %(x)s, B name N'
   174         basketresultset = self.req.execute(basketrql, {'x': ueid}, 'x')
   174         basketresultset = self._cw.execute(basketrql, {'x': ueid}, 'x')
   175         for result in basketresultset:
   175         for result in basketresultset:
   176             basketitemsrql = 'Any X WHERE X in_basket B, B eid %(x)s'
   176             basketitemsrql = 'Any X WHERE X in_basket B, B eid %(x)s'
   177             rset = self.req.execute(basketitemsrql, {'x': result[0]}, 'x')
   177             rset = self._cw.execute(basketitemsrql, {'x': result[0]}, 'x')
   178             basketref.append((result[0], result[1], rset))
   178             basketref.append((result[0], result[1], rset))
   179         return basketref
   179         return basketref
   180 
   180 
   181 
   181 
   182 class ComboboxView(EntityView):
   182 class ComboboxView(EntityView):
   189 
   189 
   190     def cell_call(self, row, col):
   190     def cell_call(self, row, col):
   191         """the combo-box view for an entity: same as text out of context view
   191         """the combo-box view for an entity: same as text out of context view
   192         by default
   192         by default
   193         """
   193         """
   194         self.wview('textoutofcontext', self.rset, row=row, col=col)
   194         self.wview('textoutofcontext', self.cw_rset, row=row, col=col)
   195 
   195 
   196 
   196 
   197 class EditableFinalView(FinalView):
   197 class EditableFinalView(FinalView):
   198     """same as FinalView but enables inplace-edition when possible"""
   198     """same as FinalView but enables inplace-edition when possible"""
   199     __regid__ = 'editable-final'
   199     __regid__ = 'editable-final'
   200 
   200 
   201     def cell_call(self, row, col, props=None):
   201     def cell_call(self, row, col, props=None):
   202         entity, rtype = self.rset.related_entity(row, col)
   202         entity, rtype = self.cw_rset.related_entity(row, col)
   203         if entity is not None:
   203         if entity is not None:
   204             self.w(entity.view('reledit', rtype=rtype))
   204             self.w(entity.view('reledit', rtype=rtype))
   205         else:
   205         else:
   206             super(EditableFinalView, self).cell_call(row, col, props)
   206             super(EditableFinalView, self).cell_call(row, col, props)