-
Notifications
You must be signed in to change notification settings - Fork 0
/
userDB.py
53 lines (41 loc) · 1.7 KB
/
userDB.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
from botserver import db
from datetime import datetime
from stateDB import ConversationState
def create_or_add_user(chatID):
db.create_all()
user = User.query.filter(User.chat_id==chatID).one_or_none()
if user != None:
user.state = ConversationState.query.filter(ConversationState.user_id == user.id).one_or_none()
#TODO - hier ne abfrage falls das None ist. Stranger case, kann aber eintreten
return user, False
else:
user = User(chat_id=chatID)
db.session.add(user)
db.session.commit()
user.state = ConversationState(user_id=user.id)
db.session.add(user.state)
db.session.commit()
return user, True
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
chat_id = db.Column(db.Integer, unique=True)
name = db.Column(db.String)
createdate = db.Column(db.DateTime)
asked_restart = db.Column(db.Boolean)
asked_stop = db.Column(db.Boolean)
username = db.Column(db.String)
auth_str = db.Column(db.String)
def __repr__(self):
return "<User(id='%i', chat_id='%i', name='%s', created at='%s')>" % (self.id, self.chat_id, self.name, str(self.createdate))
def __init__(self, chat_id):
self.chat_id = chat_id
self.createdate = datetime.now()
self.asked_restart = False
self.asked_stop = False
def get_user_by_ID(userID):
user = User.query.filter(User.id==userID).one_or_none()
if user != None:
state = ConversationState.query.filter(ConversationState.user_id == user.id).one_or_none()
return user, state
return None, None