-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
167 lines (127 loc) · 4.91 KB
/
database.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
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
159
160
161
162
163
164
165
166
167
from collections.abc import Generator
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
import util
from config import settings
from models import *
SQLALCHEMY_DATABASE_URL = "sqlite:///./" + settings.db_name
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db() -> Generator[Session]:
db = SessionLocal()
try:
yield db
finally:
db.close()
class Database:
def __init__(self) -> None:
db = next(get_db(), None)
if db is None:
raise Exception("Could not connect to database")
self.db: Session = db
def get_user(self, username: str) -> User | None:
return self.db.query(User).filter(User.username == username).first()
def get_user_by_phone(self, phone: str) -> User | None:
return self.db.query(User).filter(User.phone == phone).first()
def get_user_by_hash(self, hash: str) -> User | None:
return self.db.query(User).filter(User.hash == hash).first()
def get_users(self, skip: int = 0, limit: int = 100) -> list[User]:
return self.db.query(User).offset(skip).limit(limit).all()
def create_user(self, phone: str, username: str) -> User:
user = User(
username=username,
phone=phone,
active=True,
hash=util.user_hash(username),
pics=None,
)
self.db.add(user)
self.db.commit()
self.db.refresh(user)
return user
def update_user(self, user: User) -> User | None:
db_user = self.db.query(User).filter(User.phone == user.phone).first()
if db_user is None:
return None
db_user.username = user.username
db_user.active = user.active
self.db.commit()
return db_user
def get_reg(self, phone: str) -> Registration | None:
return self.db.query(Registration).filter(Registration.phone == phone).first()
def create_reg(self, phone: str) -> Registration:
db_reg = Registration(phone=phone, state=0)
self.db.add(db_reg)
self.db.commit()
self.db.refresh(db_reg)
return db_reg
def update_reg(
self, phone: str, state: int, username: str | None = None
) -> Registration | None:
db_reg = self.db.query(Registration).filter(Registration.phone == phone).first()
if db_reg is None:
return None
db_reg.state = state
if username is not None:
db_reg.username = username
self.db.commit()
return db_reg
def get_prompt(self, prompt_id: int) -> Prompt | None:
return self.db.query(Prompt).filter(Prompt.id == prompt_id).first()
def get_current_prompt(self) -> Prompt | None:
return self.db.query(Prompt).order_by(Prompt.id.desc()).first()
def create_prompt(self, prompt_text: str):
db_prompt = Prompt(prompt=prompt_text, date=datetime.now().date())
self.db.add(db_prompt)
self.db.commit()
self.db.refresh(db_prompt)
return db_prompt
def get_all_prompts(self) -> list[Prompt]:
return self.db.query(Prompt).all()
def get_pic(self, username: str, prompt_id: int):
return (
self.db.query(Pic)
.filter(Pic.user == username, Pic.prompt == prompt_id)
.first()
)
def get_pics_by_hash(self, user_hash: str) -> list[Pic]:
user = self.db.query(User).filter(User.hash == user_hash).first()
if user is None:
return []
return list(self.db.query(Pic).filter(Pic.user == user.username))
def get_pics_by_prompt(self, prompt_id: int) -> list[Pic]:
return list(
self.db.query(Pic)
.filter(Pic.prompt == prompt_id)
.order_by(Pic.winner.desc())
)
def get_winner_by_prompt(self, prompt_id: int) -> Pic | None:
return (
self.db.query(Pic)
.filter(Pic.prompt == prompt_id, Pic.winner == True)
.first()
)
def get_submission_status(self, user_hash: str, prompt_id: int) -> bool:
user = self.get_user_by_hash(user_hash)
if user is None:
return False
pic = self.get_pic(str(user.username), prompt_id)
return pic is not None
def create_pic(self, url: str, prompt_id: int, username: str) -> Pic | None:
if self.get_pic(username, prompt_id) is not None:
return None
pic = Pic(url=url, prompt=prompt_id, user=username)
self.db.add(pic)
self.db.commit()
self.db.refresh(pic)
return pic
def set_winner(self, pic_id: int):
pic = self.db.query(Pic).filter(Pic.id == pic_id).first()
if pic is None:
return None
pic.winner = not pic.winner
self.db.commit()
return pic