web/views/editviews.py
changeset 4278 24736a127bf1
parent 4277 35cd057339b2
child 4294 235fe518b2e4
equal deleted inserted replaced
4277:35cd057339b2 4278:24736a127bf1
   281     __regid__ = 'unrelateddivs'
   281     __regid__ = 'unrelateddivs'
   282     __select__ = match_form_params('relation')
   282     __select__ = match_form_params('relation')
   283 
   283 
   284     def cell_call(self, row, col):
   284     def cell_call(self, row, col):
   285         entity = self.cw_rset.get_entity(row, col)
   285         entity = self.cw_rset.get_entity(row, col)
   286         relname, target = self._cw.form.get('relation').rsplit('_', 1)
   286         relname, role = self._cw.form.get('relation').rsplit('_', 1)
   287         rschema = self._cw.vreg.schema.rschema(relname)
   287         rschema = self._cw.vreg.schema.rschema(relname)
   288         hidden = 'hidden' in self._cw.form
   288         hidden = 'hidden' in self._cw.form
   289         is_cell = 'is_cell' in self._cw.form
   289         is_cell = 'is_cell' in self._cw.form
   290         self.w(self.build_unrelated_select_div(entity, rschema, target,
   290         self.w(self.build_unrelated_select_div(entity, rschema, role,
   291                                                is_cell=is_cell, hidden=hidden))
   291                                                is_cell=is_cell, hidden=hidden))
   292 
   292 
   293     def build_unrelated_select_div(self, entity, rschema, target,
   293     def build_unrelated_select_div(self, entity, rschema, role,
   294                                    is_cell=False, hidden=True):
   294                                    is_cell=False, hidden=True):
   295         options = []
   295         options = []
   296         divid = 'div%s_%s_%s' % (rschema.type, target, entity.eid)
   296         divid = 'div%s_%s_%s' % (rschema.type, role, entity.eid)
   297         selectid = 'select%s_%s_%s' % (rschema.type, target, entity.eid)
   297         selectid = 'select%s_%s_%s' % (rschema.type, role, entity.eid)
   298         if rschema.symetric or target == 'subject':
   298         if rschema.symetric or role == 'subject':
   299             targettypes = rschema.objects(entity.e_schema)
   299             targettypes = rschema.objects(entity.e_schema)
   300             etypes = '/'.join(sorted(etype.display_name(self._cw) for etype in targettypes))
   300             etypes = '/'.join(sorted(etype.display_name(self._cw) for etype in targettypes))
   301         else:
   301         else:
   302             targettypes = rschema.subjects(entity.e_schema)
   302             targettypes = rschema.subjects(entity.e_schema)
   303             etypes = '/'.join(sorted(etype.display_name(self._cw) for etype in targettypes))
   303             etypes = '/'.join(sorted(etype.display_name(self._cw) for etype in targettypes))
   304         etypes = uilib.cut(etypes, self._cw.property_value('navigation.short-line-size'))
   304         etypes = uilib.cut(etypes, self._cw.property_value('navigation.short-line-size'))
   305         options.append('<option>%s %s</option>' % (self._cw._('select a'), etypes))
   305         options.append('<option>%s %s</option>' % (self._cw._('select a'), etypes))
   306         options += self._get_select_options(entity, rschema, target)
   306         options += self._get_select_options(entity, rschema, role)
   307         options += self._get_search_options(entity, rschema, target, targettypes)
   307         options += self._get_search_options(entity, rschema, role, targettypes)
   308         if 'Basket' in self._cw.vreg.schema: # XXX
   308         if 'Basket' in self._cw.vreg.schema: # XXX
   309             options += self._get_basket_options(entity, rschema, target, targettypes)
   309             options += self._get_basket_options(entity, rschema, role, targettypes)
   310         relname, target = self._cw.form.get('relation').rsplit('_', 1)
   310         relname, role = self._cw.form.get('relation').rsplit('_', 1)
   311         return u"""\
   311         return u"""\
   312 <div class="%s" id="%s">
   312 <div class="%s" id="%s">
   313   <select id="%s" onchange="javascript: addPendingInsert(this.options[this.selectedIndex], %s, %s, '%s');">
   313   <select id="%s" onchange="javascript: addPendingInsert(this.options[this.selectedIndex], %s, %s, '%s');">
   314     %s
   314     %s
   315   </select>
   315   </select>
   316 </div>
   316 </div>
   317 """ % (hidden and 'hidden' or '', divid, selectid,
   317 """ % (hidden and 'hidden' or '', divid, selectid,
   318        xml_escape(dumps(entity.eid)), is_cell and 'true' or 'null', relname,
   318        xml_escape(dumps(entity.eid)), is_cell and 'true' or 'null', relname,
   319        '\n'.join(options))
   319        '\n'.join(options))
   320 
   320 
   321     def _get_select_options(self, entity, rschema, target):
   321     def _get_select_options(self, entity, rschema, role):
   322         """add options to search among all entities of each possible type"""
   322         """add options to search among all entities of each possible type"""
   323         options = []
   323         options = []
   324         pending_inserts = get_pending_inserts(self._cw, entity.eid)
   324         pending_inserts = get_pending_inserts(self._cw, entity.eid)
   325         rtype = rschema.type
   325         rtype = rschema.type
   326         form = self._cw.vreg['forms'].select('edition', self._cw, entity=entity)
   326         form = self._cw.vreg['forms'].select('edition', self._cw, entity=entity)
   327         field = form.field_by_name(rschema, target, entity.e_schema)
   327         field = form.field_by_name(rschema, role, entity.e_schema)
   328         limit = self._cw.property_value('navigation.combobox-limit')
   328         limit = self._cw.property_value('navigation.combobox-limit')
   329         for eview, reid in field.choices(form, limit): # XXX expect 'limit' arg on choices
   329         for eview, reid in field.choices(form, limit): # XXX expect 'limit' arg on choices
   330             if reid is None:
   330             if reid is None:
   331                 if eview: # skip blank value
   331                 if eview: # skip blank value
   332                     options.append('<option class="separator">-- %s --</option>'
   332                     options.append('<option class="separator">-- %s --</option>'
   333                                    % xml_escape(eview))
   333                                    % xml_escape(eview))
   334             else:
   334             else:
   335                 optionid = relation_id(entity.eid, rtype, target, reid)
   335                 optionid = relation_id(entity.eid, rtype, role, reid)
   336                 if optionid not in pending_inserts:
   336                 if optionid not in pending_inserts:
   337                     # prefix option's id with letters to make valid XHTML wise
   337                     # prefix option's id with letters to make valid XHTML wise
   338                     options.append('<option id="id%s" value="%s">%s</option>' %
   338                     options.append('<option id="id%s" value="%s">%s</option>' %
   339                                    (optionid, reid, xml_escape(eview)))
   339                                    (optionid, reid, xml_escape(eview)))
   340         return options
   340         return options
   341 
   341 
   342     def _get_search_options(self, entity, rschema, target, targettypes):
   342     def _get_search_options(self, entity, rschema, role, targettypes):
   343         """add options to search among all entities of each possible type"""
   343         """add options to search among all entities of each possible type"""
   344         options = []
   344         options = []
   345         _ = self._cw._
   345         _ = self._cw._
   346         for eschema in targettypes:
   346         for eschema in targettypes:
   347             mode = '%s:%s:%s:%s' % (target, entity.eid, rschema.type, eschema)
   347             mode = '%s:%s:%s:%s' % (role, entity.eid, rschema.type, eschema)
   348             url = self._cw.build_url(entity.rest_path(), vid='search-associate',
   348             url = self._cw.build_url(entity.rest_path(), vid='search-associate',
   349                                  __mode=mode)
   349                                  __mode=mode)
   350             options.append((eschema.display_name(self._cw),
   350             options.append((eschema.display_name(self._cw),
   351                             '<option value="%s">%s %s</option>' % (
   351                             '<option value="%s">%s %s</option>' % (
   352                 xml_escape(url), _('Search for'), eschema.display_name(self._cw))))
   352                 xml_escape(url), _('Search for'), eschema.display_name(self._cw))))
   353         return [o for l, o in sorted(options)]
   353         return [o for l, o in sorted(options)]
   354 
   354 
   355     def _get_basket_options(self, entity, rschema, target, targettypes):
   355     def _get_basket_options(self, entity, rschema, role, targettypes):
   356         options = []
   356         options = []
   357         rtype = rschema.type
   357         rtype = rschema.type
   358         _ = self._cw._
   358         _ = self._cw._
   359         for basketeid, basketname in self._get_basket_links(self._cw.user.eid,
   359         for basketeid, basketname in self._get_basket_links(self._cw.user.eid,
   360                                                             target, targettypes):
   360                                                             role, targettypes):
   361             optionid = relation_id(entity.eid, rtype, target, basketeid)
   361             optionid = relation_id(entity.eid, rtype, role, basketeid)
   362             options.append('<option id="%s" value="%s">%s %s</option>' % (
   362             options.append('<option id="%s" value="%s">%s %s</option>' % (
   363                 optionid, basketeid, _('link to each item in'), xml_escape(basketname)))
   363                 optionid, basketeid, _('link to each item in'), xml_escape(basketname)))
   364         return options
   364         return options
   365 
   365 
   366     def _get_basket_links(self, ueid, target, targettypes):
   366     def _get_basket_links(self, ueid, role, targettypes):
   367         targettypes = set(targettypes)
   367         targettypes = set(targettypes)
   368         for basketeid, basketname, elements in self._get_basket_info(ueid):
   368         for basketeid, basketname, elements in self._get_basket_info(ueid):
   369             baskettypes = elements.column_types(0)
   369             baskettypes = elements.column_types(0)
   370             # if every elements in the basket can be attached to the
   370             # if every elements in the basket can be attached to the
   371             # edited entity
   371             # edited entity