Skip to content

Commit

Permalink
feat: add taxonomy file upload endpoint (#196)
Browse files Browse the repository at this point in the history
* feat: add taxonomy file upload endpoint

* format

* exception handling and param taxonomy name

* error codes fix
  • Loading branch information
diivi committed Jan 25, 2023
1 parent 99e4c39 commit 115c76d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
44 changes: 40 additions & 4 deletions backend/editor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
import logging
import os
import shutil
import tempfile

# Required imports
# ------------------------------------------------------------------------------------#
Expand All @@ -11,7 +13,16 @@
from typing import Optional

# FastAPI
from fastapi import BackgroundTasks, FastAPI, HTTPException, Request, Response, status
from fastapi import (
BackgroundTasks,
FastAPI,
Form,
HTTPException,
Request,
Response,
UploadFile,
status,
)
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
Expand Down Expand Up @@ -359,16 +370,41 @@ async def import_from_github(request: Request, branch: str, taxonomy_name: str):

taxonomy = TaxonomyGraph(branch, taxonomy_name)
if not taxonomy.is_valid_branch_name():
raise HTTPException(status_code=500, detail="Enter a valid branch name!")
raise HTTPException(status_code=400, detail="branch_name: Enter a valid branch name!")
if await taxonomy.does_project_exist():
raise HTTPException(status_code=500, detail="Project already exists!")
raise HTTPException(status_code=409, detail="Project already exists!")
if not await taxonomy.is_branch_unique():
raise HTTPException(status_code=500, detail="Branch name should be unique!")
raise HTTPException(status_code=409, detail="branch_name: Branch name should be unique!")

result = await taxonomy.import_from_github(description)
return result


@app.post("/{taxonomy_name}/{branch}/upload")
async def upload_taxonomy(
branch: str, taxonomy_name: str, file: UploadFile, description: str = Form(...)
):
"""
Upload taxonomy file to be parsed
"""
# use the file name as the taxonomy name
taxonomy = TaxonomyGraph(branch, taxonomy_name)
if not taxonomy.is_valid_branch_name():
raise HTTPException(status_code=400, detail="branch_name: Enter a valid branch name!")
if await taxonomy.does_project_exist():
raise HTTPException(status_code=409, detail="Project already exists!")
if not await taxonomy.is_branch_unique():
raise HTTPException(status_code=409, detail="branch_name: Branch name should be unique!")

with tempfile.TemporaryDirectory(prefix="taxonomy-") as tmpdir:
filepath = f"{tmpdir}/{file.filename}"
with open(filepath, "wb") as f:
shutil.copyfileobj(file.file, f)
result = await taxonomy.upload_taxonomy(filepath, description)

return result


@app.post("/{taxonomy_name}/{branch}/nodes")
async def create_node(request: Request, branch: str, taxonomy_name: str):
"""
Expand Down
12 changes: 12 additions & 0 deletions backend/editor/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ async def import_from_github(self, description):
except Exception as e:
raise TaxnonomyImportError() from e

async def upload_taxonomy(self, filepath, description):
"""
Helper function to upload a taxonomy file and create a project node
"""
try:
status = await self.parse_taxonomy(filepath)
async with TransactionCtx():
await self.create_project(description)
return status
except Exception as e:
raise TaxnonomyImportError() from e

def dump_taxonomy(self):
"""
Helper function to create the txt file of a taxonomy
Expand Down
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ uvloop==0.16.0
watchfiles==0.18.1
websockets==10.3
PyGithub==1.56
python-multipart==0.0.5
-e ../parser/

0 comments on commit 115c76d

Please sign in to comment.