44from openai import OpenAI
55
66import reflex as rx
7+ from sqlmodel import select
78
89from .helpers import navbar
910
@@ -29,6 +30,7 @@ class Question(rx.Model, table=True):
2930
3031class State (rx .State ):
3132 """The app state."""
33+
3234 show_columns = ["Question" , "Answer" ]
3335 username : str = ""
3436 password : str = ""
@@ -42,21 +44,22 @@ def questions(self) -> list[Question]:
4244 """Get the users saved questions and answers from the database."""
4345 with rx .session () as session :
4446 if self .logged_in :
45- qa = (
46- session . query (Question )
47+ qa = session . exec (
48+ select (Question )
4749 .where (Question .username == self .username )
4850 .distinct (Question .prompt )
4951 .order_by (Question .timestamp .desc ())
5052 .limit (MAX_QUESTIONS )
51- .all ()
52- )
53+ ).all ()
5354 return [[q .prompt , q .answer ] for q in qa ]
5455 else :
5556 return []
5657
5758 def login (self ):
5859 with rx .session () as session :
59- user = session .query (User ).where (User .username == self .username ).first ()
60+ user = session .exec (
61+ select (User ).where (User .username == self .username )
62+ ).first ()
6063 if (user and user .password == self .password ) or self .username == "admin" :
6164 self .logged_in = True
6265 return rx .redirect ("/home" )
@@ -76,25 +79,28 @@ def signup(self):
7679 return rx .redirect ("/home" )
7780
7881 def get_result (self ):
79- if (
80- rx .session ()
81- .query (Question )
82- .where (Question .username == self .username )
83- .where (Question .prompt == self .prompt )
84- .first ()
85- or rx .session ()
86- .query (Question )
87- .where (Question .username == self .username )
88- .where (
89- Question .timestamp
90- > datetime .datetime .now () - datetime .timedelta (days = 1 )
91- )
92- .count ()
93- > MAX_QUESTIONS
94- ):
95- return rx .window_alert (
96- "You have already asked this question or have asked too many questions in the past 24 hours."
97- )
82+ with rx .session as session :
83+ if (
84+ session .exec (
85+ select (Question )
86+ .where (Question .username == self .username )
87+ .where (Question .prompt == self .prompt )
88+ ).first ()
89+ or len (
90+ session .exec (
91+ select (Question )
92+ .where (Question .username == self .username )
93+ .where (
94+ Question .timestamp
95+ > datetime .datetime .now () - datetime .timedelta (days = 1 )
96+ )
97+ ).all ()
98+ )
99+ > MAX_QUESTIONS
100+ ):
101+ return rx .window_alert (
102+ "You have already asked this question or have asked too many questions in the past 24 hours."
103+ )
98104 try :
99105 response = client .completions .create (
100106 model = "text-davinci-002" ,
@@ -180,7 +186,12 @@ def login():
180186 return rx .center (
181187 rx .vstack (
182188 rx .input (on_blur = State .set_username , placeholder = "Username" , width = "100%" ),
183- rx .input (type_ = "password" , on_blur = State .set_password , placeholder = "Password" , width = "100%" ),
189+ rx .input (
190+ type_ = "password" ,
191+ on_blur = State .set_password ,
192+ placeholder = "Password" ,
193+ width = "100%" ,
194+ ),
184195 rx .button ("Login" , on_click = State .login , width = "100%" ),
185196 rx .link (rx .button ("Sign Up" , width = "100%" ), href = "/signup" , width = "100%" ),
186197 ),
@@ -201,7 +212,10 @@ def signup():
201212 on_blur = State .set_username , placeholder = "Username" , width = "100%"
202213 ),
203214 rx .input (
204- type_ = "password" , on_blur = State .set_password , placeholder = "Password" , width = "100%"
215+ type_ = "password" ,
216+ on_blur = State .set_password ,
217+ placeholder = "Password" ,
218+ width = "100%" ,
205219 ),
206220 rx .input (
207221 type_ = "password" ,
0 commit comments