generated from MinBZK/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding more structure and database support
- Loading branch information
1 parent
dad1fc6
commit ff5d50e
Showing
11 changed files
with
132 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
from typing import Any | ||
|
||
from fastapi import APIRouter, Request | ||
from fastapi.responses import HTMLResponse | ||
from fastapi.templating import Jinja2Templates | ||
|
||
from tad.models.task import MoveTask | ||
from tad.services.tasks import TasksService | ||
|
||
router = APIRouter() | ||
|
||
tasks_service = TasksService() | ||
templates = Jinja2Templates(directory="tad/site/templates") | ||
|
||
|
||
@router.get("/") | ||
async def test(): | ||
return [{"username": "Rick"}, {"username": "Morty"}] | ||
|
||
|
||
@router.post("/move", response_class=HTMLResponse) | ||
async def move_task(request: Request): | ||
json = await request.json() | ||
task = tasks_service.move_task( | ||
int(json["taskId"]), int(json["statusId"]), json["previousSiblingId"], json["nextSiblingId"] | ||
async def move_task(request: Request, move_task: MoveTask) -> HTMLResponse: | ||
""" | ||
Move a task through an API call. | ||
:param request: the request object | ||
:param move_task: the move task object | ||
:return: a HTMLResponse object, in this case the html code of the card that was moved | ||
""" | ||
task = TasksService.move_task( | ||
move_task.id, | ||
move_task.status_id, | ||
convert_to_int_if_is_int(move_task.previous_sibling_id), | ||
convert_to_int_if_is_int(move_task.next_sibling_id), | ||
) | ||
return templates.TemplateResponse(request=request, name="task.jinja", context={"task": task}) | ||
|
||
|
||
def convert_to_int_if_is_int(value: Any) -> int | Any: | ||
# If the given value is of type integer, convert it to integer, otherwise return the given value | ||
if isinstance(value, int): | ||
return int(value) | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import logging | ||
|
||
from tad.core.singleton import Singleton | ||
from tad.repositories.statuses import StatusesRepository | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class StatusesService(metaclass=Singleton): | ||
__statuses_repository = StatusesRepository() | ||
class StatusesService: | ||
@staticmethod | ||
def get_status(status_id): | ||
return StatusesRepository.find_by_id(status_id) | ||
|
||
def __init__(self): | ||
logger.info("Statuses service initialized") | ||
# TODO find out why logging is not visible | ||
|
||
def get_status(self, status_id): | ||
return self.__statuses_repository.find_by_id(status_id) | ||
|
||
def get_statuses(self) -> []: | ||
return self.__statuses_repository.find_all() | ||
@staticmethod | ||
def get_statuses() -> []: | ||
return StatusesRepository.find_all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from fastapi.testclient import TestClient | ||
from tad.models.task import MoveTask | ||
|
||
|
||
def test_get_root(client: TestClient) -> None: | ||
move_task: MoveTask = MoveTask(taskId="1", statusId="2", previousSiblingId="3", nextSiblingId="4") | ||
print(move_task.model_dump()) | ||
response = client.post("/tasks/move", data=move_task.model_dump()) | ||
assert response.status_code == 200 | ||
assert response.headers["content-type"] == "text/html; charset=utf-8" | ||
|
||
assert b"<h1>Welcome to the Home Page</h1>" in response.content |