[Mpuls-commits] r1368 - wasko/branches/2.0/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Feb 9 14:12:23 CET 2010


Author: torsten
Date: 2010-02-09 14:12:22 +0100 (Tue, 09 Feb 2010)
New Revision: 1368

Modified:
   wasko/branches/2.0/mpulsweb/lib/validators.py
Log:
* mpulsweb/lib/validators.py: Moved some Validatore from waskaweb to
  base


Modified: wasko/branches/2.0/mpulsweb/lib/validators.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/validators.py	2010-02-09 13:09:43 UTC (rev 1367)
+++ wasko/branches/2.0/mpulsweb/lib/validators.py	2010-02-09 13:12:22 UTC (rev 1368)
@@ -3,6 +3,18 @@
 import formencode
 import re
 
+from pylons.i18n import _
+from mpulsweb.lib.db import db
+
+class ValidatorStateObject:
+
+    def __setattr__(self, key, value):
+        self.__dict__[key] = value
+
+    def __getattr__(self, key):
+        return self.__dict__[key]
+
+
 class BaseFormValidator(formencode.Schema):
     def __init__(self):
         formencode.api.set_stdtranslation(domain="FormEncode", languages=["de"])
@@ -42,9 +54,57 @@
         if len(non_letters) < self.non_letter:
             raise formencode.Invalid(self.message("non_letter", state, non_letter=self.non_letter_literal), value, state)
 
+class UserGroupExistsCheck(formencode.validators.FancyValidator):
+
+    messages = {
+        'group_exists': u'Es existiert bereits eine Benutzergruppe mit dem Namen "%(name)s"!'
+        }
+
+    def _to_python(self, value, state):
+        # _to_python gets run before validate_python.  Here we
+        # strip whitespace off the password, because leading and
+        # trailing whitespace in a password is too elite.
+        return value.strip()
+
+    def validate_python(self, value, state):
+        try:
+            conn, cur = None, None
+            if state:
+                fields = {'name': value, 'id': state.groupid}
+            else:
+                fields = {'name': value, 'id': None}
+            try:
+                conn = db.getConnection()
+                cur  = conn.cursor()
+                cur.execute("SELECT name FROM ka_benutzergruppe_tbl_view WHERE id = %(id)s", fields)
+                result = cur.fetchone()
+
+                if result:
+                    newname = result[0]
+                else: 
+                    newname = result
+
+                if newname != value:
+                    cur.execute("SELECT id FROM ka_benutzergruppe_tbl_view WHERE name = %(name)s", fields)
+                    result = cur.fetchone()
+                    if result:
+                        raise formencode.Invalid(self.message("group_exists", state, name=value), value, state)
+            finally:
+                db.recycleConnection(conn, cur)
+        except StandardError, err:
+            print "Fehler beim Validieren des Gruppennamens: %s" % err
+
+
 class NewPasswordForm(BaseFormValidator):
     passwd = SecurePassword()  
     passwd2 = formencode.validators.String() 
     chained_validators = [formencode.validators.FieldsMatch('passwd', 'passwd2')]
 
+class NewUserGroupForm(BaseFormValidator):
+    name = UserGroupExistsCheck(not_empty=True) 
+
+class EditUserGroupForm(NewUserGroupForm):
+    standin = formencode.ForEach(formencode.validators.Int(), convert_to_list=True)
+
+
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8



More information about the Mpuls-commits mailing list