-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
158 lines (139 loc) · 5.01 KB
/
models.py
File metadata and controls
158 lines (139 loc) · 5.01 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
import datetime
import jwt
from flask_sqlalchemy import SQLAlchemy
from settings import SECRET_KEY
from utils import format_datetime as f_dt
from utils import sha512
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(32), index=True, unique=True)
nickname = db.Column(db.Unicode(32))
password_hash = db.Column(db.String(128))
email = db.Column(db.String(255), unique=True, nullable=True)
email_verified = db.Column(db.Boolean, default=False)
date_joined = db.Column(db.DateTime, default=datetime.datetime.now)
last_login = db.Column(db.DateTime, default=datetime.datetime.now)
active = db.Column(db.Boolean, default=True)
save_method = db.Column(db.String(32), default='normal')
diary_password_hash = db.Column(db.String(128), nullable=True)
diaries = db.relationship('Diary', backref='User', lazy='dynamic')
def to_json(self):
return {
'id':
self.id,
'username':
self.username,
'nickname':
self.nickname,
'email':
self.email,
'date_joined':
f_dt(self.date_joined),
'last_login':
f_dt(self.last_login),
'active':
self.active,
'save_method':
self.save_method,
'diary_password_hash':
self.save_method == 'aes' and self.diary_password_hash or ''
}
def __init__(self,
username,
password,
email=None,
nickname=None,
save_method='normal',
diary_password_hash=None):
self.username = username
self.password_hash = sha512(password)
self.email = email
self.nickname = nickname
self.save_method = save_method if save_method in ('normal',
'aes') else 'normal'
self.diary_password_hash = diary_password_hash
@staticmethod
def login(username, password):
user = User.query.filter_by(username=username).first()
if user is None:
return None
if user.password_hash != sha512(password):
return None
return user
def get_token(self):
payload = {
'user_id': self.id,
'exp': datetime.datetime.now() + datetime.timedelta(days=1)
}
jwt_data = jwt.encode(payload=payload,
key=SECRET_KEY,
algorithm='HS256')
if type(jwt_data) is bytes:
# In case a outdated version of PyJWT is installed
jwt_data = jwt_data.decode('utf-8')
return f'JWT {jwt_data}'
@staticmethod
def check_token(token):
try:
payload = jwt.decode(jwt=token,
key=SECRET_KEY,
algorithms=['HS256'])
user = User.query.get(payload['user_id'])
if user.active:
return user
return None
except jwt.ExpiredSignatureError:
return None
def __eq__(self, other):
return self.id == other.id
def __ne__(self, other):
return self.id != other.id
def __repr__(self):
return f'<User {self.username}>'
class Diary(db.Model):
__tablename__ = 'diaries'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(127))
content = db.Column(db.Text)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship('User',
backref='Diary',
lazy='joined',
viewonly=True)
tags = db.Column(db.Unicode(255))
create_time = db.Column(db.DateTime, default=datetime.datetime.now)
update_time = db.Column(db.DateTime, default=datetime.datetime.now)
public = db.Column(db.Boolean, default=False)
cover = db.Column(db.String(511), nullable=True)
def __init__(self, title, content, user_id, tags, public, cover):
self.title = title
self.content = content
self.user_id = user_id
self.tags = tags
self.public = public
self.cover = cover
def to_json(self, max_length=None, public=False):
data = {
'id': self.id,
'title': self.title,
'content':
self.content[:max_length] if max_length else self.content,
'tags': self.tags,
'cover': self.cover,
'create_time': f_dt(self.create_time),
'update_time': f_dt(self.update_time),
'public': self.public,
'user': {
'nickname': self.user.nickname,
}
}
if public == False:
data['user'].update({
'id': self.user.id,
'username': self.user.username
})
return data
def __repr__(self):
return f'<Diary {self.title}>'