forked from jiwu4742/info9117_flaskr
/
models.py
executable file
·131 lines (101 loc) · 3.7 KB
/
models.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
from flask_bcrypt import generate_password_hash, check_password_hash
class User(object):
"""
Defines a user with associated username, password, Gravatar email address, admin privileges, and approval from admin for log in.
I am treating _username, _password, _gravataremail, _flag_admin, and _flag_approval as private fiel, _flag_admin, and _flag_approvalds of the class, but this is purely an
implementation detail. You simply need to make an assignment to a User object's "password"
field like so:
user.password = "newpassword"
and the setter function is automatically called.
"""
def __init__(self, username, password, gravataremail, flag_admin, flag_approval):
self.username = username
self.password = password
self.gravataremail = gravataremail
self.flag_admin = flag_admin
self.flag_approval = flag_approval
#### username
@property
def username(self):
"""
:return: The username
"""
return self._username
@username.setter
def username(self, newname):
"""
Set the username.
This is as simple as it looks, simply making an assignment to
the _username field so it can be returned in the getter.
"""
self._username = newname
#### password
@property
def password(self):
"""
Getter method for password.
:return: The User object's hashed password
"""
return self._password
@password.setter
def password(self, plaintext):
"""
Sets the user's password, converting the plain text argument to a hashed password.
:param plaintext: the plain text being entered by the user, to be hased
"
"""
self._password = generate_password_hash(plaintext)
#### Gravatar email address
@property
def gravataremail(self):
"""
:return: The Gravatar email address
"""
return self._gravataremail
@gravataremail.setter
def gravataremail(self, newemail):
"""
Set the Gravatar email
This is as simple as it looks, simply making an assignment to
the _gravataremail field so it can be returned in the getter.
"""
self._gravataremail = newemail
#### identifier for admin privileges
@property
def flag_admin(self):
"""
:return: The identifier for admin privileges
"""
return self._flag_admin
@flag_admin.setter
def flag_admin(self, newflag_admin):
"""
Set the identifier for admin privileges
This is as simple as it looks, simply making an assignment to
the _flag_admin field so it can be returned in the getter.
"""
self._flag_admin = newflag_admin
#### identifier for admin approval
@property
def flag_approval(self):
"""
:return: The identifier for admin approval
"""
return self._flag_approval
@flag_approval.setter
def flag_approval(self, newflag_approval):
"""
Set the identifier for admin approval
This is as simple as it looks, simply making an assignment to
the _flag_admin field so it can be returned in the getter.
"""
self._flag_approval = newflag_approval
#### check password
def check_password(self, plaintext):
"""
Check that a plaintext password is equal to the hashed password.
At this stage, this method is not being used except in testing.
:param plaintext: plaintext password to check
:return: Boolean value; if true, then the plaintext and hash correspond, else false.
"""
return check_password_hash(self.password, plaintext)