Виктор обнови решението на 23.04.2014 14:21 (преди над 10 години)
+import re
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def preserve_username(self, match):
+ if len(match.group('username')) < 6:
+ return '[FILTERED]@' + match.group('hostname')
+ return match.group('username')[:3] + '[FILTERED]@' + match.group('hostname')
+
+ def preserve_country_code(self, match):
+ if match.group('international') is not None:
+ return match.group('international') + ' [FILTERED]'
+ return '[PHONE]'
+
+ def filtered(self):
+ username = r'(?P<username>[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{0,200})'
+ hostname = r'(?P<hostname>([0-9a-zA-Z][0-9a-zA-Z-]{0,61}[0-9a-zA-Z]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)'
+ email = username + '@' + hostname
+ prefix = r'((?<=[^a-zA-Z0-9\+])0(?=[^0])|(?P<international>(\b00|\+)[1-9][0-9]{0,2}))'
+ number = r'([ ()-]{0,2}[0-9]){6,11}'
+ phone = prefix + number
+ if self.partially_preserve_email_username:
+ result = re.sub(email, self.preserve_username, self.text)
+ elif self.preserve_email_hostname:
+ result = re.sub(email, r'[FILTERED]@\g<hostname>', self.text)
+ else:
+ result = re.sub(email, '[EMAIL]', self.text)
+ if self.preserve_phone_country_code:
+ result = re.sub(phone, self.preserve_country_code, result)
+ else:
+ result = re.sub(phone, '[PHONE]', result)
+ return result
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ username = r'[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{0,200}'
+ hostname = r'([0-9a-zA-Z][0-9a-zA-Z-]{0,61}[0-9a-zA-Z]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+ email = username + '@' + hostname + r'$'
+ return re.match(email, value) is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ prefix = r'(0(?=[^0])|(00|\+)[1-9][0-9]{0,2})'
+ number = r'([ ()-]{0,2}[0-9]){6,11}'
+ phone = prefix + number + r'$'
+ return re.match(phone, value) is not None
+
+ @classmethod
+ def is_hostname(cls, value):
+ hostname = r'([0-9a-zA-Z][0-9a-zA-Z-]{0,61}[0-9a-zA-Z]?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+ return re.match(hostname, value) is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ octet = r'(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
+ ip_address = octet + r'\.' + octet + r'\.' + octet + r'\.' + octet + r'$'
+ return re.match(ip_address, value) is not None
+
+ @classmethod
+ def is_number(cls, value):
+ number = r'-?(0|[1-9][0-9]*(\.[0-9]+)?)$'
+ return re.match(number, value) is not None
+
+ @classmethod
+ def is_integer(cls, value):
+ integer = r'-?(0|[1-9][0-9]*)$'
+ return re.match(integer, value) is not None
+
+ @classmethod
+ def is_date(cls, value):
+ date = r'[0-9]{4}-(0[0-9]|1[0-2])-([0-2][0-9]|3[01])$'
+ return re.match(date, value) is not None
+
+ @classmethod
+ def is_time(cls, value):
+ time = r'([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$'
+ return re.match(time, value) is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ date = r'[0-9]{4}-(0[0-9]|1[0-2])-([0-2][0-9]|3[01])'
+ time = r'([01][0-9]|[2[0-3]):[0-5][0-9]:[0-5][0-9]'
+ datetime = date + r'[ T]' + time + r'$'
+ return re.match(datetime, value) is not None