models/User: add active

This commit is contained in:
lemoer 2024-12-21 01:45:10 +01:00
parent 1e801789b8
commit fce9452b72

View File

@ -18,6 +18,7 @@ class User(SQLModel, table=True):
id: int = Field(primary_key=True) id: int = Field(primary_key=True)
name: str name: str
joined_at: datetime.datetime = Field(default_factory=utc_now) joined_at: datetime.datetime = Field(default_factory=utc_now)
active: bool = True
participation_requests: list["ParticipationRequest"] = Relationship(back_populates="user") participation_requests: list["ParticipationRequest"] = Relationship(back_populates="user")
@ -38,7 +39,7 @@ class User(SQLModel, table=True):
return self.joined_at return self.joined_at
def all_users_sorted(session: Session) -> List[User]: 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()) users.sort(key=lambda u: u.sort_time())
return users return users
@ -55,6 +56,19 @@ def next_user_to_send_request(session: Session, task: "Task") -> User | None:
return u 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): class Task(SQLModel, table=True):
id: int = Field(primary_key=True) id: int = Field(primary_key=True)
name: str name: str
@ -128,6 +142,7 @@ class ParticipationState(enum.Enum):
# TIMEOUT -> REQUESTED (should not happen, bug) # TIMEOUT -> REQUESTED (should not happen, bug)
# #
# => TODO: What should happen after the due?!!! # => TODO: What should happen after the due?!!!
# => TODO: What happens when a user has two requests?
class StateTransition: class StateTransition:
@ -487,3 +502,29 @@ def test_reject(session):
assert(r2.state == ParticipationState.REJECTED) assert(r2.state == ParticipationState.REJECTED)
session.commit() 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]