Back to index

plone3  3.1.7
ExpressionValidator.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # TALESField - Field with TALES support for Archetypes
00004 # Copyright (C) 2005 Sidnei da Silva, Daniel Nouri and contributors
00005 #
00006 # This program is free software; you can redistribute it and/or modify
00007 # it under the terms of the GNU General Public License as published by
00008 # the Free Software Foundation; either version 2 of the License, or
00009 # (at your option) any later version.
00010 #
00011 # This program is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 # GNU General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU General Public License
00017 # along with this program; if not, write to the Free Software
00018 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 #
00020 ##############################################################################
00021 """
00022 $Id: __init__.py,v 1.2 2005/02/26 17:56:10 sidnei Exp $
00023 """
00024 
00025 from Products.validation.interfaces.IValidator import IValidator
00026 from Products.PageTemplates.Expressions import getEngine
00027 
00028 class ExpressionValidator:
00029     """ Validator for TALES Expressions
00030 
00031     Basically, if the expression compiles it's a valid expression,
00032     otherwise it's invalid and you get a message saying that the
00033     expression has errors.
00034 
00035     
00036     >>> val=ExpressionValidator('python: int(value) == 5')
00037     >>> class C:i=1
00038     >>> c=C()
00039     >>> val(5,c) is True
00040     True
00041 
00042     now lets fail a test
00043     >>> val(4,c) 
00044     'validation failed, expr was:python: int(value) == 5'
00045     
00046     It is also possible to specify the error string
00047     
00048     >>> val=ExpressionValidator('python: int(value) == 5', 'value doesnt match %(value)s')
00049     >>> val(4,c) 
00050     'value doesnt match 4'
00051     
00052        
00053     """
00054 
00055     __implements__ = (IValidator,)
00056 
00057     name = 'talesexpressionvalidator'
00058     
00059     def __init__(self,expression=None,errormsg=None):
00060         self.expression=expression
00061         self.errormsg=errormsg
00062         if expression:
00063             self.compiledExpression=getEngine().compile(expression)
00064 
00065     def __call__(self, value, instance, *args, **kwargs):
00066         kw={
00067            'here':instance,
00068            'object':instance,
00069            'instance':instance,
00070            'value':value,
00071            'args':args,
00072            'kwargs':kwargs,
00073            }
00074            
00075         context=getEngine().getContext(kw)
00076         res=self.compiledExpression(context)
00077         
00078         if res:
00079             return True
00080         else:
00081             if self.errormsg:
00082                 return self.errormsg % kw
00083             else:
00084                 return 'validation failed, expr was:'+self.expression
00085            
00086 
00087 #validation.register(TALESExpressionValidator())