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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Feb 9 17:22:01 CET 2010


Author: torsten
Date: 2010-02-09 17:21:59 +0100 (Tue, 09 Feb 2010)
New Revision: 1421

Modified:
   wasko/branches/2.0/mpulsweb/lib/validators.py
Log:
* mpulsweb/lib/validators.py (NewUserForm), (LoginCheck): Copied from
  waskaweb



Modified: wasko/branches/2.0/mpulsweb/lib/validators.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/validators.py	2010-02-09 16:11:47 UTC (rev 1420)
+++ wasko/branches/2.0/mpulsweb/lib/validators.py	2010-02-09 16:21:59 UTC (rev 1421)
@@ -94,6 +94,51 @@
         except StandardError, err:
             print "Fehler beim Validieren des Gruppennamens: %s" % err
 
+class LoginCheck(formencode.validators.FancyValidator):
+
+    psql_max = 63
+    login_max = 0
+    valid_char = re.compile(r'^[a-z_0-9]+$')
+
+    messages = {
+        'too_long': u'Die Anmeldekennung darf nicht länger als %(max)i '
+                  u'Zeichen lang sein',
+        'invalid_char': u'Die Anmeldekennung darf nur Kleinbuchstaben, Zahlen oder Unterstriche enthalten',
+        'login_exists': u'Es existiert bereits ein Nutzer mit der Anmeldekennung "%(login)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.
+        user = session['USER_AUTHORIZED']
+        agency = user.getAgency()
+        prefix_str = "ka_%s_" % agency
+        prefix_length = len(prefix_str)
+        self.login_max =  self.psql_max - prefix_length
+        return value.strip()
+
+    def validate_python(self, value, state):
+        if len(value) > (self.login_max):
+            raise formencode.Invalid(self.message("too_long", state, max=self.login_max), value, state)
+        if not self.valid_char.match(value):
+            raise formencode.Invalid(self.message("invalid_char", state), value, state)
+        try:
+            conn, cur = None, None
+            loginname = "_".join(["ka", state.agency, value])
+            fields = {'login': loginname}
+            try:
+                conn = db.getConnection()
+                cur  = conn.cursor()
+                cur.execute("SELECT id FROM ka_benutzer_tbl_view WHERE login = %(login)s", fields)
+                result = cur.fetchone()
+                if result:
+                    raise formencode.Invalid(self.message("login_exists", state, login=value), value, state)
+            finally:
+                db.recycleConnection(conn, cur)
+        except StandardError, err:
+            print "Fehler beim Validieren der Anmeldekennung: %s" % err
+
 class MaxMinInt(formencode.validators.FancyValidator):
 
     """
@@ -151,6 +196,16 @@
     passwd2 = formencode.validators.String() 
     chained_validators = [formencode.validators.FieldsMatch('passwd', 'passwd2')]
 
+class NewUserForm(BaseFormValidator):
+    def __init__(self):
+        formencode.api.set_stdtranslation(domain="FormEncode", languages=["de"])
+
+    firstname = formencode.validators.String(not_empty=True) 
+    lastname = formencode.validators.String(not_empty=True) 
+    login = formencode.All(formencode.validators.String(not_empty=True), LoginCheck()) 
+    activated = formencode.validators.StringBoolean(if_missing=False)
+    newpass = formencode.validators.StringBoolean(if_missing=False)
+
 class NewUserGroupForm(BaseFormValidator):
     name = UserGroupExistsCheck(not_empty=True) 
 



More information about the Mpuls-commits mailing list