No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

regexvarchar.py 1.2KB

12345678910111213141516171819202122232425262728293031323334
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from .varchar import DataHandler as VarcharDataHandler
  4. class DataHandler(VarcharDataHandler):
  5. help = 'String field validated with a regex. Takes two options : max_length and regex'
  6. base_type = 'char'
  7. ## @brief A string field validated by a regex
  8. # @param regex str : a regex string (passed as argument to re.compile())
  9. # @param max_length int : the max length for this field (default : 10)
  10. # @param **kwargs
  11. def __init__(self, regex='', max_length=10, **kwargs):
  12. self.regex = regex
  13. self.compiled_re = re.compile(regex) # trigger an error if invalid regex
  14. super(self.__class__, self).__init__(max_length=max_length, **kwargs)
  15. def _check_data_value(self, value):
  16. error = None
  17. if not self.compiled_re.match(value):
  18. value = ''
  19. error = TypeError('"%s" doesn\'t match the regex "%s"' % (value, self.regex))
  20. return value, error
  21. def can_override(self, data_handler):
  22. if not super().can_override(data_handler):
  23. return False
  24. if data_handler.max_length != self.max_length:
  25. return False
  26. return True