Skip to content

Commit

Permalink
Merge pull request #47 from Clinical-Genomics/crud2
Browse files Browse the repository at this point in the history
Crud2
  • Loading branch information
mayabrandi committed Mar 30, 2021
2 parents c3d6972 + 1dcdf6d commit b1fbcd9
Show file tree
Hide file tree
Showing 62 changed files with 591 additions and 431 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,6 +1,14 @@
from fastapi import FastAPI

from NIPTool.server.web_app.api.api_v1.endpoints import batches, index, sample, update, download, statistics, login
from NIPTool.API.external.api.api_v1.endpoints import (
batches,
index,
sample,
update,
download,
statistics,
login,
)

app = FastAPI()

Expand Down
@@ -1,68 +1,96 @@
from fastapi import APIRouter, Depends, Request
from NIPTool.adapter.plugin import NiptAdapter
from NIPTool.crud import find
from NIPTool.models.database import Batch, User
from NIPTool.server.web_app.utils import *
from NIPTool.server.constants import TRISOMI_TRESHOLDS
from NIPTool.server.web_app.api.deps import get_nipt_adapter
from NIPTool.API.external.utils import *
from NIPTool.API.external.constants import TRISOMI_TRESHOLDS
from NIPTool.API.external.api.deps import get_nipt_adapter

from fastapi.templating import Jinja2Templates

router = APIRouter()
templates = Jinja2Templates(directory="templates")
CURRENT_USER=User(username='mayapapaya', email='mayabrandi@123.com', role='RW').dict()
CURRENT_USER = User(username="mayapapaya", email="mayabrandi@123.com", role="RW").dict()


@router.post("/")
def batches(request: Request, adapter: NiptAdapter = Depends(get_nipt_adapter)):#, user: User = Depends(get_current_active_user)):
def batches(
request: Request, adapter: NiptAdapter = Depends(get_nipt_adapter)
): # , user: User = Depends(get_current_active_user)):
"""List of all batches"""

all_batches = list(adapter.batches())
return templates.TemplateResponse("batches.html",
context={"request": request, "batches": all_batches,
"current_user": CURRENT_USER,
"page_id": "all_batches"})
all_batches: List[Batch] = find.batches(adapter=adapter)
return templates.TemplateResponse(
"batches.html",
context={
"request": request,
"batches": all_batches,
"current_user": CURRENT_USER,
"page_id": "all_batches",
},
)


@router.get("/")
def batches(request: Request, adapter: NiptAdapter = Depends(get_nipt_adapter)): #, user: User = Depends(get_current_active_user)):
def batches(
request: Request, adapter: NiptAdapter = Depends(get_nipt_adapter)
): # , user: User = Depends(get_current_active_user)):
"""List of all batches"""

all_batches = list(adapter.batches())
return templates.TemplateResponse("batches.html",
context={"request": request, "batches": all_batches,
"current_user": CURRENT_USER,
"page_id": "all_batches"})
all_batches: List[Batch] = find.batches(adapter=adapter)
return templates.TemplateResponse(
"batches.html",
context={
"request": request,
"batches": all_batches,
"current_user": CURRENT_USER,
"page_id": "all_batches",
},
)


