Кирил обнови решението на 21.04.2014 17:14 (преди над 10 години)
+def text_to_email_filtered(email, hostname, username):
+
+ if not bool(re.search(r'^[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})+)$)$', email)):
+ return email
+
+ email_to_list = email.split(' ')
+ i = 0
+ value = email_to_list
+ for val in value:
+ match = re.search(
+ r'^[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})+)$)$', val)
+
+ if match is not None and hostname == False and username == False:
+ value[i] = '[EMAIL] '
+
+ elif match is not None and hostname == True and username == False:
+ v = value[i].split('@')
+ value[i] = '[FILTERED]' + '@' + v[1] + ' '
+
+ elif match is not None and username == True:
+ v = value[i].split('@')
+ if len(v[0]) < 7:
+ value[i] = '[FILTERED]' + '@' + v[1] + ' '
+ else:
+ value[i] = str(value[i][0]) + str(value[i][1]) + \
+ str(value[i][2]) + '[FILTERED]' + \
+ '@' + v[1] + ' '
+
+ else:
+ value[i] = val + ' '
+ i = i + 1
+ new_text = ''.join(value)
+ new_text = new_text[:-1]
+ return new_text
+
+
+def text_to_tel(text, code):
+ phone = re.compile(
+ r'(\+[1-9]\d{,2}|00[1-9]\d{,2}|0[1-9]{,3})[ -()]{,2}([ -()]{,2}[0-9][ -()]{,2}){6,10}[0-9]')
+
+ if code == False:
+ iterator = phone.finditer(text)
+ for match in iterator:
+ # print(match.group(0))
+ start, end = match.span()
+ text = text[:start] + '[PHONE]' + text[end:]
+
+ if code == True:
+ iterator = phone.finditer(text)
+ for match in iterator:
+ start, end = match.span()
+
+ p = re.search('^(\+[1-9]\d{,2}|00[1-9]\d{,2})', match.group(0))
+
+ if p is not None:
+ text = text[:start] + p.group(0) + '[FILTERED]' + text[end:]
+ else:
+ text = text[:start] + '[FILTERED]' + text[end:]
+ return text
+
+
+class PrivacyFilter:
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self._text = text
+
+ def filtered(self):
+ text = text_to_email_filtered(
+ self._text, self.preserve_email_hostname, self.partially_preserve_email_username)
+ text = text_to_tel(text, self.preserve_phone_country_code)
+ return text
+
+
+import calendar
+import re
+
+
+class Validations:
+
+ @classmethod
+ def is_phone(cls, value):
+ if bool(re.search(r'^(\+[1-9]\d{1,2}|00[1-9]\d{1,2}|0[1-9]{1,3})([ ()-]{,2}\d[ ()-]{,2}){6,11}[0-9]$', value)):
+ return True
+ return False
+
+ @classmethod
+ def is_email(cls, value):
+ if value.count('@') != 1:
+ return False
+ domain_host = value.split('@')
+ # da go proverq dali stana
+ # and and len(domain_host[0])<65 :
+ if bool(re.search(r'^[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})+)$)', value)):
+ if len(domain_host[0]) < 64 and cls.is_hostname(domain_host[1]):
+ return True
+ return False
+
+ @classmethod
+ def is_hostname(cls, value):
+ if bool(re.match(r'([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
+ new_hosts = value.split('.')
+ for host in new_hosts:
+ if len(host) > 64:
+ return False
+ return True
+ return False
+
+ @classmethod
+ def is_ip_address(cls, value):
+ value_for_expr = '.' + value
+ if re.match(r'(\.\d{1,4}){4,4}$', value_for_expr) != type(None):
+ list_of_bits = value.split('.')
+ if int(list_of_bits[0]) > 0 and int(list_of_bits[0]) < 256:
+ for i in list_of_bits:
+ if int(i) >= 0 and int(i) < 256:
+ continue
+ else:
+ return False
+ else:
+ return False
+ return True
+ return False
+
+ @classmethod
+ def is_number(cls, value):
+ if bool(re.match(r'(-?0\.\d|-?[1-9]|-?0$|-?[1-9]\d*\.\d)\d*$', value)):
+ return True
+ return False
+
+ @classmethod
+ def is_integer(cls, value):
+ if bool(re.search(r'(^-?0$|^-?[1-9]\d*$)', value)):
+ return True
+ return False
+
+ @classmethod
+ def is_date(cls, value):
+ date_dash = value + '-'
+ if bool(re.search(r'^\d{4,4}-(\d{2,2}-){2,2}$', date_dash)):
+ new_value = value.split('-')
+ if int(new_value[1]) > 0 and int(new_value[1]) < 13:
+ if int(new_value[2]) in range(1, 32) and int(new_value[1]) in [1, 3, 5, 7, 10, 12]:
+ return True
+ if int(new_value[2]) in range(1, 31) and int(new_value[1]) not in [1, 2, 3, 5, 7, 10, 12]:
+ return True
+ if int(new_value[1]) == 2 and calendar.isleap(int(new_value[0])) \
+ and int(new_value[2]) in range(1, 30):
+ return True
+
+ if int(new_value[1]) == 2 and not calendar.isleap(int(new_value[0])) \
+ and int(new_value[2]) in range(1, 29):
+ return False
+ return False
+
+ @classmethod
+ def is_time(cls, value):
+ # def is_hour(value):
+ hour_value = value + ':'
+ if bool(re.search(r'^(\d{1,2}:){3,3}$', hour_value)):
+ hour_parts = hour_value.split(':')
+ if int(hour_parts[0]) >= 0 and int(hour_parts[0]) < 24:
+ if int(hour_parts[1]) >= 0 and int(hour_parts[2]) >= 0:
+ if int(hour_parts[1]) < 60 and int(hour_parts[2]) < 60:
+ return True
+ return False
+
+ @classmethod
+ def is_datetime(cls, value):
+ if not bool(re.search(r'^\d{4,4}-\d{2,2}-\d{2,2} \d{2,2}:\d{2,2}:\d{2,2}$', value)):
+ return False
+ new_value = value.split(' ')
+ if cls.is_date(new_value[0]) and cls.is_time(new_value[1]):
+ return True
+ return False