Commit d31316d3 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

Migrate to cw 3.25

and drop monkey-patches which are hence no more necessary.

Closes #17106806
parent ba6a36fa1ce7
......@@ -14,7 +14,7 @@ description = 'Data Exchange Standard for Archival'
web = 'http://www.cubicweb.org/project/%s' % distname
__depends__ = {
'cubicweb': '>= 3.24, < 3.25',
'cubicweb': '>= 3.25, < 3.26',
'six': '>= 1.4.0',
'cubicweb-eac': None,
'cubicweb-skos': '>= 0.12.1',
......
......@@ -14,115 +14,10 @@
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
from logilab.common.decorators import monkeypatch
# hack to avoid crash when cube is installed in a directory specified by CW_CUBES_PATH, because
# `RegistrableInstance` attempt to compute its __module__, but in that case the file isn't in the
# python path. This should go away once cubes become regular packages.
import traceback
from logilab.common import registry
from cubicweb import rtags
from cubicweb.entity import Entity
@monkeypatch(registry.RegistrableInstance, methodname='__new__')
@staticmethod
def __new__(cls, *args, **kwargs):
"""Add a __module__ attribute telling the module where the instance was
created, for automatic registration.
"""
module = kwargs.pop('__module__', None)
self = super(registry.RegistrableInstance, cls).__new__(cls)
if module is None:
filepath = traceback.extract_stack(limit=2)[0][0]
module = registry._modname_from_path(filepath)
self.__module__ = module
return self
@monkeypatch(rtags.RelationTags)
def __init__(self, __module__=None):
super(rtags.RelationTags, self).__init__()
self._tagdefs = {}
Entity.cw_skip_copy_for.append(('container', 'subject'))
Entity.cw_skip_copy_for.append(('container', 'object'))
Entity.cw_skip_copy_for.append(('clone_of', 'subject'))
Entity.cw_skip_copy_for.append(('clone_of', 'object'))
# auto-configuration of custom fields ##############################################################
# (https://www.cubicweb.org/ticket/14474840)
# other part in views/patches
from cubicweb.web import formfields # noqa
@monkeypatch(formfields) # noqa
def guess_field(eschema, rschema, role='subject', req=None, **kwargs):
"""This function return the most adapted field to edit the given relation
(`rschema`) where the given entity type (`eschema`) is the subject or object
(`role`).
The field is initialized according to information found in the schema,
though any value can be explicitly specified using `kwargs`.
"""
fieldclass = None
rdef = eschema.rdef(rschema, role)
if role == 'subject':
targetschema = rdef.object
if rschema.final:
if rdef.get('internationalizable'):
kwargs.setdefault('internationalizable', True)
else:
targetschema = rdef.subject
card = rdef.role_cardinality(role)
composite = getattr(rdef, 'composite', None)
kwargs['name'] = rschema.type
kwargs['role'] = role
kwargs['eidparam'] = True
# don't mark composite relation as required, we want the composite element
# to be removed when not linked to its parent
kwargs.setdefault('required', card in '1+' and composite != formfields.neg_role(role))
if role == 'object':
kwargs.setdefault('label', (eschema.type, rschema.type + '_object'))
else:
kwargs.setdefault('label', (eschema.type, rschema.type))
kwargs.setdefault('help', rdef.description)
if rschema.final:
fieldclass = kwargs.pop('fieldclass', formfields.FIELDS[targetschema])
if issubclass(fieldclass, formfields.StringField):
if eschema.has_metadata(rschema, 'format'):
# use RichTextField instead of StringField if the attribute has
# a "format" metadata. But getting information from constraints
# may be useful anyway...
for cstr in rdef.constraints:
if isinstance(cstr, formfields.StaticVocabularyConstraint):
raise Exception('rich text field with static vocabulary')
return formfields.RichTextField(**kwargs)
# init StringField parameters according to constraints
for cstr in rdef.constraints:
if isinstance(cstr, formfields.StaticVocabularyConstraint):
kwargs.setdefault('choices', cstr.vocabulary)
break
for cstr in rdef.constraints:
if isinstance(cstr, formfields.SizeConstraint) and cstr.max is not None:
kwargs['max_length'] = cstr.max
return fieldclass(**kwargs)
if issubclass(fieldclass, formfields.FileField):
if req:
aff_kwargs = req.vreg['uicfg'].select('autoform_field_kwargs', req)
else:
aff_kwargs = formfields._AFF_KWARGS
for metadata in formfields.KNOWN_METAATTRIBUTES:
metaschema = eschema.has_metadata(rschema, metadata)
if metaschema is not None:
metakwargs = aff_kwargs.etype_get(eschema, metaschema, 'subject')
kwargs['%s_field' % metadata] = formfields.guess_field(eschema, metaschema,
req=req, **metakwargs)
return fieldclass(**kwargs)
return formfields.RelationField.fromcardinality(card, **kwargs)
......@@ -24,9 +24,6 @@ from cubicweb.web import formfields as ff
# XXX alias to avoid side effect: uicfg will become our uicfg submodule. Not necessary with 3.23?
from cubicweb.web.views import uicfg as cwuicfg
# XXX trigger monkey patches necessary to import this module, until we use a recent version of
# lgc.registry supporting instantiation of rtags with explicit __module__ argument
from .. import site_cubicweb # noqa
from ..xsd import XSDM_MAPPING
from ..xsd2uicfg import FIRST_LEVEL_ETYPES
from .widgets import SEDAMetaField
......
......@@ -1117,7 +1117,7 @@ class DataObjectSimplifiedAutomaticEntityForm(widgets.SimplifiedAutomaticEntityF
& simplified_profile())
def inlined_form_views(self):
views = super(DataObjectSimplifiedAutomaticEntityForm, self).inlined_form_views()
views = list(super(DataObjectSimplifiedAutomaticEntityForm, self).inlined_form_views())
ref_forms = [v.form for v in views if v.rtype == 'seda_data_object_reference_id']
if ref_forms: # may be empty if user has no write access
ref_form = ref_forms[0]
......
......@@ -46,90 +46,3 @@ def __init__(self, *args, **kwargs):
# this js file contains a custom implementation of addInlineCreationForm that propage
# sedaContainerEID
autoform.AutomaticEntityForm.needs_js += ('cubes.seda.form.js',)
# fix unexpected redirect after clicking on cancel in reledit ######################################
# (https://www.cubicweb.org/ticket/13120795)
from cubicweb.web.views import reledit # noqa
orig_build_form = reledit.AutoClickAndEditFormView._build_form
@monkeypatch(reledit.AutoClickAndEditFormView)
def _build_form(*args, **kwargs):
form, renderer = orig_build_form(*args, **kwargs)
for button in getattr(form, 'form_buttons', ()):
if button.label.endswith('cancel') and 'class' in button.attrs:
button.attrs['class'] = button.attrs['class'].replace('cwjs-edition-cancel', '')
return form, renderer
# auto-configuration of custom fields ##############################################################
# (https://www.cubicweb.org/ticket/14474840)
# other part in site_cubicweb
from cubicweb.web import formfields # noqa
from cubicweb.web.views import forms # noqa
@monkeypatch(forms.EntityFieldsForm, methodname='field_by_name')
@forms.iclassmethod
def field_by_name(cls_or_self, name, role=None, eschema=None):
"""return field with the given name and role. If field is not explicitly
defined for the form but `eclass` is specified, guess_field will be
called.
"""
try:
return super(forms.EntityFieldsForm, cls_or_self).field_by_name(name, role)
except forms.form.FieldNotFound:
if eschema is None or role is None or name not in eschema.schema:
raise
rschema = eschema.schema.rschema(name)
# XXX use a sample target type. Document this.
tschemas = rschema.targets(eschema, role)
fieldclass = cls_or_self.uicfg_aff.etype_get(
eschema, rschema, role, tschemas[0])
kwargs = cls_or_self.uicfg_affk.etype_get(
eschema, rschema, role, tschemas[0])
if kwargs is None:
kwargs = {}
if fieldclass:
if not isinstance(fieldclass, type):
return fieldclass # already an instance
kwargs['fieldclass'] = fieldclass
if isinstance(cls_or_self, type):
req = None
else:
req = cls_or_self._cw
field = formfields.guess_field(eschema, rschema, role, req=req, eidparam=True, **kwargs)
if field is None:
raise
return field
# allow configuration of inlined form renderer using a class attribute #############################
# (https://www.cubicweb.org/ticket/15755515)
from logilab.common.decorators import cached # noqa
autoform.InlineEntityEditionFormView.form_renderer_id = 'inline'
@monkeypatch(autoform.InlineEntityEditionFormView, methodname='form')
@property
@cached
def form(self):
entity = self._entity()
form = self._cw.vreg['forms'].select('edition', self._cw,
entity=entity,
formtype='inlined',
form_renderer_id=self.form_renderer_id,
copy_nav_params=False,
mainform=False,
parent_form=self.pform,
**self.cw_extra_kwargs)
if self.pform is None:
form.restore_previous_post(form.session_key())
self.add_hiddens(form, entity)
return form
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment