Skip to content

Add create-by audit for scenario creation (again) #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion api/src/api/app/apis/scenarios_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@
response_model_by_alias=True,
)
async def create_scenario(
request: Request,
scenario: Scenario = Body(None, description="")
) -> ID:
"""Create a new scenario to be simulated."""
return await controller.create_scenario(scenario)

# Tag creator info
scenario.creator_user_id = request.state.user.userId if request.state.user else None
scenario.creator_org_id = request.state.realm if request.state.realm else None

return await controller.create_scenario(
scenario
)


@router.delete(
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/app/controller/scenario_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ScenarioController:

async def create_scenario(
self,
scenario: Optional[Scenario],
scenario: Optional[Scenario]
) -> ID:
"""Create a new scenario to be simulated."""
if not scenario:
Expand Down
2 changes: 2 additions & 0 deletions api/src/api/app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Scenario(SQLModel, table=True):
timestampSubmitted: Optional[datetime] = Field(default=None, nullable=True)
timestampSimulated: Optional[datetime] = Field(default=None, nullable=True)

creatorUserId: Optional[uuid.UUID] = Field(default=None, nullable=True)
creatorOrgId: Optional[str] = Field(default=None, nullable=True)

class ParameterDefinition(SQLModel, table=True):
id: Optional[uuid.UUID] = Field(default_factory=uuid.uuid4, primary_key=True, nullable=False)
Expand Down
4 changes: 4 additions & 0 deletions api/src/api/app/db/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ def scenario_create(scenario: Scenario) -> ID:
percentiles=','.join([str(perc) for perc in scenario.percentiles]) if scenario.percentiles else '50',
timestampSubmitted=datetime.now(),
timestampSimulated=None,
creatorUserId=scenario.creator_user_id,
creatorOrgId=scenario.creator_org_id
)
with next(get_session()) as session:
nested_dict = lambda: defaultdict(nested_dict)
Expand Down Expand Up @@ -536,6 +538,8 @@ def scenario_get_by_id(id: StrictStr) -> Scenario:
percentiles=[int(perc) for perc in scenario.percentiles.split(',')] if scenario.percentiles else [50],
timestampSubmitted=scenario.timestampSubmitted,
timestampSimulated=scenario.timestampSimulated,
creator_user_id=str(scenario.creatorUserId),
creator_org_id=scenario.creatorOrgId
)

def scenario_get_all() -> List[ReducedScenario]:
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/app/middlewares/authentication_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def dispatch(self, request, call_next):
# async def get_user(request: Request):
# return request.state.user
request.state.user = user

request.state.realm = realm
# (Optional) role check can be added
# if ['admin'] not in user.role:
# raise HTTPException(
Expand Down
3 changes: 3 additions & 0 deletions api/src/api/app/models/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class Scenario(BaseModel):
timestamp_simulated: Optional[datetime] = Field(default=None, alias="timestampSimulated", description="Timestamp when the scenario was finished simulating and data is available")
__properties: ClassVar[List[str]] = ["id", "name", "description", "startDate", "endDate", "modelId", "modelParameters", "nodeListId", "linkedInterventions", "percentiles", "timestampSubmitted", "timestampSimulated"]

creator_user_id: Optional[str] = None
creator_org_id: Optional[str] = None

model_config = {
"populate_by_name": True,
"validate_assignment": True,
Expand Down