Skip to content

Commit

Permalink
Merge pull request #114 from ssi-dk/feat/use-csv-in-workspace-data
Browse files Browse the repository at this point in the history
feat: use csv in workspace data
  • Loading branch information
sjkp authored Oct 18, 2024
2 parents e9e14bb + f7ff80e commit fda91df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 38 deletions.
3 changes: 2 additions & 1 deletion web/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ openapi-spec-validator==0.2.9
pydantic==2.8.2
hkdf
python-keycloak
flask-cors
flask-cors
flask
2 changes: 1 addition & 1 deletion web/src/SAP/src/controllers/WorkspacesController.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def get_workspace_data(user, token_info, workspace_id):
if res is None:
return abort(404)

return jsonify(res["values"])
return res
97 changes: 61 additions & 36 deletions web/src/SAP/src/repositories/workspaces.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
from typing import Any, Dict, List, TypedDict, Union

from bson.objectid import ObjectId

from ...src.security.permission_check import authorized_columns

from ...generated.models.update_workspace import UpdateWorkspace
from ...src.repositories.analysis import get_analysis_with_metadata, get_single_analysis_by_object_id
from ...src.repositories.analysis import (
get_analysis_with_metadata,
get_single_analysis_by_object_id,
)
from ...common.database import get_collection, WORKSPACES_COL_NAME
from ...generated.models import CreateWorkspace
from ...generated.models import CloneWorkspace
import io
from flask import send_file


def trim(item):
item["id"] = str(item["_id"])
item.pop("_id", None)
item.pop("username", None)
return item


def get_sequence(sample_id: str):
single = get_single_analysis_by_object_id(sample_id)
sequence = get_analysis_with_metadata(single["sequence_id"])
return sequence


def get_workspaces(user: str):
workspaces = get_collection(WORKSPACES_COL_NAME)
return list(map(trim, workspaces.find({"created_by": user})))


def get_workspace(user: str, workspace_id: str):
workspaces = get_collection(WORKSPACES_COL_NAME)
if ObjectId.is_valid(workspace_id):
workspace = trim(workspaces.find_one({"created_by": user, "_id": ObjectId(workspace_id)}))
workspace = trim(
workspaces.find_one({"created_by": user, "_id": ObjectId(workspace_id)})
)
else:
workspace = trim(workspaces.find_one({"created_by": user, "name": workspace_id}))
workspace = trim(
workspaces.find_one({"created_by": user, "name": workspace_id})
)

if workspace is None:
return workspace
Expand All @@ -42,42 +52,40 @@ def get_workspace(user: str, workspace_id: str):

return workspace

WorkspaceData = TypedDict('WorkspaceData', {'keys': List[str], 'values': List[List[Any]]})

def get_workspace_data(user: str, token_info: Dict[str, str], workspace_id: str) -> Union[WorkspaceData,None]:
def get_workspace_data(user: str, token_info: Dict[str, str], workspace_id: str):
workspace = get_workspace(user, workspace_id)

if workspace is None:
return workspace
return None

authorized = authorized_columns(token_info)

values = []
if len(authorized) == 0:
return None

csv = ",".join(f'"{column}"' for column in authorized)
for sample in workspace["samples"]:
row = []
for column in authorized:
if column in sample:
row.append(sample[column])
else:
row.append("")
values.append(row)

return {
"keys": authorized,
"values": values
}
csv += "\n" + ",".join(
f'"{sample[column]}"' if sample[column] else ""
for column in authorized
if column in sample
)

file = io.BytesIO(bytes(csv, "utf-8"))
return send_file(file, download_name="proj.csv", mimetype="text/csv")


def delete_workspace(user: str, workspace_id: str):
workspaces = get_collection(WORKSPACES_COL_NAME)

return workspaces.delete_one(
{"_id": ObjectId(workspace_id), "created_by": user}
)
return workspaces.delete_one({"_id": ObjectId(workspace_id), "created_by": user})


def delete_workspace_sample(user: str, workspace_id: str, sample_id: str):
workspaces = get_collection(WORKSPACES_COL_NAME)

update_filter = {'created_by': user, '_id': ObjectId(workspace_id)}
update_filter = {"created_by": user, "_id": ObjectId(workspace_id)}
update = {"$pull": {"samples": sample_id}}
return workspaces.update_one(update_filter, update, upsert=True)

Expand All @@ -87,36 +95,53 @@ def create_workspace(user: str, workspace: CreateWorkspace):

if workspace.samples is None:
workspace.samples = []

record = {**workspace.to_dict(), "created_by": user}
return workspaces.update_one({'created_by': user, 'name': workspace.name}, {"$set": record}, upsert=True)
return workspaces.update_one(
{"created_by": user, "name": workspace.name}, {"$set": record}, upsert=True
)


def clone_workspace(user: str, cloneWorkspaceInfo: CloneWorkspace):
workspaces = get_collection(WORKSPACES_COL_NAME)

if ObjectId.is_valid(cloneWorkspaceInfo.id):
workspace = trim(workspaces.find_one({"created_by": user, "_id": ObjectId(cloneWorkspaceInfo.id)}))
workspace = trim(
workspaces.find_one(
{"created_by": user, "_id": ObjectId(cloneWorkspaceInfo.id)}
)
)
else:
workspace = trim(workspaces.find_one({"created_by": user, "name": cloneWorkspaceInfo.id}))
workspace = trim(
workspaces.find_one({"created_by": user, "name": cloneWorkspaceInfo.id})
)

if workspace is None:
return None

if workspace["samples"] is None:
if workspace["samples"] is None:
workspace["samples"] = []

return workspaces.insert_one({'created_by': user, 'name': cloneWorkspaceInfo.name, 'samples': workspace["samples"]})
return workspaces.insert_one(
{
"created_by": user,
"name": cloneWorkspaceInfo.name,
"samples": workspace["samples"],
}
)


def update_workspace(user: str, workspace_id: str, workspace: UpdateWorkspace):
workspaces = get_collection(WORKSPACES_COL_NAME)

update_filter = {'created_by': user, '_id': ObjectId(workspace_id)}
update = {"$addToSet": {"samples": { "$each": workspace.samples}}}
update_filter = {"created_by": user, "_id": ObjectId(workspace_id)}
update = {"$addToSet": {"samples": {"$each": workspace.samples}}}
return workspaces.update_one(update_filter, update, upsert=True)


def update_microreact(microreact_reference):
workspaces = get_collection(WORKSPACES_COL_NAME)

update_filter = {'_id': ObjectId(microreact_reference['id'])}
update = {"$set": {"microreact": microreact_reference['microreact']}}
update_filter = {"_id": ObjectId(microreact_reference["id"])}
update = {"$set": {"microreact": microreact_reference["microreact"]}}
return workspaces.update_one(update_filter, update, upsert=True)

0 comments on commit fda91df

Please sign in to comment.