[Mpuls-commits] r3320 - base/trunk/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Aug 3 17:05:54 CEST 2010


Author: torsten
Date: 2010-08-03 17:05:53 +0200 (Tue, 03 Aug 2010)
New Revision: 3320

Modified:
   base/trunk/mpulsweb/lib/validators.py
Log:
* mpulsweb/lib/validators.py (TimeDeltaCheck.conv_dates): New Validator to check the timedelta between to given dates.



Modified: base/trunk/mpulsweb/lib/validators.py
===================================================================
--- base/trunk/mpulsweb/lib/validators.py	2010-08-03 13:31:51 UTC (rev 3319)
+++ base/trunk/mpulsweb/lib/validators.py	2010-08-03 15:05:53 UTC (rev 3320)
@@ -226,7 +226,63 @@
         if not self.month_style in ('dd/mm/yyyy', 'mm/dd/yyyy', 'dd.mm.jjjj'):
             raise TypeError("Bad month_style: %r" % self.month_style)
 
+class TimeDeltaCheck(FormValidator):
 
+    """Tests that the given datetimes do not have a too large timedelta.
+    """
+
+    field_names = None
+    validate_partial_form = False # do not run is a prior test failed 
+    __unpackargs__ = ('*', 'field_names')
+    days = None
+
+    messages = {
+        'toomuchdays': _(u"Die Datumsangaben dürfen maximal %(value)s Tage auseinander liegen."),
+        'notDict': _("Fields should be a dictionary"),
+        }
+
+    def __init__(self, *args, **kw):
+        super(FormValidator, self).__init__(*args, **kw)
+        if len(self.field_names) < 2:
+            raise TypeError("FieldsMatch() requires at least two field names")
+
+    def conv_dates(self, dict):
+        # Dates are already converted to datetime objects here. as we want
+        # german representation in error messages convert the values back to
+        # german format
+        for key, value in dict.iteritems():
+            if isinstance(value, datetime.date):
+                value = format_date(value) 
+                dict[key] = value
+        return dict
+
+    def validate_python(self, field_dict, state):
+        try:
+            begin = field_dict[self.field_names[0]]
+            end = field_dict[self.field_names[1]]
+        except TypeError:
+            # Generally because field_dict isn't a dict
+            raise Invalid(self.message('notDict', state), field_dict, state)
+        except KeyError:
+            ref = ''
+        errors = {}
+        if begin > end:
+            begin, end = end, begin
+        td = end - begin
+        if self.days:
+            if td.days > self.days:
+                errors[self.field_names[0]] = self.message('toomuchdays', state, value=self.days) 
+                errors[self.field_names[1]] = self.message('toomuchdays', state, value=self.days)
+        if errors:
+            error_list = errors.items()
+            error_list.sort()
+            error_message = '<br>\n'.join(['%s: %s' % (name, value)
+                                           for name, value in error_list])
+            raise formencode.Invalid(error_message, self.conv_dates(field_dict), state,
+                                     error_dict=errors)
+
+
+
 class DateCheck(formencode.validators.FancyValidator):
 
     valid_date = re.compile(r'^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$')
@@ -252,7 +308,6 @@
             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}$')



More information about the Mpuls-commits mailing list