Rob Crittenden wrote:
Rob Crittenden wrote:
This started as a problem in allowing leading/trailing whitespaces on
primary keys. In nearly every command other than add query is True so
all rules were ignored on the primary key. This meant that to enforce
whitespace we would need to define a validator for each one.

I decided instead to set self.all_rules to just the class rules if query
== True. So the minimum set of validators will be executed against each
type but param-specific validators will only run on add.

I talked to Martin about this a bit this morning. My original intention
was to make some pretty invasive changes related to query and he talked
me out of them. He felt that in anything other than an add the
validators shouldn't be run. We compromised on letting Paramter-specific
validators be run.

This has pretty big implications on primary keys so test carefully.

https://fedorahosted.org/freeipa/ticket/1285
https://fedorahosted.org/freeipa/ticket/1286
https://fedorahosted.org/freeipa/ticket/1287

rob

self-NACK, found a problem.

rob

Add only to Str class, fixed pylint error.

rob
>From 98fc57943b0fcf933ace572ba1e09b9f2570e961 Mon Sep 17 00:00:00 2001
From: Rob Crittenden <rcrit...@redhat.com>
Date: Fri, 24 Jun 2011 14:32:57 -0400
Subject: [PATCH] Enforce class rules when query=True, continue to not run validators.

This started as a problem in allowing leading/trailing whitespaces
on primary keys. In nearly every command other than add query is True
so all rules were ignored on the primary key. This meant that to
enforce whitespace we would need to define a validator for each one.

I decided instead to set self.all_rules to just the class rules if
query == True. So the minimum set of validators will be executed
against each type but param-specific validators will only run on add.

https://fedorahosted.org/freeipa/ticket/1285
https://fedorahosted.org/freeipa/ticket/1286
https://fedorahosted.org/freeipa/ticket/1287
---
 ipalib/parameters.py |   23 +++++++++++++++++++----
 1 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index ee660848bb5931601ab0494a007b45ce711604bd..5d414458a9a8544b2b26903307fd8823407a1189 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -432,7 +432,10 @@ class Param(ReadOnly):
         # Check that all the rules are callable
         self.class_rules = tuple(class_rules)
         self.rules = rules
-        self.all_rules = self.class_rules + self.rules
+        if self.query:
+            self.all_rules = self.class_rules
+        else:
+            self.all_rules = self.class_rules + self.rules
         for rule in self.all_rules:
             if not callable(rule):
                 raise TypeError(
@@ -727,8 +730,6 @@ class Param(ReadOnly):
                 else:
                     raise RequirementError(name=self.name)
             return
-        if self.query:
-            return
         if self.multivalue:
             if type(value) is not tuple:
                 raise TypeError(
@@ -1125,7 +1126,7 @@ class Data(Param):
         ('pattern', (basestring,), None),
         ('pattern_errmsg', (basestring,), None),
     )
-    
+
     re = None
     re_errmsg = None
 
@@ -1242,6 +1243,10 @@ class Str(Data):
     Also see the `Bytes` parameter.
     """
 
+    kwargs = Data.kwargs + (
+        ('noextrawhitespace', bool, True),
+    )
+
     type = unicode
     type_error = _('must be Unicode text')
 
@@ -1268,6 +1273,16 @@ class Str(Data):
             error=ugettext(self.type_error),
         )
 
+    def _rule_noextrawhitespace(self, _, value):
+        """
+        Do not allow leading/trailing spaces.
+        """
+        assert type(value) is unicode
+        if self.noextrawhitespace is False: #pylint: disable=E1101
+            return
+        if len(value) != len(value.strip()):
+            return _('Leading and trailing spaces are not allowed')
+
     def _rule_minlength(self, _, value):
         """
         Check minlength constraint.
-- 
1.7.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to