-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
63 lines (55 loc) · 2.04 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
from . import db, app
import datetime
import jwt
class User(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
def encode_auth_token(self, user_id):
"""
Generates the Auth Token
:return: string
"""
try:
payload = {
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=100, seconds=5),
'iat': datetime.datetime.utcnow(),
'sub': user_id
}
return jwt.encode(
payload,
app.config.get('SECRET_KEY'),
algorithm='HS256'
)
except Exception as e:
return e
@staticmethod
def decode_auth_token(auth_token):
"""
Validates the auth token
:param auth_token:
:return: integer|string
"""
try:
payload = jwt.decode(auth_token, app.config.get('SECRET_KEY'), algorithms=['HS256'])
return payload['sub']
except jwt.ExpiredSignatureError:
return 'Signature expired. Please log in again.'
except jwt.InvalidTokenError:
return 'Invalid token. Please log in again.'
def __repr__(self):
return f'id={self.id}'
class Transaction(db.Model):
id = db.Column(db.Integer, primary_key=True)
payer = db.Column(db.String(80), nullable=False)
points = db.Column(db.Integer, nullable=False)
remainingPoints = db.Column(db.Integer, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
userID = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
user = db.relationship('User', backref=db.backref('transactions', lazy=True))
def __repr__(self):
return f'id={self.id}, payer={self.payer}, points={self.points}, timestamp={self.timestamp}'
def to_dict(self):
return {
'payer': self.payer,
'points': self.points,
'timestamp': self.timestamp.strftime('%Y-%m-%dT%H:%M:%SZ')
}