@router.get("/{batch_id}/")
def batch(request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
"""Batch view with table of all samples in the batch."""
samples = adapter.batch_samples(batch_id)
return templates.TemplateResponse('batch/tabs/table.html',
context={"request": request, "batch": adapter.batch(batch_id),
"sample_info": [get_sample_info(sample) for
sample in samples],
"page_id": "batches",
"current_user": CURRENT_USER})

samples: List[Sample] = find.batch_samples(batch_id=batch_id, adapter=adapter)
return templates.TemplateResponse(
"batch/tabs/table.html",
context={
"request": request,
"batch": find.batch(batch_id=batch_id, adapter=adapter),
"sample_info": [get_sample_info(sample) for sample in samples],
"page_id": "batches",
"current_user": CURRENT_USER,
},
)


@router.post("/{batch_id}/")
def batch(request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
"""Batch view with table of all samples in the batch."""
samples = adapter.batch_samples(batch_id)
return templates.TemplateResponse('batch/tabs/table.html',
context={"request": request, "batch": adapter.batch(batch_id),
"sample_info": [get_sample_info(sample) for
sample in samples],
"page_id": "batches",
"current_user": CURRENT_USER})

samples: List[Sample] = find.batch_samples(batch_id=batch_id, adapter=adapter)
return templates.TemplateResponse(
"batch/tabs/table.html",
context={
"request": request,
"batch": find.batch(batch_id=batch_id, adapter=adapter),
"sample_info": [get_sample_info(sample) for sample in samples],
"page_id": "batches",
"current_user": CURRENT_USER,
},
)


@router.get("/{batch_id}/{ncv}")
def NCV(request: Request, batch_id: str, ncv, adapter: NiptAdapter = Depends(get_nipt_adapter)):
"""Batch view with with NCV plot"""
batch: Batch = adapter.batch(batch_id)
u=get_tris_control_normal(adapter, ncv)
batch: Batch = find.batch(batch_id=batch_id, adapter=adapter)

return templates.TemplateResponse(
"batch/tabs/NCV.html", context=dict(
"batch/tabs/NCV.html",
context=dict(
request=request,
tris_thresholds=TRISOMI_TRESHOLDS,
batch=batch.dict(),
Expand All @@ -71,14 +99,18 @@ def NCV(request: Request, batch_id: str, ncv, adapter: NiptAdapter = Depends(get
normal_data=get_tris_control_normal(adapter, ncv),
abnormal_data=get_tris_control_abnormal(adapter, ncv, 0),
page_id=f"batches_NCV{ncv}",
current_user=CURRENT_USER)
current_user=CURRENT_USER,
),
)


@router.get("/batches/{batch_id}/fetal_fraction_XY")
def fetal_fraction_XY(request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
def fetal_fraction_XY(
request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)
):
"""Batch view with fetal fraction (X against Y) plot"""
batch: Batch = adapter.batch(batch_id)
batch: Batch = find.batch(batch_id=batch_id, adapter=adapter)

control = get_ff_control_normal(adapter)
abnormal = get_ff_control_abnormal(adapter)
return templates.TemplateResponse(
Expand All @@ -92,14 +124,18 @@ def fetal_fraction_XY(request: Request, batch_id: str, adapter: NiptAdapter = De
max_x=max(control["FFX"]) + 1,
min_x=min(control["FFX"]) - 1,
batch=batch.dict(),
page_id="batches_FF_XY")
page_id="batches_FF_XY",
),
)


@router.get("/batches/{batch_id}/fetal_fraction")
def fetal_fraction(request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
def fetal_fraction(
request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)
):
"""Batch view with fetal fraction plot"""
batch: Batch = adapter.batch(batch_id)
batch: Batch = find.batch(batch_id=batch_id, adapter=adapter)

return templates.TemplateResponse(
"batch/tabs/FF.html",
context=dict(
Expand All @@ -108,15 +144,17 @@ def fetal_fraction(request: Request, batch_id: str, adapter: NiptAdapter = Depen
control=get_ff_control_normal(adapter),
cases=get_ff_cases(adapter, batch_id),
batch=batch.dict(),
page_id="batches_FF")
page_id="batches_FF",
),
)


@router.get("/batches/{batch_id}/coverage")
def coverage(request: Request, batch_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
"""Batch view with coverage plot"""
batch: Batch = adapter.batch(batch_id)
samples = list(adapter.batch_samples(batch_id))
batch: Batch = find.batch(batch_id=batch_id, adapter=adapter)
samples: List[Sample] = find.batch_samples(batch_id=batch_id, adapter=adapter)

scatter_data = get_scatter_data_for_coverage_plot(samples)
box_data = get_box_data_for_coverage_plot(samples)
return templates.TemplateResponse(
Expand All @@ -128,16 +166,20 @@ def coverage(request: Request, batch_id: str, adapter: NiptAdapter = Depends(get
x_axis=list(range(1, 23)),
scatter_data=scatter_data,
box_data=box_data,
page_id="batches_cov")
page_id="batches_cov",
),
)


@router.get("/batches/{batch_id}/report/{coverage}")
def report(request: Request, batch_id: str, coverage: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
def report(
request: Request, batch_id: str, coverage: str, adapter: NiptAdapter = Depends(get_nipt_adapter)
):
"""Report view, collecting all tables and plots from one batch."""

batch: Batch = adapter.batch(batch_id)
samples = list(adapter.batch_samples(batch_id))
batch: Batch = find.batch(batch_id=batch_id, adapter=adapter)
samples: List[Sample] = find.batch_samples(batch_id=batch_id, adapter=adapter)

scatter_data = get_scatter_data_for_coverage_plot(samples)
box_data = get_box_data_for_coverage_plot(samples)
control = get_ff_control_normal(adapter)
Expand Down Expand Up @@ -168,5 +210,6 @@ def report(request: Request, batch_id: str, coverage: str, adapter: NiptAdapter
x_axis=list(range(1, 23)),
scatter_data=scatter_data,
box_data=box_data,
page_id="batches")
page_id="batches",
),
)
@@ -1,6 +1,6 @@
from fastapi import APIRouter, Depends, Request
from NIPTool.adapter.plugin import NiptAdapter
from NIPTool.server.web_app.api.deps import get_nipt_adapter, get_current_active_user
from NIPTool.API.external.api.deps import get_nipt_adapter, get_current_active_user
from fastapi.templating import Jinja2Templates
from NIPTool.parse.batch import validate_file_path
from pathlib import Path
Expand All @@ -11,7 +11,9 @@


@router.get("/batch_download/{batch_id}/{file_id}")
def batch_download(request: Request, batch_id: str, file_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
def batch_download(
request: Request, batch_id: str, file_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)
):
"""View for batch downloads"""

batch = adapter.batch(batch_id)
Expand All @@ -23,11 +25,15 @@ def batch_download(request: Request, batch_id: str, file_id: str, adapter: NiptA

path = Path(file_path)

return FileResponse(str(path.absolute()), media_type='application/octet-stream', filename=path.name)
return FileResponse(
str(path.absolute()), media_type="application/octet-stream", filename=path.name
)


@router.get("/sample_download/{sample_id}/{file_id}")
def sample_download(request: Request, sample_id: str, file_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)):
def sample_download(
request: Request, sample_id: str, file_id: str, adapter: NiptAdapter = Depends(get_nipt_adapter)
):
"""View for sample downloads"""

sample = adapter.sample(sample_id)
Expand All @@ -37,4 +43,6 @@ def sample_download(request: Request, sample_id: str, file_id: str, adapter: Nip
return RedirectResponse(request.url)

file = Path(file_path)
return FileResponse(str(file.absolute()), media_type='application/octet-stream', filename=file.name)
return FileResponse(
str(file.absolute()), media_type="application/octet-stream", filename=file.name
)
Expand Up @@ -5,15 +5,21 @@
from fastapi.security import OAuth2PasswordRequestForm
from NIPTool.models.server.login import Token, UserInDB, User

from NIPTool.server.web_app.api.deps import get_current_active_user, authenticate_user, create_access_token, \
temp_get_config
from NIPTool.API.external.api.deps import (
get_current_active_user,
authenticate_user,
create_access_token,
temp_get_config,
)
from datetime import timedelta

router = APIRouter()


@router.post("/token", response_model=Token)
def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), config: dict = Depends(temp_get_config)):
def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends(), config: dict = Depends(temp_get_config)
):
user: UserInDB = authenticate_user(form_data.username, form_data.password)
if not user:
raise HTTPException(
Expand All @@ -32,12 +38,14 @@ def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), con
@router.post("/login")
def login(token: Token = Depends(login_for_access_token)):
if token:
headers = {'Authorization': f"{token.get('token_type')} {token.get('access_token')}",
"accept": "application/json"}
headers = {
"Authorization": f"{token.get('token_type')} {token.get('access_token')}",
"accept": "application/json",
}
print(headers)
return RedirectResponse('../batches', headers=headers)
return RedirectResponse("../batches", headers=headers)


#@router.post("/login")
#def login(current_user: User = Security(get_current_active_user, scopes=["items", "me"])):
# @router.post("/login")
# def login(current_user: User = Security(get_current_active_user, scopes=["items", "me"])):
# return RedirectResponse('../batches')

0 comments on commit b1fbcd9

Please sign in to comment.