Commits

Andreas Madsack  committed a988883

added UUID-field (original commit from Gentle!)

  • Participants
  • Parent commits 74e0fb6

Comments (0)

Files changed (2)

File wtforms/ext/sqlalchemy/orm.py

         field_args['validators'].append(validators.MacAddress())
         return f.TextField(**field_args)
 
+    @converts('dialects.postgresql.base.UUID')
+    def conv_PGUuid(self, field_args, **extra):
+        field_args.setdefault('label', u'UUID')
+        field_args['validators'].append(validators.UUID())
+        return f.TextField(**field_args)
 
 def model_fields(model, only=None, exclude=None, field_args=None, converter=None):
     """

File wtforms/validators.py

         super(URL, self).__call__(form, field)
 
 
+class UUID(Regexp):
+    """
+    Validates a Mac address.
+
+    :param message:
+        Error message to raise in case of a validation error.
+    """
+    def __init__(self, message=None):
+
+        super(UUID, self).__init__(
+            r'^[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$', message=message)
+
+    def __call__(self, form, field):
+        if self.message is None:
+            self.message = field.gettext(u'Invalid UUID.')
+
+        super(UUID, self).__call__(form, field)
+
+
 class AnyOf(object):
     """
     Compares the incoming data to a sequence of valid inputs.