Skip to content
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

feat: Update GitHub functions #368

Merged
merged 19 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
51 changes: 14 additions & 37 deletions backend/editor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
Taxonomy Editor Backend API
"""
import logging
import os

# Required imports
# ------------------------------------------------------------------------------------#
from datetime import datetime
from enum import Enum
from typing import Optional

# FastAPI
Expand All @@ -29,13 +27,17 @@

# DB helper imports
from . import graph_db

# Controller imports
from .controllers.project_controller import edit_project
from .entries import TaxonomyGraph

# Custom exceptions
from .exceptions import GithubBranchExistsError, GithubUploadError

# Data model imports
from .models import Footer, Header
from .models.node_models import Footer, Header
from .models.project_models import ProjectEdit, ProjectStatus

# -----------------------------------------------------------------------------------#

Expand Down Expand Up @@ -103,27 +105,6 @@ def check_single(id):
raise HTTPException(status_code=500, detail="Multiple entries found")


def file_cleanup(filepath):
"""
Helper function to delete a taxonomy file from local storage
"""
try:
os.remove(filepath)
except FileNotFoundError:
log.warn(f"Taxonomy file {filepath} not found for deletion")
eric-nguyen-cs marked this conversation as resolved.
Show resolved Hide resolved


class StatusFilter(str, Enum):
"""
Enum for project status filter
"""

OPEN = "OPEN"
CLOSED = "CLOSED"
LOADING = "LOADING"
FAILED = "FAILED"


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
reformatted_errors = []
Expand Down Expand Up @@ -168,7 +149,7 @@ async def pong(response: Response):


@app.get("/projects")
async def list_all_projects(response: Response, status: Optional[StatusFilter] = None):
async def list_all_projects(response: Response, status: Optional[ProjectStatus] = None):
"""
List projects created in the Taxonomy Editor with a status filter
"""
Expand All @@ -180,13 +161,13 @@ async def list_all_projects(response: Response, status: Optional[StatusFilter] =

@app.get("{taxonomy_name}/{branch}/set-project-status")
async def set_project_status(
response: Response, branch: str, taxonomy_name: str, status: Optional[StatusFilter] = None
response: Response, branch: str, taxonomy_name: str, status: Optional[ProjectStatus] = None
):
"""
Set the status of a Taxonomy Editor project
"""
taxonomy = TaxonomyGraph(branch, taxonomy_name)
result = await taxonomy.set_project_status(status)
result = await edit_project(taxonomy.project_name, ProjectEdit(status=status))
return result


Expand Down Expand Up @@ -343,10 +324,8 @@ async def export_to_text_file(
response: Response, branch: str, taxonomy_name: str, background_tasks: BackgroundTasks
):
taxonomy = TaxonomyGraph(branch, taxonomy_name)
file = await taxonomy.file_export()
file = await taxonomy.file_export(background_tasks)

# Add a background task for removing exported taxonomy file
background_tasks.add_task(file_cleanup, file)
return FileResponse(file)


Expand All @@ -356,9 +335,7 @@ async def export_to_github(
):
taxonomy = TaxonomyGraph(branch, taxonomy_name)
try:
url, file = await taxonomy.github_export()
# Add a background task for removing exported taxonomy file
background_tasks.add_task(file_cleanup, file)
url = await taxonomy.github_export(background_tasks)
return url

except GithubBranchExistsError:
Expand Down Expand Up @@ -387,10 +364,10 @@ async def import_from_github(
raise HTTPException(status_code=422, 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():
if not await taxonomy.is_branch_unique(from_github=True):
raise HTTPException(status_code=409, detail="branch_name: Branch name should be unique!")

status = await taxonomy.import_from_github(description, background_tasks)
status = await taxonomy.import_taxonomy(description, background_tasks)
return status


Expand All @@ -410,10 +387,10 @@ async def upload_taxonomy(
raise HTTPException(status_code=422, 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():
if not await taxonomy.is_branch_unique(from_github=False):
raise HTTPException(status_code=409, detail="branch_name: Branch name should be unique!")

result = await taxonomy.import_from_github(description, background_tasks, file)
result = await taxonomy.import_taxonomy(description, background_tasks, file)

return result

Expand Down
41 changes: 41 additions & 0 deletions backend/editor/controllers/project_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ..graph_db import get_current_transaction
from ..models.project_models import Project, ProjectCreate, ProjectEdit


async def get_project(project_id: str) -> Project:
"""
Get project by id
"""
query = """
MATCH (p:PROJECT {id: $project_id})
RETURN p
"""
params = {"project_id": project_id}
result = await get_current_transaction().run(query, params)
return Project(**(await result.single())["p"])


async def create_project(project: ProjectCreate):
"""
Create project
"""
query = """
CREATE (p:PROJECT $project) SET p.created_at = datetime()
"""
params = {"project": project.model_dump()}
await get_current_transaction().run(query, params)


async def edit_project(project_id: str, project_edit: ProjectEdit):
"""
Edit project
"""
query = """
MATCH (p:PROJECT {id: $project_id})
SET p += $project_edit
"""
params = {
"project_id": project_id,
"project_edit": project_edit.model_dump(exclude_unset=True),
}
await get_current_transaction().run(query, params)
Loading
Loading