Стефани обнови решението на 18.04.2014 16:08 (преди над 10 години)
+import re
+VALID_MAIL = '[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@[a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{,2})?'
+VALID_MAIL_PREFIX = '[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,200}@'
+VALID_HOST = '@[a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{,2})?'
+VALID_LOCAL_PHONE = '0[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){6,10}\d?'
+VALID_INTERNATIONAL_PHONE = '(00|\+)[1-9]\d{,2}([-\s\(\)]{,2}\d){6,10}\d?'
+IP = '^(\d\.|[1-9]\d\.|1\d\d\.|2[0-5][0-6]\.){3}(\d|[1-9]\d|1\d\d|2[0-5][0-6])\Z'
+
+
+class Validations:
+
+ @classmethod
+ def is_phone(self, value):
+ inner = '([-\s\(\)]{,2}\d){6,10}\d?\Z'
+ local = "^0[-\s\(\)]{,2}[1-9]" + inner
+ international = "^(00|\+)[1-9]\d{,2}" + inner
+ return bool(re.match(international, value))\
+ or bool(re.match(local, value))
+
+ @classmethod
+ def is_hostname(self, value):
+ return bool(re.match('^[a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]?\.[a-zA-Z]{2,3}(\.[a-zA-Z]{,2})?\Z', value))
+
+ @classmethod
+ def is_email(self, value):
+ return bool(re.match(VALID_MAIL_PREFIX, value)) and \
+ self.is_hostname(value.split('@', 1)[1])
+
+ @classmethod
+ def is_ip_address(self, value):
+ return bool(re.match(IP, value))
+
+ @classmethod
+ def is_integer(self, value):
+ return bool(re.match('^-?(0|[1-9][0-9]*)\Z', value))
+
+ @classmethod
+ def is_number(self, value):
+ return bool(re.match('^-?(0[1-9]*|[1-9][0-9]*).?[0-9]*\Z', value))
+
+ @classmethod
+ def is_date(self, value):
+ return bool(re.match(
+ '^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])\Z',
+ value))
+
+ @classmethod
+ def is_time(self, value):
+ return bool(re.match('^([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)\Z', value))
+
+ @classmethod
+ def is_datetime(self, value):
+ split_T = value.split('T', 1)
+ split_S = value.split(' ', 1)
+ return self.is_date(split_T[0]) and self.is_time(split_T[1]) or \
+ self.is_date(split_S[0]) and self.is_time(split_S[1])
+
+
+class PrivacyFilter:
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, value):
+ self.value = value
+
+ def filtered(self):
+ working_copy = self.value
+
+ while re.search(VALID_MAIL, working_copy):
+ mail = re.search(VALID_MAIL, working_copy).group()
+ host = re.search(VALID_HOST, mail).group()
+ prefix = re.search(
+ '[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{,2}', mail).group()
+ length = len(re.search(VALID_MAIL_PREFIX, mail).group())
+ if self.partially_preserve_email_username and length < 6:
+ target = '[FILTERED]' + host
+ elif self.partially_preserve_email_username:
+ target = prefix + '[FILTERED]' + host
+ elif self.preserve_email_hostname:
+ target = '[FILTERED]' + host
+ else:
+ target = '[EMAIL]'
+ working_copy = working_copy.replace(mail, target)
+
+ while re.search(VALID_LOCAL_PHONE, working_copy) or \
+ re.search(VALID_INTERNATIONAL_PHONE, working_copy):
+ local_phone = re.search(VALID_LOCAL_PHONE, working_copy)
+ international_phone = re.search(
+ VALID_INTERNATIONAL_PHONE, working_copy)
+ if local_phone:
+ working_copy = working_copy.replace(
+ local_phone.group(), '[PHONE]')
+ elif not self.preserve_phone_country_code and international_phone:
+ working_copy = working_copy.replace(
+ international_phone.group(), '[PHONE]')
+ else:
+ prefix = re.search(
+ '(00|\+)[1-9]\d{,2}', international_phone.group())
+ target = prefix.group() + " [FILTERED]"
+ working_copy = working_copy.replace(
+ international_phone.group(), target)
+ return working_copy