-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
52 lines (38 loc) · 1.27 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
from flask_sqlalchemy import SQLAlchemy
import config
db = SQLAlchemy()
def SetUp(app):
app.config.from_object(config.db_config)
db.app = app
db.init_app(app)
def Create():
db.drop_all()
db.create_all()
class User(db.Model):
"""Table to store auth status"""
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.BigInteger)
usename = db.Column(db.String(128))
first_name = db.Column(db.String(128))
last_name = db.Column(db.String(128))
state = db.Column(db.String(8))
def login(self):
self.state = config.states.auth.value
db.session.commit()
def logout(self):
self.state = config.states.init.value
db.session.commit()
def __repr__(self):
return '<User state: {}>'.format(self.state)
def CreateUser(message):
"""Add new string to the database"""
user = User(user_id=message.chat.id,
usename=message.from_user.username,
last_name=message.from_user.last_name,
first_name=message.from_user.first_name,
state=config.states.init.value)
db.session.add(user)
db.session.commit()
def GetUser(message):
"""Return the user instance"""
return User.query.filter_by(user_id=message.chat.id).first()