Skip to content

Commit

Permalink
Session.exec to Session.query (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElijahAhianyo authored Nov 22, 2023
1 parent e99caeb commit d71a4eb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 19 deletions.
4 changes: 2 additions & 2 deletions crm/crm/state/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LoginState(State):

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

def sign_up(self):
with rx.session() as sess:
user = sess.exec(User.select.where(User.email == self.email_field)).first()
user = sess.query(User).filter_by(email=self.email_field).first()
if user:
return rx.window_alert(
"Looks like you’re already registered! Try logging in instead."
Expand Down
11 changes: 4 additions & 7 deletions local_auth/local_auth/base_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ def authenticated_user(self) -> User:
corresponding to the currently authenticated user.
"""
with rx.session() as session:
result = session.exec(
select(User, AuthSession).where(
result = session.query(User, AuthSession).where(
AuthSession.session_id == self.auth_token,
AuthSession.expiration
>= datetime.datetime.now(datetime.timezone.utc),
User.id == AuthSession.user_id,
),
).first()
).first()

if result:
user, session = result
return user
Expand All @@ -56,9 +55,7 @@ def is_authenticated(self) -> bool:
def do_logout(self) -> None:
"""Destroy AuthSessions associated with the auth_token."""
with rx.session() as session:
for auth_session in session.exec(
AuthSession.select.where(AuthSession.session_id == self.auth_token)
).all():
for auth_session in session.query(AuthSession).filter_by(session_id=self.auth_token).all():
session.delete(auth_session)
session.commit()
self.auth_token = self.auth_token
Expand Down
4 changes: 1 addition & 3 deletions local_auth/local_auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def on_submit(self, form_data) -> rx.event.EventSpec:
username = form_data["username"]
password = form_data["password"]
with rx.session() as session:
user = session.exec(
User.select.where(User.username == username)
).one_or_none()
user = session.query(User).filter_by(username=username).first()
if user is not None and not user.enabled:
self.error_message = "This account is disabled."
return rx.set_value("password", "")
Expand Down
4 changes: 1 addition & 3 deletions local_auth/local_auth/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ async def handle_registration(
self.error_message = "Username cannot be empty"
yield rx.set_focus("username")
return
existing_user = session.exec(
User.select.where(User.username == username)
).one_or_none()
existing_user = session.query(User).filter_by(username=username).first()
if existing_user is not None:
self.error_message = (
f"Username {username} is already registered. Try a different name"
Expand Down
6 changes: 2 additions & 4 deletions twitter/twitter/state/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def signup(self):
with rx.session() as session:
if self.password != self.confirm_password:
return rx.window_alert("Passwords do not match.")
if session.exec(User.select.where(User.username == self.username)).first():
if session.query(User).filter_by(username=self.username).first():
return rx.window_alert("Username already exists.")
self.user = User(username=self.username, password=self.password)
session.add(self.user)
Expand All @@ -27,9 +27,7 @@ def signup(self):
def login(self):
"""Log in a user."""
with rx.session() as session:
user = session.exec(
User.select.where(User.username == self.username)
).first()
user = session.query(User).filter_by(username=self.username).first()
if user and user.password == self.password:
self.user = user
return rx.redirect("/")
Expand Down

0 comments on commit d71a4eb

Please sign in to comment.