Елена обнови решението на 22.04.2014 14:58 (преди над 10 години)
+import re
+
+
+class Validations:
+ @classmethod
+ def is_email(self, value):
+ email = value.split("@")
+ if len(email) != 2:
+ return False
+ if self.is_hostname(email[1]):
+ if re.match("[a-zA-Z0-9][a-zA-Z0-9\-_+.]{0,200}$", email[0]):
+ return True
+ else:
+ return False
+ else:
+ return False
+
+ @classmethod
+ def is_phone(self, value):
+ if re.match("((^0)|([+(^00)][1-9][0-9]{1,2}))([ \-()]{0,2}[0-9]){6,11}$", value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_hostname(self, value):
+ if re.match("([a-zA-z0-9][a-zA-z0-9-]{0,61}[a-zA-z0-9]\.)*[a-zA-z0-9][a-zA-z0-9-]{0,61}[a-zA-z0-9]\.[a-zA-z]{2,3}(\.[a-zA-z]{2})?$", value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_ip_address(self, value):
+ ips = value.split(".")
+ if len(ips) != 4:
+ return False
+ else:
+ for ip in ips:
+ if not re.match("(1?[0-9]?[0-9]$)|(2[0-4][0-9]$)|(25[0-5]$)", ip):
+ return False
+ return True
+
+ @classmethod
+ def is_number(self, value):
+ if re.match("-?(0|[1-9][0-9]*)(\.[0-9]*)?$", value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_integer(self, value):
+ if re.match("-?(0|[1-9][0-9]*)$", value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_date(self, value):
+ if re.match("[0-9]{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])$", value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_time(self, value):
+ if re.match("([01][0-9]|2[0-3]):([0-5][0-9]|60):([0-5][0-9]|60)$", value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_datetime(self, value):
+ if re.match("[0-9]{4}-(0[0-9]|1[0-2])-([0-2][0-9]|3[0-1])[ T]([01][0-9]|2[0-3]):([0-5][0-9]|60):([0-5][0-9]|60)$", value):
+ return True
+ else:
+ return False
+
+
+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
+ self._EMAIL_REGULAR_EXPRESSION = "[a-zA-Z0-9][a-zA-Z0-9\-_+.]{0,200}@([a-zA-z0-9][a-zA-z0-9-]{0,61}[a-zA-z0-9]\.)*[a-zA-z0-9][a-zA-z0-9-]{0,61}[a-zA-z0-9]\.[a-zA-z]{2,3}(\.[a-zA-z]{2})?"
+
+ def _preserve_hostname(self, email):
+ filtered = email.group().split("@")
+ filtered[0] = "[FILTERED]"
+ return '@'.join(filtered)
+
+ def _preserve_phone_country_code(self, phone):
+ filtered = str(phone.group())
+ # how are we supposed to tell the numbers in the code apart from the numbers in the phone?!
+ # for example, +359123456789 could be:
+ # country code +359 123456789 or +35 9123456789 or even +3 59123456789
+ # assuming that the country code is always three digits
+ if filtered[0] == '+' or (filtered[0] == '0' and filtered[1] == '0'):
+ if filtered[0] == '+':
+ filtered = filtered[0] + filtered[1] + filtered[2] + filtered[3] + " [FILTERED]"
+ else:
+ filtered = filtered[0] + filtered[1] + filtered[2] + filtered[3] + filtered[4] + filtered[5]
+ filtered += " FILTERED"
+ else:
+ filtered = "[FILTERED]"
+ return filtered
+
+ def _preserve_email_username(self, email):
+ filtered = email.group().split("@")
+ if len(filtered[0]) <= 6:
+ filtered[0] = "[FILTERED]"
+ else:
+ filtered[0] = filtered[0][0] + filtered[0][1] + filtered[0][2] + "[FILTERED]"
+ return '@'.join(filtered)
+
+ def filtered(self):
+ filtered_text = self._text
+ if self.partially_preserve_email_username:
+ filtered_text = re.sub(self._EMAIL_REGULAR_EXPRESSION, self._preserve_email_username, filtered_text)
+ elif self.preserve_email_hostname:
+ filtered_text = re.sub(self._EMAIL_REGULAR_EXPRESSION, self._preserve_hostname, filtered_text)
+ else:
+ filtered_text = re.sub(self._EMAIL_REGULAR_EXPRESSION, "[EMAIL]", filtered_text)
+ if self.preserve_phone_country_code:
+ filtered_text = re.sub("(0|([+(^00)][1-9][0-9]{1,2}))([ \-()]{0,2}[0-9]){6,11}", self._preserve_phone_country_code, filtered_text)
+ else:
+ filtered_text = re.sub("(0|([+(^00)][1-9][0-9]{1,2}))([ \-()]{0,2}[0-9]){6,11}", "[PHONE]", filtered_text)
+ return filtered_text