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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Feb 11 08:01:37 CET 2010


Author: torsten
Date: 2010-02-11 08:01:36 +0100 (Thu, 11 Feb 2010)
New Revision: 1481

Modified:
   wasko/branches/2.0/mpulsweb/lib/validators.py
Log:
* mpulsweb/lib/validators.py (TimeCheck), (DateCheck),
(CreateAppointmentForm): Copied from waskaweb.


Modified: wasko/branches/2.0/mpulsweb/lib/validators.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/validators.py	2010-02-10 16:54:18 UTC (rev 1480)
+++ wasko/branches/2.0/mpulsweb/lib/validators.py	2010-02-11 07:01:36 UTC (rev 1481)
@@ -2,6 +2,8 @@
 
 import formencode
 import re
+import datetime
+import time
 
 from pylons import session
 from pylons.i18n import _
@@ -23,6 +25,49 @@
     allow_extra_fields = True
     filter_extra_fields = False 
 
+class DateCheck(formencode.validators.FancyValidator):
+
+    valid_date = re.compile(r'^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$')
+
+    messages = {
+        'invalid_format': u'Bitte geben Sie das Datum im Format "TT.MM.JJJJ" an.',
+        'invalid_date':   u'Bitte geben Sie ein gültiges Datum an.'
+        }
+
+    def _to_python(self, value, state):
+        return value.strip()
+
+    def validate_python(self, value, state):
+        if not self.valid_date.match(value):
+            raise formencode.Invalid(self.message("invalid_format", state), value, state)
+        try:
+            token = value.split('.')
+            testdate = datetime.datetime(int(token[2]), int(token[1]), int(token[0]))
+        except:
+            raise formencode.Invalid(self.message("invalid_date", state), value, state)
+
+class TimeCheck(formencode.validators.FancyValidator):
+
+    valid_date = re.compile(r'^[0-9]{1,2}:[0-9]{1,2}$')
+
+    messages = {
+        'invalid_format': u'Bitte geben Sie die Zeit im Format "HH:MM" an.',
+        'invalid_time':   u'Bitte geben Sie eine gültige Zeit an.'
+        }
+
+    def _to_python(self, value, state):
+        return value.strip()
+
+    def validate_python(self, value, state):
+        if not self.valid_date.match(value):
+            raise formencode.Invalid(self.message("invalid_format", state), value, state)
+        try:
+            token = value.split(':')
+            testtime = datetime.time(int(token[0]), int(token[1]))
+        except:
+            raise formencode.Invalid(self.message("invalid_time", state), value, state)
+
+
 class SecurePassword(formencode.validators.FancyValidator):
 
     min = 8
@@ -151,7 +196,6 @@
         10
         >>> Int.to_python('ten')
         Traceback (most recent call last):
-            ...
         Invalid: Please enter an integer value
     """
 
@@ -207,6 +251,13 @@
     activated = formencode.validators.StringBoolean(if_missing=False)
     newpass = formencode.validators.StringBoolean(if_missing=False)
 
+class CreateAppointmentForm(BaseFormValidator):
+    start_date = formencode.All(formencode.validators.String(not_empty=True), DateCheck())
+    start_time = formencode.All(formencode.validators.String(not_empty=True), TimeCheck())
+    end_date   = DateCheck()
+    end_time   = TimeCheck()
+    title      = formencode.All(formencode.validators.String(not_empty=True), StringTooLong(128))
+
 class EditUserForm(NewUserForm):
     login = None
 



More information about the Mpuls-commits mailing list