Repository navigation
Expand file tree
/
Copy pathcustomeroperation.py
More file actions
90 lines (74 loc) · 3.37 KB
/
Copy pathcustomeroperation.py
File metadata and controls
90 lines (74 loc) · 3.37 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
import re
class CustomerOperation:
@staticmethod
def validate_email(user_email):
email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(email_regex, user_email) is not None
@staticmethod
def validate_mobile(user_mobile):
mobile_regex = r'^(04|03)\d{8}$'
return re.match(mobile_regex, user_mobile) is not None
@staticmethod
def register_customer(user_name, user_password, user_email, user_mobile):
if not CustomerOperation.validate_email(user_email) or not CustomerOperation.validate_mobile(user_mobile):
return False
from datetime import datetime
current_time = datetime.now().strftime("%d-%m-%Y_%H:%M:%S")
user_id = UserOperation.generate_unique_user_id()
if UserOperation.check_username_exist(user_name):
return False
customer = Customer(user_id=user_id, user_name=user_name, user_password=user_password,
user_register_time=current_time, user_role='customer', user_email=user_email, user_mobile=user_mobile)
with open("data/users.txt", 'a') as file:
file.write(str(customer) + '\n')
return True
@staticmethod
def update_profile(attribute_name, value, customer_object):
if attribute_name == 'user_email':
if not CustomerOperation.validate_email(value):
return False
elif attribute_name == 'user_mobile':
if not CustomerOperation.validate_mobile(value):
return False
customer_object.__setattr__(attribute_name, value)
customers = CustomerOperation.get_all_customers()
for i, customer in enumerate(customers):
if customer.user_id == customer_object.user_id:
customers[i] = customer_object
break
with open("data/users.txt", 'w') as file:
for customer in customers:
file.write(str(customer) + '\n')
return True
@staticmethod
def delete_customer(customer_id):
customers = CustomerOperation.get_all_customers()
for i, customer in enumerate(customers):
if customer.user_id == customer_id:
del customers[i]
with open("data/users.txt", 'w') as file:
for customer in customers:
file.write(str(customer) + '\n')
return True
return False
@staticmethod
def get_customer_list(page_number):
customers = CustomerOperation.get_all_customers()
total_pages = (len(customers) + 9) // 10
start_index = (page_number - 1) * 10
end_index = start_index + 10
page_customers = customers[start_index:end_index]
return page_customers, page_number, total_pages
@staticmethod
def delete_all_customers():
with open("data/users.txt", 'w') as file:
pass
@staticmethod
def get_all_customers():
customers = []
with open("data/users.txt", 'r') as file:
for line in file:
user_data = eval(line.strip())
if user_data['user_role'] == 'customer':
customers.append(Customer(**user_data))
return customers