Божидар обнови решението на 23.04.2014 14:18 (преди над 10 години)
+import re
+
+
+class Validations:
+ @classmethod
+ def is_hostname(cls, value):
+ domain = '([a-zA-Z0-9]{1}([a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
+ tld = '(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})$'
+ pattern = re.compile('^(' + domain + '\.)*' + domain + tld)
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_email(cls, value):
+ if bool(re.search('@{1}', value)) is False:
+ return False
+ email_pattern = r'^([a-zA-Z0-9]){1}([a-zA-Z0-9]|_|\+|\.|-){0,200}$'
+ s = value.split('@')
+ return bool(re.search(email_pattern, s[0])) and cls.is_hostname(s[1])
+
+ @classmethod
+ def is_ip_address(cls, value):
+ number_range = '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
+ pattern = re.compile('^(' + number_range + '\.){3}' + number_range + '$')
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_number(cls, value):
+ pattern = r'^(-)?(0|[1-9][0-9]*)(\.[0-9]+)?$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_integer(cls, value):
+ pattern = r'^(-)?(0|[1-9][0-9]*)$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_date(cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_time(cls, value):
+ pattern = r'^([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_datetime (cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(\s|T|-)([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_phone(cls, value):
+ interval_pattern = r"((\,|\s|\(|\)){0,2})"
+ base_digit_pattern = r'(' + interval_pattern + '?[0-9])'
+ local_pattern = re.compile(r'^0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}$')
+ international_pattern = re.compile(r'^(00|'+')'+ \
+ '?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11}$')
+ return bool(re.search(local_pattern, value)) or bool(re.search(international_pattern, value))
+
+
+class PrivacyFilter:
+ domain = r'(?:[a-zA-Z0-9]{1}(?:[a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
+ tld = r'(?:\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})'
+ email_pattern = re.compile(r'(\b(?:[a-zA-Z0-9]){1}(?:[a-zA-Z0-9]|\s|\+|\.|-){0,200}' + '@' + '(?:' + domain + '\.)*' + domain + tld + r'\b)')
+ interval_pattern = r'(?:(?:\,|\s|\(|\)){0,2})'
+ base_digit_pattern = r'(?:' + interval_pattern + '?[0-9])'
+ local_pattern = re.compile(r'0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}')
+ international_pattern = re.compile(r'((?:00|\+)'+ \
+ '?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11})')
+
+ def __init__(self, text):
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+ self.text = text
+
+ def filtered(self):
+ self.filter()
+ return self.text
+
+ def filter(self):
+ emails_to_be_filtered = re.findall(self.email_pattern, self.text)
+ if self.preserve_email_hostname == False and self.partially_preserve_email_username == False:
+ self.text = re.sub(self.email_pattern, "[EMAIL]", self.text)
+ elif self.partially_preserve_email_username == True:
+ for email in emails_to_be_filtered:
+ email_parts = email[0].split('@')
+ if len(email_parts[0]) >= 6:
+ email_parts[0] = email_parts[0][0:3] + "[FILTERED]@"
+ self.text = self.text.replace(email[0], email_parts[0] + email_parts[1])
+ else:
+ self.text = self.text.replace(email[0], "[FILTERED]@" + email_parts[1])
+ else:
+ for email in emails_to_be_filtered:
+ self.text = self.text.replace(email[0], re.sub('([a-zA-Z0-9]|_|\.|-){1,201}@',"[FILTERED]@",email[0]))
+
+ self.text = re.sub(self.local_pattern, "[PHONE]", self.text)
+ phones_to_be_filtered = re.findall(self.international_pattern, self.text)
+ if phones_to_be_filtered != ['']:
+ if self.preserve_phone_country_code == True:
+ for phone in phones_to_be_filtered:
+ phone_digits = ''.join(digit for digit in phone[0] if digit.isdigit())
+ code = ""
+ if phone_digits[0] is '0':
+ phone_digits = phone_digits[2:]
+ code = "00"
+ else:
+ code = "+"
+ if len(phone_digits) >= 9:
+ code = code + phone_digits[0:3]
+ elif len(phone_digits) == 8:
+ code = code + phone_digits[0:2]
+ else:
+ code = code + phone_digits[0]
+ self.text = self.text.replace(phone[0], code + " [FILTERED]")
+ else:
+ for phone in phones_to_be_filtered:
+ self.text = self.text.replace(phone[0],"[PHONE]")
+