Beispiel #1
0
#
# You should have received a copy of the GNU General Public License
# along with ECQuiz; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

""" A simple validator for lists. Currently not used. """

import re

from zope.interface import implements
from Products.validation.interfaces.IValidator import IValidator

from tools import registerValidatorLogged

listValidatorRe = re.compile(ur"\s*(?P<nonEmpty>\[\s*'[^']+'(\s*,\s*'[^']+'\s*)*\s*\])|(?P<empty>\[\s*\])\s*")

class ListValidator:
    """ A simple validator for lists. """

    implements(IValidator)

    def __init__(self, name):
        self.name = name
    def __call__(self, value, *args, **kwargs):
        if (not listValidatorRe.match(value)):
            return """Validation failed"""
        return 1

# Register this validator in Zope
registerValidatorLogged(ListValidator, 'isList')
Beispiel #2
0
    
    def __init__(self, name):
        self.name = name
    
    def __call__(self, value, *args, **kwargs):
        # First we have to find out if this is meant to be text/html
        instance = kwargs.get('instance', None)
        field    = kwargs.get('field',    None)
        if instance and field:
            request     = getattr(instance, 'REQUEST', None)
            formatField = "%s_text_format" % field.getName()
            if request and ( request.get(formatField, 
                '').strip().lower() == 'text/html' ):
                # Aha! The format was set to 'text/html'
                if isinstance(value, str):
                    string = '<a>' + value + '</a>'
                else:
                    string = u'<a>' + value + u'</a>'
                try:
                    doc = parseString(string)
                    doc.unlink() # Destroy the document object so the garbage
                    # collector can delete it
                    return 1
                except:
                    return """Please use XHTML conformant markup."""
        # The format was not 'text/html' or something went wrong
        return True

# Register this validator in Zope
registerValidatorLogged(XMLValidator, 'isXML')