Ticket #7589: monkeypatch.py

File monkeypatch.py, 3.2 KB (added by miohtama, 5 years ago)

monkeypatch to fix the issue

Line 
1"""
2
3    Fix KSS validation issues
4   
5    See: https://dev.plone.org/plone/ticket/7589#comment:11
6
7"""
8from zope.interface import implements
9
10from Acquisition import aq_inner
11from Products.CMFCore.utils import getToolByName
12from Products.CMFPlone import PloneMessageFactory as _
13
14from plone.app.kss.plonekssview import PloneKSSView
15from plone.app.kss.interfaces import IPloneKSSView
16
17SKIP_KSSVALIDATION_FIELDTYPES = ('image', 'file')
18
19from zope.deprecation import deprecated
20
21missing_uid_deprecation = \
22"This view does not provide a KSS instance UID as required. Falling back to "
23"the global context on inline-editing will be removed in Plone 3.5. Please "
24"update your templates."
25
26import archetypes.kss.validation
27from archetypes.kss.validation import ValidationView
28
29def kssValidateField(self, fieldname, value, uid=None):
30    '''Validate a given field
31    '''
32    # validate the field, actually
33
34    if uid is not None:
35        rc = getToolByName(aq_inner(self.context), 'reference_catalog')
36        instance = rc.lookupObject(uid)
37    else:
38        deprecated(ValidationView, missing_uid_deprecation)
39        instance = aq_inner(self.context)
40
41    field = instance.getField(fieldname)
42    if field.type in SKIP_KSSVALIDATION_FIELDTYPES:
43        return self.render()
44   
45    # full_form_validation = False --> notify validators
46    # that we don't have full form POST context available
47    # and emptiness validators should not give meaningful
48    # errors
49    error = field.validate(value, instance, {}, full_form_validation=False)
50    # XXX
51    if isinstance(error, str):
52        error = error.decode('utf', 'replace')
53    # replace the error on the page
54    self.getCommandSet('atvalidation').issueFieldError(fieldname, error)
55    return self.render()
56
57ValidationView.kssValidateField = kssValidateField
58
59from Products.validation.validators import EmptyValidator
60
61_marker = []
62
63def __empty_validator_call__(self, value, *args, **kwargs):
64    isEmpty  = kwargs.get('isEmpty', False)
65    instance = kwargs.get('instance', None)
66    field    = kwargs.get('field', None)
67   
68    # Ignore empty validator when called in KSS context
69    # https://dev.plone.org/plone/ticket/7589#comment
70    full_form = kwargs.get("full_form_validation", True)
71
72    # XXX: This is a temporary fix. Need to be fixed right for AT 2.0
73    #      content_edit / BaseObject.processForm() calls
74    #      widget.process_form a second time!
75    if instance and field and full_form:
76        widget  = field.widget
77        request = getattr(instance, 'REQUEST', None)
78        if request and request.form:
79            form   = request.form
80            result = widget.process_form(instance, field, form,
81                                         empty_marker=_marker,
82                                         emptyReturnsMarker=True)
83            if result is _marker or result is None:
84                isEmpty = True
85
86    if isEmpty:
87        return True
88    elif value == '' or value is None:
89        return True
90    else:
91        if getattr(self, 'showError', False):
92            return ("Validation failed(%(name)s): '%(value)s' is not empty." %
93                   { 'name' : self.name, 'value': value})
94        else:
95            return False
96
97EmptyValidator.EmptyValidator.__call__ = __empty_validator_call__
98