-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_model.py
More file actions
201 lines (197 loc) · 8.11 KB
/
customer_model.py
File metadata and controls
201 lines (197 loc) · 8.11 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from sqlalchemy.sql import func
from config import *
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$')
PW_REGEX = re.compile('^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{6,20}$')
PHONE_REGEX=re.compile('^\d{3}-\d{3}-\d{4}$')
class Customer(db.Model):
__tablename__="customers"
id=db.Column(db.Integer,primary_key=True)
username=db.Column(db.String(255))
name=db.Column(db.String(255))
email=db.Column(db.String(255),nullable=False)
phone_number=db.Column(db.String(20))
password=db.Column(db.String(255))
favorite_order_id=db.Column(db.Integer)
note=db.Column(db.String(255))
created_at = db.Column(db.DateTime, server_default=func.now())
updated_at = db.Column(db.DateTime, server_default=func.now(), onupdate=func.now())
# favorite_order_id can be null, so not sure about making it a foreign key and relationship
# favorite=db.relationship('Order',foreign_keys=[favorite_order_id])
def __repr__(self):
return '<User {}>'.format(self.name)
def update_favorite(self,favorite_order_id):
self.favorite_order_id=favorite_order_id
db.session.commit()
def update_name(self,new_name):
self.name=new_name
db.session.commit()
def update_email(self,new_email):
self.email=new_email
db.session.commit()
def update_phone(self,new_phone):
self.phone_number=new_phone
db.session.commit()
def update_password(self,new_password):
hashed_pwd=bcrypt.generate_password_hash(new_password)
self.password=hashed_pwd
db.session.commit()
def update_note(self,new_note):
self.note=new_note
db.session.commit()
@classmethod
def validate_username(cls,username):
errors=[]
existing_users=cls.query.filter(cls.username==username).count()
if (existing_users)>0:
errors.append("This user's first and last name is already registered!")
return errors
@classmethod
def validate_password(cls, password, confirm_password):
errors=[]
if len(password)<5:
errors.append("Password must be at least 5 characters long!")
if password!=confirm_password:
errors.append("Passwords don't match!")
return errors
@classmethod
def validate_name(cls,name):
errors=[]
if len(name)<3:
errors.append("Please enter your name (at leat 3 characters).")
# if not name.isalpha():
# errors.append("Names must be alphabet characters only!")
return errors
@classmethod
def validate_email(cls,email_address):
errors=[]
if not EMAIL_REGEX.match(email_address): # test whether a field matches the pattern
errors.append("Invalid email address!")
existing_users=cls.query.filter_by(email=email_address).count()
if (existing_users)>0:
errors.append("This email address is currently in use by another user!")
return errors
@classmethod
def validate_phone(cls,phone_number):
errors=[]
if len(phone_number)<7:
errors.append("Please enter a valid 10 digit phone number.")
# if not PHONE_REGEX.match(phone_number):
# errors.append("Please enter a valid phone number.")
return errors
@classmethod
def validate_address(cls,addr):
errors=[]
if len(addr)<3:
errors.append("Please enter your Street Address (at leat 3 characters).")
return errors
@classmethod
def validate_city(cls,name):
errors=[]
if len(name)<3:
errors.append("Please enter your City (at leat 3 characters).")
return errors
@classmethod
def validate_info(cls,customer_info):
errors=[]
#errors+=validate_username(customer_info['username'])
errors+=cls.validate_name(customer_info['name'])
errors+=cls.validate_password(customer_info['password'],customer_info['confirm_password'])
errors+=cls.validate_email(customer_info['email_address'])
errors+=cls.validate_phone(customer_info['phone_number'])
errors+=cls.validate_address(customer_info['street_address'])
errors+=cls.validate_city(customer_info['city'])
return errors
@classmethod
def new(cls,customer_info):
'''
customer_info=[username':string,'password':string, 'name':string,'email':string,'phone':string]
'''
hashed_pwd=bcrypt.generate_password_hash(customer_info['password'])
# new_customer=cls(username=customer_info['username'],password=hashed_pwd,name=customer_info['name'], email=customer_info['email'],phone_number=customer_info['phone'])
new_customer=cls(username=customer_info['email_address'],password=hashed_pwd,name=customer_info['name'], email=customer_info['email_address'],phone_number=customer_info['phone_number'])
db.session.add(new_customer)
db.session.commit()
address=Address.new(new_customer.id,customer_info)
return new_customer
@classmethod
def get(cls,customer_id):
return cls.query.get(customer_id)
@classmethod
def get_all(cls):
return cls.query.all()
@classmethod
def validate_login(cls,form):
user=cls.query.filter_by(email=form['email_address']).first()
print(user)
if user:
if bcrypt.check_password_hash(user.password,form['password']):
return user
return None
@classmethod
def is_logged_in(cls,user_id,login_session):
user=cls.query.get(user_id)
result=False
if user:
if bcrypt.check_password_hash(login_session,str(user.created_at)):
result=True
return result
@classmethod
def get_session_key(cls,id):
user=cls.query.get(id)
session_key=bcrypt.generate_password_hash(str(user.created_at))
return session_key
#
@classmethod
def edit_user(cls,customer_id,form):
cust_update = Customer.get(customer_id)
cust_update.name = form['name']
cust_update.email = form['email']
cust_update.phone_number = form['phone']
address=cust_update.addresses[0]
address.street_address=form['street_address']
address.city=form['city']
state=State.by_name(form['state'])
address.state_id=state.id
db.session.commit()
return cust_update.name
class Address(db.Model):
__tablename__="addresses"
id=db.Column(db.Integer,primary_key=True)
customer_id=db.Column(db.Integer,db.ForeignKey('customers.id'),nullable=False)
street_address=db.Column(db.String(255))
city=db.Column(db.String(255))
state_id=db.Column(db.Integer,db.ForeignKey('states.id'),nullable=False)
created_at = db.Column(db.DateTime, server_default=func.now())
updated_at = db.Column(db.DateTime, server_default=func.now(), onupdate=func.now())
state=db.relationship('State',foreign_keys=[state_id],uselist=False)
customer=db.relationship('Customer',foreign_keys=[customer_id],backref=db.backref("addresses",cascade="all,delete-orphan"))
@classmethod
def new(cls,customer_id,address):
state=State.by_name(address['state'])
if not state:
state=State.new(address['state'])
print("Address:",address)
new_address=cls(customer_id=customer_id,street_address=address['street_address'],city=address['city'],state_id=state.id)
db.session.add(new_address)
db.session.commit()
return new_address
class State(db.Model):
__tablename__="states"
id=db.Column(db.Integer,primary_key=True)
name=db.Column(db.String(2))
created_at = db.Column(db.DateTime, server_default=func.now())
updated_at = db.Column(db.DateTime, server_default=func.now(), onupdate=func.now())
@classmethod
def new(cls,name):
state=cls(name=name)
db.session.add(state)
db.session.commit()
return state
@classmethod
def get_all(cls):
return cls.query.all()
@classmethod
def by_name(cls,name):
return cls.query.filter(cls.name==name).first()