Commits

Florent Cayré committed 9ba61d7

translation "value" attribute now uses the same form field and widget as the corresponding translated field in the original entity

  • Participants
  • Parent commits d51260c

Comments (0)

Files changed (2)

test/unittest_i18nfield.py

 
 from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
+from cubicweb.web.views.autoform import InlinedFormField
 
 from cubes.i18nfield.utils import LANGS_BY_CODE, LANGS_BY_EID
 
         tr_fr = req.create_entity('Translation', value=u'salut', lang=self.fr,
                                   of_field=self.card.reverse_i18nfield_of[0])
         self.assertRaises(ValidationError, self.commit)
+
+
+    def _first_inlined_form(self, form):
+        return [field.view.form for field in form.fields
+                if isinstance(field, InlinedFormField)][0]
+
+    def _card_form(self, req, vid):
+        return req.vreg['forms'].select(vid, req, rset=self.card.as_rset())
+
+    def test_formfield(self):
+        '''translation value field and widget classes must be the same as the
+        translated field of the original entity'''
+        req = self.request()
+        req.form['lang_code'] = u'fr'
+        # get card translation value field
+        tr_card_form = self._card_form(req, 'translate_entity')
+        title_form = self._first_inlined_form(tr_card_form)
+        tr_form = self._first_inlined_form(title_form)
+        tr_card_field = tr_form.field_by_name('value', 'subject')
+        # get card title field
+        std_form = self._card_form(req, 'edition')
+        std_field = std_form.field_by_name('title', 'subject')
+        # check field and widget classes
+        self.assertEqual(type(tr_card_field), type(std_field))
+        self.assertEqual(type(tr_card_field.widget), type(std_field.widget))
 from itertools import izip, repeat
 
 from logilab.mtconverter import xml_escape
+from logilab.common.decorators import iclassmethod
 
 from cubicweb import typed_eid, tags
 from cubicweb.selectors import (adaptable, has_permission, is_instance,
-                                match_form_params, one_line_rset, yes)
+                                match_form_params, one_line_rset, match_kwargs)
 from cubicweb.view import EntityView
 from cubicweb.web import uicfg
 from cubicweb.web.action import Action
+from cubicweb.web.form import FieldNotFound
+from cubicweb.web.formfields import guess_field
 from cubicweb.web.formwidgets import HiddenInput
 from cubicweb.web.views.editforms import EditionFormView
 from cubicweb.web.views.autoform import (AutomaticEntityForm as AutoForm,
 from cubes.i18nfield.utils import LANGS_BY_EID
 
 
+_AFF = uicfg.autoform_field
+_AFF_KWARGS = uicfg.autoform_field_kwargs
+_AFS = uicfg.autoform_section
+_AFFK = uicfg.autoform_field_kwargs
+
+
 def lang_from_code(req, code=None):
     code = code or req.form.get('lang_code')
     if code is None:
             title = u'%s (%s)' % (self._cw._(lang.name), action)
             yield self.build_action(title, url)
         yield self.build_action(self._cw._('manage translations'),
-                                self.build_url('', rql='Translation',
-                                               vid='manage_translations'))
+                                self._cw.build_url('', rql='Translation',
+                                                   vid='manage_translations'))
 
     def fill_menu(self, box, menu):
         menu.append_anyway = True
     def limit(self):
         if 'forcedisplay' in self._cw.form:
             return None
-        return 10 # XXX debugging only, use properties instead
+        return self._cw.property_value('navigation.page-size')
 
     def select_rql(self):
         limit = self.limit()
             i18nctx, **kwargs)
 
 
-_AFS = uicfg.autoform_section
-_AFFK = uicfg.autoform_field_kwargs
+class TranslationEditionForm(AutoForm):
+    __select__ = (AutoForm.__select__ & is_instance('Translation')
+                  & match_kwargs('pform'))
+
+    @iclassmethod
+    def field_by_name(cls_or_self, name, role=None, eschema=None):
+        '''make the field used for translation value the same than the one
+        used for the field in the translated entity itself'''
+        if (name, role) == ('value', 'subject'):
+            try:
+                return super(TranslationEditionForm, cls_or_self).field_by_name(
+                    name, role)
+            except FieldNotFound:
+                if eschema is None:
+                    raise
+            i18nfield = cls_or_self.parent_form.edited_entity
+            orig_eschema = i18nfield.i18nfield_of[0].e_schema
+            rschema = orig_eschema.schema.rschema(i18nfield.field_name)
+            tschemas = rschema.targets(orig_eschema, role)
+            fieldcls = _AFF.etype_get(orig_eschema, rschema, role, tschemas[0])
+            kwargs = _AFF_KWARGS.etype_get(orig_eschema, rschema,
+                                           role, tschemas[0])
+            if kwargs is None:
+                kwargs = {}
+            if fieldcls:
+                if not isinstance(fieldcls, type):
+                    return fieldcls # already and instance
+                return fieldcls(name=name, role=role, eidparam=True, **kwargs)
+            field = guess_field(orig_eschema, rschema, role,
+                                eidparam=True, **kwargs)
+            if field is None:
+                raise
+            field.name = name
+            if getattr(field, 'get_format_field', None):
+                field.get_format_field = lambda form: None
+            return field
+        else:
+            return super(TranslationEditionForm, cls_or_self).field_by_name(
+                name, role)
+
 
 def translation_form_lang(form, field):
     lang = lang_from_code(form._cw)