From fce9452b72511d759a73df234571055713dcd93b Mon Sep 17 00:00:00 2001 From: lemoer Date: Sat, 21 Dec 2024 01:45:10 +0100 Subject: [PATCH] models/User: add active --- models.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/models.py b/models.py index c870753..5f1e211 100644 --- a/models.py +++ b/models.py @@ -18,6 +18,7 @@ class User(SQLModel, table=True): id: int = Field(primary_key=True) name: str joined_at: datetime.datetime = Field(default_factory=utc_now) + active: bool = True participation_requests: list["ParticipationRequest"] = Relationship(back_populates="user") @@ -38,7 +39,7 @@ class User(SQLModel, table=True): return self.joined_at def all_users_sorted(session: Session) -> List[User]: - users = session.exec(select(User)).all() + users = session.exec(select(User).where(User.active)).all() users.sort(key=lambda u: u.sort_time()) return users @@ -55,6 +56,19 @@ def next_user_to_send_request(session: Session, task: "Task") -> User | None: return u +def get_user_by_name(session: Session, name: str, only_active: bool = True) -> Result[User, None]: + Q = select(User).where(User.name == name) + + if only_active: + Q = Q.where(User.active) + + user = session.exec(Q).first() + + if user is None: + return Err(None) + else: + return Ok(user) + class Task(SQLModel, table=True): id: int = Field(primary_key=True) name: str @@ -128,6 +142,7 @@ class ParticipationState(enum.Enum): # TIMEOUT -> REQUESTED (should not happen, bug) # # => TODO: What should happen after the due?!!! +# => TODO: What happens when a user has two requests? class StateTransition: @@ -487,3 +502,29 @@ def test_reject(session): assert(r2.state == ParticipationState.REJECTED) session.commit() + +def test_active(session): + u1 = User(name="u1") + u2 = User(name="u2") + u3 = User(name="u3") + + session.add(u1) + session.add(u2) + session.add(u3) + + session.commit() + + users = all_users_sorted(session) + assert users == [u1, u2, u3] + + u2.active = False + session.commit() + + users = all_users_sorted(session) + assert users == [u1, u3] + + u2.active = True + session.commit() + + users = all_users_sorted(session) + assert users == [u1, u2, u3]