generated from nighthawkcoders/flask_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_users.py
231 lines (197 loc) · 8.43 KB
/
model_users.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
""" database dependencies to support Users db examples """
import os
import shutil
from random import randrange
from __init__ import db
from sqlalchemy.exc import IntegrityError
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
''' Tutorial: https://www.sqlalchemy.org/library.html#tutorials, try to get into Python shell and follow along '''
# Define the 'Users Notes' table with a relationship to Users within the model
class Notes(db.Model):
__tablename__ = 'notes'
# Define the Notes schema
id = db.Column(db.Integer, primary_key=True)
note = db.Column(db.Text, unique=False, nullable=False)
image = db.Column(db.String, unique=False)
# Define a relationship in Notes Schema to userID who originates the note, many-to-one (many notes to one user)
userID = db.Column(db.Integer, db.ForeignKey('users.userID'))
# Constructor of a Notes object, initializes of instance variables within object
def __init__(self, userID, note, image):
self.userID = userID
self.note = note
self.image = image
# Returns a string representation of the Notes object, similar to java toString()
# returns string
def __repr__(self):
return "Notes(" + str(self.id) + "," + self.note + "," + str(self.userID) + ")"
# CRUD create, adds a new record to the Notes table
# returns the object added or None in case of an error
def create(self):
try:
# creates a Notes object from Notes(db.Model) class, passes initializers
db.session.add(self) # add prepares to persist person object to Notes table
db.session.commit() # SqlAlchemy "unit of work pattern" requires a manual commit
return self
except IntegrityError:
db.session.remove()
return None
# CRUD read, returns dictionary representation of Notes object
# returns dictionary
def read(self):
return {
"id": self.id,
"userID": self.userID,
"note": self.note,
"image": self.image
}
# Define the Users table within the model
# -- Object Relational Mapping (ORM) is the key concept of SQLAlchemy
# -- a.) db.Model is like an inner layer of the onion in ORM
# -- b.) Users represents data we want to store, something that is built on db.Model
# -- c.) SQLAlchemy ORM is layer on top of SQLAlchemy Core, then SQLAlchemy engine, SQL
class Users(UserMixin, db.Model):
__tablename__ = 'users'
# Define the Users schema
userID = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=False, nullable=False)
email = db.Column(db.String(255), unique=True, nullable=False)
password = db.Column(db.String(255), unique=False, nullable=False)
phone = db.Column(db.String(255), unique=False, nullable=False)
# Defines a relationship between User record and Notes table, one-to-many (one user to many notes)
notes = db.relationship("Notes", cascade='all, delete', backref='users', lazy=True)
# constructor of a User object, initializes of instance variables within object
def __init__(self, name, email, password, phone):
self.name = name
self.email = email
self.set_password(password)
self.phone = phone
# returns a string representation of object, similar to java toString()
def __repr__(self):
return "Users(" + str(self.userID) + "," + self.name + "," + str(self.email) + ")"
# CRUD create/add a new record to the table
# returns self or None on error
def create(self):
try:
# creates a person object from Users(db.Model) class, passes initializers
db.session.add(self) # add prepares to persist person object to Users table
db.session.commit() # SqlAlchemy "unit of work pattern" requires a manual commit
return self
except IntegrityError:
db.session.remove()
return None
# CRUD read converts self to dictionary
# returns dictionary
def read(self):
return {
"userID": self.userID,
"name": self.name,
"email": self.email,
"password": self.password,
"phone": self.phone,
"notes": self.notes,
"query": "by_alc" # This is for fun, a little watermark
}
def read2(self):
return {
"userID": self.userID,
"name": self.name,
"email": self.email,
"phone": self.phone,
}
# CRUD update: updates users name, password, phone
# returns self
def update(self, name="", email="", password="", phone=""):
"""only updates values with length"""
if len(name) > 0:
self.name = name
if len(email) > 0:
self.email = email
if len(password) > 0:
self.set_password(password)
if len(phone) > 0:
self.phone = phone
db.session.commit()
return self
# CRUD delete: remove self
# None
def delete(self):
db.session.delete(self)
db.session.commit()
return None
# set password method is used to create encrypted password
def set_password(self, password):
"""Create hashed password."""
self.password = generate_password_hash(password, method='sha256')
# check password to check versus encrypted password
def is_password_match(self, password):
"""Check hashed password."""
result = check_password_hash(self.password, password)
return result
# required for login_user, overrides id (login_user default) to implemented userID
def get_id(self):
return self.userID
"""Database Creation and Testing section"""
# Builds working data for testing
def model_builder():
"""Create required directories"""
try:
os.makedirs('volumes')
os.makedirs('volumes/uploads')
except:
pass
"""Create database and tables"""
db.create_all()
"""Tester data for table"""
u1 = Users(name='Thomas Edison', email='tedison@example.com', password='123toby', phone="1111111111")
u2 = Users(name='Nicholas Tesla', email='ntesla@example.com', password='123niko', phone="1111112222")
u3 = Users(name='Alexander Graham Bell', email='agbell@example.com', password='123lex', phone="1111113333")
u4 = Users(name='Eli Whitney', email='eliw@example.com', password='123whit', phone="1111114444")
u5 = Users(name='John Mortensen', email='jmort1021@gmail.com', password='123qwerty', phone="8587754956")
# u6 intends to succeed with a unique email
u6 = Users(name='John Mortensen', email='jmort1021@yahoo.com', password='123qwerty', phone="8587754956")
# U7 intended to fail as duplicate key
u7 = Users(name='John Mortensen', email='jmort1021@yahoo.com', password='123qwerty', phone="8586791294")
table = [u1, u2, u3, u4, u5, u6, u7]
"""Builds sample user/note(s) data"""
for row in table:
# prime uploads folder
shutil.copy("static/assets/ncs_logo.png", "volumes/uploads")
# add some notes with default image
try:
'''add a few 1 to 4 notes per user'''
for num in range(randrange(1, 4)):
note = "#### " + row.name + " note " + str(num) + ". \n Generated by test data."
row.notes.append(Notes(userID=row.userID, note=note, image='ncs_logo.png'))
'''add user/note data to table'''
db.session.add(row)
db.session.commit()
except IntegrityError:
'''fails with bad or duplicate data'''
db.session.remove()
print(f"Records exist, duplicate email, or error: {row.email}")
# Looks into database
def model_driver():
print("---------------------------")
print("Create Tables and Seed Data")
print("---------------------------")
model_builder()
print("---------------------------")
print("Table: " + Users.__tablename__)
print("Columns: ", Users.__table__.columns.keys())
print("---------------------------")
print("Table: " + Notes.__tablename__)
print("Columns: ", Notes.__table__.columns.keys())
print("---------------------------")
print()
users = Users.query
for user in users:
print("User" + "-" * 81)
print(user.read())
print("Notes" + "-" * 80)
for note in user.notes:
print(note.read())
print("-" * 85)
print()
if __name__ == "__main__":
model_driver()