This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.py
101 lines (73 loc) · 2.65 KB
/
validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import datetime
import re
from exceptions import PasswordError
class BaseValidator:
def __init__(self, text):
self.text = text
def validate(self) -> bool:
return True if self.text else False
class DateValidator(BaseValidator):
def __init__(self, datetime_timestamp: int):
self.datetime_ = datetime.datetime.fromtimestamp(datetime_timestamp)
super().__init__(datetime_timestamp)
def validate(self) -> bool:
if self.datetime_ > datetime.datetime.now():
return False
return True
class PasswordValidator(BaseValidator):
def __init__(self, password):
self.password = password
super().__init__(password)
def _check_password_len(self):
return not len(self.password) <= 8
def _check_digits_in_password(self):
for letter in self.password:
if letter.isdigit():
return True
else:
return False
def _check_upper_and_lower_case_in_password(self):
upper_count = 0
lower_count = 0
for letter in self.password:
if letter.isupper():
upper_count += 1
elif letter.islower():
lower_count += 1
if upper_count >= 1 and lower_count >= 1:
return True
else:
return False
@staticmethod
def _rplce_lttr_to_anthr_lttr_in_kbrd(password, first_row, second_row):
new_password = ''
for letter in password.lower():
for keyboard_row_index in range(len(first_row)):
if letter in first_row[keyboard_row_index]:
letter_index = first_row[keyboard_row_index].index(letter)
letter = second_row[keyboard_row_index][letter_index]
new_password += letter
break
else:
new_password += letter
return new_password
def validate(self) -> bool:
try:
if not self._check_password_len():
raise PasswordError()
if not self._check_upper_and_lower_case_in_password():
raise PasswordError()
if not self._check_digits_in_password():
raise PasswordError()
except PasswordError:
return False
return True
class EmailValidator(BaseValidator):
def __init__(self, email):
super().__init__(email)
self.email = email
def validate(self) -> bool:
email_regex = r'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$'
if re.search(email_regex, self.email):
return True
return False