Skip to content

Commit

Permalink
Add user capacity to AppUser object
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrek14 committed Nov 6, 2023
1 parent 5742a84 commit 28f58f8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)], db: Se
first_name=decoded["given_name"],
last_name=decoded["family_name"],
roles=[],
capacities=user_in_db.capacities,
)
if USE_OIDC_ROLES:
user.roles = decoded[OIDC_ROLES_PROPERTY].copy()
Expand Down
20 changes: 14 additions & 6 deletions api/models/user.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import List
from sqlalchemy import Date, Column, ForeignKey, Integer, String, Numeric, UniqueConstraint, CheckConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship, Mapped, mapped_column
from sqlalchemy.ext.hybrid import hybrid_property
from db.base_class import Base
from datetime import date


class User(Base):
Expand All @@ -11,6 +12,7 @@ class User(Base):
id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
password = Column(String(length=256), nullable=True)
login = Column(String(length=100), nullable=False, unique=True)
capacities: Mapped[List["UserCapacity"]] = relationship()


class UserGroup(Base):
Expand Down Expand Up @@ -63,9 +65,15 @@ class UserCapacity(Base):

id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
capacity = Column("journey", Numeric(precision=8, scale=4), nullable=False)
init_date = Column(Date, nullable=False)
end_date = Column(Date, nullable=True)
user = Column("usrid", Integer, ForeignKey("usr.id"), nullable=False)
start = Column("init_date", Date, nullable=False)
end = Column("end_date", Date, nullable=True)
user_id = Column("usrid", Integer, ForeignKey("usr.id"), nullable=False)

@hybrid_property
def is_current(self):
if not self.end:
return date.today() >= self.start
return date.today() >= self.start and date.today() <= self.end

__table_args__ = (
UniqueConstraint("usrid", "init_date", name="unique_journey_history_user_date"),
Expand Down
12 changes: 12 additions & 0 deletions api/schemas/user.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from typing import List, Optional
from pydantic import ConfigDict, BaseModel
from datetime import date


class UserCapacity(BaseModel):
capacity: Optional[float] = None
start: Optional[date] = None
end: Optional[date] = None
is_current: Optional[bool] = None
model_config = ConfigDict(from_attributes=True)


class User(BaseModel):
id: int
login: str
capacities: Optional[List[UserCapacity]] = None
model_config = ConfigDict(from_attributes=True)


Expand All @@ -26,3 +36,5 @@ class AppUser(BaseModel):
first_name: Optional[str] = None
last_name: Optional[str] = None
roles: Optional[List[str]] = None
capacities: Optional[List[UserCapacity]] = None
model_config = ConfigDict(from_attributes=True)
8 changes: 8 additions & 0 deletions frontend/src/domain/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ export type User = {
firstName: string
lastName: string
roles: Roles
capacities: Array<UserCapacity>
}

type UserCapacity = {
capacity: number
start: string
end: string
isCurrent: boolean
}

0 comments on commit 28f58f8

Please sign in to comment.