Skip to content

Commit

Permalink
Allow specifying the model instance project_id separately from the mo…
Browse files Browse the repository at this point in the history
…del project_id, for cases where the model has already been published and we're adding a new instance.
  • Loading branch information
apdavison committed Oct 14, 2024
1 parent 3b28062 commit d28afb0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion validation_service_api/validation_service/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def to_kg_object(self, client):
class ModelInstance(BaseModel):
id: UUID = None
uri: HttpUrl = None
project_id: str = None
version: str
description: str = None
parameters: HttpUrl = None
Expand All @@ -326,6 +327,8 @@ class Config: # can probably remove this
def from_kg_query(cls, item, client):
item["id"] = client.uuid_from_uri(item["uri"])
item["model_id"] = client.uuid_from_uri(item["model_id"])
space = item["project_id"] # what the query calls "project_id" is really the space
item["project_id"] = project_id_from_space(space)
if item["timestamp"]:
item["timestamp"] = datetime.fromisoformat(item["timestamp"]).date()
item.pop("repository", None)
Expand Down Expand Up @@ -408,6 +411,7 @@ def from_kg_object(cls, instance, client, model_id, scope):
instance_data = {
"id": instance.uuid,
"uri": instance.id,
"project_id": project_id_from_space(instance.space),
"version": instance.version_identifier or "unknown",
"description": instance.version_innovation,
"parameters": None, # todo: get from instance.input_data
Expand Down Expand Up @@ -496,7 +500,7 @@ class ScientificModel(BaseModel):
alias: str = None
author: List[Person]
owner: List[Person]
project_id: str = None # make this required?
project_id: str
organization: str = None
private: bool = True
cell_type: CellType = None
Expand Down
19 changes: 14 additions & 5 deletions validation_service_api/validation_service/resources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
ScientificModelPatch,
ModelInstance,
ModelInstancePatch,
project_id_from_space,
special_spaces
)
from ..queries import build_model_project_filters, model_alias_exists, expand_combinations

Expand Down Expand Up @@ -596,13 +598,20 @@ async def create_model_instance(
kg_service_client = get_kg_client_for_service_account()
model_project = _get_model_by_id_or_alias(model_id, kg_user_client, scope="any")
# check permissions for this model
if model_project.space is None:
collab_id = model_instance.project_id
if collab_id is None:
if model_project.space is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Unable to determine access permissions - please contact EBRAINS support"
)
else:
collab_id = project_id_from_space(model_project.space)
if collab_id in special_spaces:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Unable to determine access permissions - please contact EBRAINS support"
status_code=status.HTTP_403_FORBIDDEN,
detail=f"Cannot create new model instances in space {collab_id}, please use a private or collab space",
)
collab_id = model_project.space[len("collab-"):]
assert len(collab_id) > 0, f"{model_id} {model_project.space}"
if not (
await user.can_edit_collab(collab_id)
or await user.is_admin()
Expand Down

0 comments on commit d28afb0

Please sign in to comment.