Skip to content

Commit d71a4eb

Browse files
Session.exec to Session.query (#172)
1 parent e99caeb commit d71a4eb

File tree

5 files changed

+10
-19
lines changed

5 files changed

+10
-19
lines changed

crm/crm/state/login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class LoginState(State):
1111

1212
def log_in(self):
1313
with rx.session() as sess:
14-
user = sess.exec(User.select.where(User.email == self.email_field)).first()
14+
user = sess.query(User).filter_by(email=self.email_field).first()
1515
if user and user.password == self.password_field:
1616
self.user = user
1717
return rx.redirect("/")
@@ -20,7 +20,7 @@ def log_in(self):
2020

2121
def sign_up(self):
2222
with rx.session() as sess:
23-
user = sess.exec(User.select.where(User.email == self.email_field)).first()
23+
user = sess.query(User).filter_by(email=self.email_field).first()
2424
if user:
2525
return rx.window_alert(
2626
"Looks like you’re already registered! Try logging in instead."

local_auth/local_auth/base_state.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ def authenticated_user(self) -> User:
3131
corresponding to the currently authenticated user.
3232
"""
3333
with rx.session() as session:
34-
result = session.exec(
35-
select(User, AuthSession).where(
34+
result = session.query(User, AuthSession).where(
3635
AuthSession.session_id == self.auth_token,
3736
AuthSession.expiration
3837
>= datetime.datetime.now(datetime.timezone.utc),
3938
User.id == AuthSession.user_id,
40-
),
41-
).first()
39+
).first()
40+
4241
if result:
4342
user, session = result
4443
return user
@@ -56,9 +55,7 @@ def is_authenticated(self) -> bool:
5655
def do_logout(self) -> None:
5756
"""Destroy AuthSessions associated with the auth_token."""
5857
with rx.session() as session:
59-
for auth_session in session.exec(
60-
AuthSession.select.where(AuthSession.session_id == self.auth_token)
61-
).all():
58+
for auth_session in session.query(AuthSession).filter_by(session_id=self.auth_token).all():
6259
session.delete(auth_session)
6360
session.commit()
6461
self.auth_token = self.auth_token

local_auth/local_auth/login.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def on_submit(self, form_data) -> rx.event.EventSpec:
2525
username = form_data["username"]
2626
password = form_data["password"]
2727
with rx.session() as session:
28-
user = session.exec(
29-
User.select.where(User.username == username)
30-
).one_or_none()
28+
user = session.query(User).filter_by(username=username).first()
3129
if user is not None and not user.enabled:
3230
self.error_message = "This account is disabled."
3331
return rx.set_value("password", "")

local_auth/local_auth/registration.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ async def handle_registration(
3333
self.error_message = "Username cannot be empty"
3434
yield rx.set_focus("username")
3535
return
36-
existing_user = session.exec(
37-
User.select.where(User.username == username)
38-
).one_or_none()
36+
existing_user = session.query(User).filter_by(username=username).first()
3937
if existing_user is not None:
4038
self.error_message = (
4139
f"Username {username} is already registered. Try a different name"

twitter/twitter/state/auth.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def signup(self):
1616
with rx.session() as session:
1717
if self.password != self.confirm_password:
1818
return rx.window_alert("Passwords do not match.")
19-
if session.exec(User.select.where(User.username == self.username)).first():
19+
if session.query(User).filter_by(username=self.username).first():
2020
return rx.window_alert("Username already exists.")
2121
self.user = User(username=self.username, password=self.password)
2222
session.add(self.user)
@@ -27,9 +27,7 @@ def signup(self):
2727
def login(self):
2828
"""Log in a user."""
2929
with rx.session() as session:
30-
user = session.exec(
31-
User.select.where(User.username == self.username)
32-
).first()
30+
user = session.query(User).filter_by(username=self.username).first()
3331
if user and user.password == self.password:
3432
self.user = user
3533
return rx.redirect("/")

0 commit comments

Comments
 (0)