-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
94 lines (61 loc) · 1.96 KB
/
validation.py
File metadata and controls
94 lines (61 loc) · 1.96 KB
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
# base imports
import re
import datetime
# modules
import SQL
import security
# User validation
def verify_login(username, password):
pwdhash = SQL.users.find_hash(username)
return pwdhash and security.verify_password(pwdhash, password)
def user_exists(username):
return bool(SQL.users.find_hash(username))
# Employees validation
def employee_exists(employee_id):
return bool(SQL.employees.get_employee(employee_id))
def check_employee_id(employee_id):
return bool(re.match(r'^E[0-9]{4}$', employee_id))
# Payments validation
def check_payment_id(payment_id):
return bool(re.match(r'^P[0-9]{4}$', payment_id))
# Tenants validation
def check_tenant_id(tenant_id):
return bool(re.match(r'^T[0-9]{4}$', tenant_id))
def tenant_exists(tenant_id):
return bool(SQL.tenants.get_tenant(tenant_id))
def apartments_exist():
return bool(SQL.apartments.get_addresses())
# Apartments validation
def check_apartment_id(apartment_id):
return bool(re.match(r'A[0-9]{4}$', apartment_id))
def apartment_exists(apartment_id):
return bool(SQL.apartments.get_apartment(apartment_id))
# Flats validation
def apartment_flat_exists(apartment_id, flat_number):
return bool(SQL.flats.get_flat_id(apartment_id, flat_number))
# Generic multi-table validation
def check_contact(contact):
return bool(re.match(r"\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}", contact))
def check_postcode(postcode):
return bool(re.match(r"^([a-zA-Z]{1,2}\d{1,2})\s(\d[a-zA-Z]{2})$", postcode))
def is_float(amount):
try:
float(amount)
except ValueError:
return False
else:
return True
def check_date(date):
try:
datetime.datetime.strptime(date, "%Y-%m-%d")
except ValueError:
return False
else:
return True
def check_time(time):
try:
datetime.datetime.strptime(time, "%H:%M")
except ValueError:
return False
else:
return True