-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
89 lines (64 loc) · 2.2 KB
/
db.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
from peewee import *
from typing import List
import datetime
db = SqliteDatabase('main.db')
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField(unique=True)
pwhash = CharField()
class Thread(BaseModel):
pass
class Participant(BaseModel):
thread_id = ForeignKeyField(Thread, backref='participant')
user_id = ForeignKeyField(User)
class Message(BaseModel):
thread_id = ForeignKeyField(Thread, backref='messages')
user_id = ForeignKeyField(User)
text = CharField()
time = DateTimeField()
def get_user(login: str):
db.connect(reuse_if_open=True)
try:
user = User.get(User.username == login)
except Exception:
return
db.close()
return user
def create_thread(*usrnames: List[str]):
db.connect(reuse_if_open=True)
thread = Thread.create()
for i in usrnames:
Participant.create(thread_id=thread, user_id=User.get(User.username == i))
db.close()
def get_threads(user_id: int):
threads = Thread.select().join(Participant).where(Participant.user_id == user_id)
threads_dict = {}
for i in threads:
for j in i.participant:
if j.user_id.id == user_id:
continue
# {имя: id беседы}
threads_dict[j.user_id.username] = i.id
return threads_dict
def get_messages(thread_id: int) -> dict:
messages = Message.select().where(Message.thread_id == thread_id).order_by(Message.time)
messages_dict = {}
for i in messages:
messages_dict[i.id] = {'datetime': i.time,
'text': i.text,
'user_id': i.user_id.id,
'username': i.user_id.username}
return messages_dict
def create_message(text: str, thread_id: int, from_id: int) -> None:
db.connect(reuse_if_open=True)
Message.create(thread_id=thread_id, text=text,
user_id=from_id, time=datetime.datetime.now())
db.close()
def create_user(login: str, pwhash: str):
db.connect(reuse_if_open=True)
User.create(username=login, pwhash=pwhash)
db.close()
def init():
db.create_tables([User, Thread, Participant, Message])