[reledit] try to be smart at getting a good default vid for relations, factor computing cardinality from reledit and formfields
--- a/utils.py Wed Jul 08 19:11:22 2009 +0200
+++ b/utils.py Wed Jul 08 19:47:36 2009 +0200
@@ -326,4 +326,11 @@
"""
# XXX deprecated, no more necessary
+def compute_cardinality(eschema, rschema, role):
+ if role == 'subject':
+ targetschema = rschema.objects(eschema)[0]
+ return rschema.rproperty(eschema, targetschema, 'cardinality')[0]
+ targetschema = rschema.subjects(eschema)[0]
+ return rschema.rproperty(targetschema, eschema, 'cardinality')[1]
+
--- a/web/formfields.py Wed Jul 08 19:11:22 2009 +0200
+++ b/web/formfields.py Wed Jul 08 19:47:36 2009 +0200
@@ -14,7 +14,7 @@
from yams.constraints import SizeConstraint, StaticVocabularyConstraint
from cubicweb.schema import FormatConstraint
-from cubicweb.utils import ustrftime
+from cubicweb.utils import ustrftime, compute_cardinality
from cubicweb.common import tags, uilib
from cubicweb.web import INTERNAL_FIELD_VALUE
from cubicweb.web.formwidgets import (
@@ -460,7 +460,7 @@
fieldclass = None
if role == 'subject':
targetschema = rschema.objects(eschema)[0]
- card = rschema.rproperty(eschema, targetschema, 'cardinality')[0]
+ card = compute_cardinality(eschema, rschema, role)
help = rschema.rproperty(eschema, targetschema, 'description')
if rschema.is_final():
if rschema.rproperty(eschema, targetschema, 'internationalizable'):
@@ -470,7 +470,7 @@
kwargs.setdefault('initial', get_default)
else:
targetschema = rschema.subjects(eschema)[0]
- card = rschema.rproperty(targetschema, eschema, 'cardinality')[1]
+ card = compute_cardinality(eschema, rschema, role)
help = rschema.rproperty(targetschema, eschema, 'description')
kwargs['required'] = card in '1+'
kwargs['name'] = rschema.type
--- a/web/views/editforms.py Wed Jul 08 19:11:22 2009 +0200
+++ b/web/views/editforms.py Wed Jul 08 19:47:36 2009 +0200
@@ -17,7 +17,7 @@
from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity,
specified_etype_implements, yes)
-from cubicweb.utils import make_uid
+from cubicweb.utils import make_uid, compute_cardinality
from cubicweb.view import EntityView
from cubicweb.common import tags
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, eid_param
@@ -93,19 +93,29 @@
_ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
_defaultlandingzone = u'<img title="%s" class="needsvalidation" src="data/file.gif"/>'
_landingzonemsg = _('double click to edit this field')
+ # default relation vids according to cardinality
+ _one_rvid = 'incontext'
+ _many_rvid = 'csv'
+
+ def _compute_best_vid(self, entity, rtype, role):
+ if compute_cardinality(entity.e_schema,
+ entity.schema.rschema(rtype),
+ role) in '+*':
+ return self._many_rvid
+ return self._one_rvid
def cell_call(self, row, col, rtype=None, role='subject',
- reload=False, # controls reloading the whole page after change
- rvid='textoutofcontext', # vid to be applied to other side of rtype
- escape=True, # depending on the vid, will xml_escape or not
- default=None, # default value
- landing_zone=None # prepend value with a separate html element to click onto
- # (esp. needed when values are links)
+ reload=False, # controls reloading the whole page after change
+ rvid=None, # vid to be applied to other side of rtype
+ escape=True, # depending on the vid, will xml_escape or not
+ default=None, # default value
+ landing_zone=None # prepend value with a separate html element to click onto
+ # (esp. needed when values are links)
):
"""display field to edit entity's `rtype` relation on double-click"""
assert rtype
- rschema = self.schema.rschema(rtype)
entity = self.entity(row, col)
+ rschema = entity.schema.rschema(rtype)
if not default:
default = xml_escape(self.req._('<no value>'))
if rschema.is_final():
@@ -114,6 +124,8 @@
self.w(value)
return
else:
+ if rvid is None:
+ rvid = self._compute_best_vid(entity, rtype, role)
rset = entity.related(rtype, role)
candidate = self.view(rvid, rset, 'null')
if candidate and escape:
@@ -146,6 +158,7 @@
self.w(form.form_render(renderer=renderer))
def _build_relation_form(self, entity, value, rtype, role, row, col, rvid, default, escape, lzone):
+ print 'relation', rvid
divid = 'd%s' % make_uid('%s-%s' % (rtype, entity.eid))
event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : rvid,
'default' : default, 'role' : role, 'escape' : escape, 'lzone' : lzone}