make buttons submit and cancel work
This commit is contained in:
@@ -25,5 +25,7 @@ def get_issue_details(issue: Issue) -> IssueDetailsResponse:
|
||||
return response
|
||||
|
||||
|
||||
def update_comic(comic: ComicSchema, comic_id: AnyStr, db: Session):
|
||||
def update_comic(comic: ComicSchema, comic_id: AnyStr, db: Session) -> type[Comic] | None:
|
||||
logger.info(f"update_comic: {comic} with {comic_id}")
|
||||
comic = db.get(Comic, comic_id)
|
||||
return comic
|
||||
|
||||
@@ -23,6 +23,7 @@ class ComicDetailsResponse(BaseModel):
|
||||
|
||||
|
||||
class ComicSchema(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
weblink: Optional[AnyUrl]
|
||||
completed: Optional[bool]
|
||||
|
||||
@@ -17,26 +17,38 @@
|
||||
|
||||
<div class="row my-5">
|
||||
<h3 class="text-center display-4">Edit an Comic entry</h3>
|
||||
<form method="POST">
|
||||
<form class="form-horizontal" method="POST">
|
||||
<div class="form-group">
|
||||
<label for="title">Title</label>
|
||||
<input type="text" class="form-control" id="title" name="title" value="{{comic_title}}" placeholder="Comic title here">
|
||||
<label class="control-label col-sm-2" for="title">Title</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="title" name="title" value="{{comic_title}}" placeholder="Comic title here">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="weblink">Link</label>
|
||||
<input type="text" class="form-control" id="weblink" name="weblink" value="{{comic_weblink}}" placeholder="Web link for comic here">
|
||||
<label class="control-label col-sm-2" for="weblink">Link</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="weblink" name="weblink" value="{{comic_weblink}}" placeholder="Web link for comic here">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<label class="form-check-label" for="completed">Completed</label>
|
||||
<input type="checkbox" id="completed" class="form-check-input" name="completed" value="{{comic_completed}}" placeholder="Is comic series completed?">
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<div class="checkbox">
|
||||
<label><input type="checkbox" id="completed" name="completed" value="{{comic_completed}}"> Completed</label>
|
||||
</div
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<label class="form-check-label" for="current_order">Completed</label>
|
||||
<input type="checkbox" id="current_order" class="form-check-input" name="current_order" value="{{comic_completed}}" placeholder="Is comic series completed?">
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<div class="checkbox">
|
||||
<label><input type="checkbox" id="current_order" name="current_order" value="{{comic_current_order}}"> Current Order</label>
|
||||
</div
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
<button type="cancel" class="btn btn-primary">Cancel</button>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<button type="submit" class="btn btn-primary" name="action" value="submit">Submit</button>
|
||||
<button type="cancel" class="btn btn-primary" name="action" value="cancel">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -3,16 +3,25 @@ from typing import List, Optional
|
||||
|
||||
|
||||
class ValidateComicForm:
|
||||
def __init__(self, request: Request):
|
||||
def __init__(self, request: Request, comic_id: str, completed: bool, current_order: bool):
|
||||
self.request = request
|
||||
self.errors: List = []
|
||||
self.id = comic_id
|
||||
self.title: Optional[str] = None
|
||||
self.weblink: Optional[str] = None
|
||||
self.completed = completed
|
||||
self.current_order = current_order
|
||||
|
||||
async def load_data(self):
|
||||
form = await self.request.form()
|
||||
print(f"{form.keys()}")
|
||||
self.title = form.get("title")
|
||||
self.weblink = form.get("weblink")
|
||||
|
||||
def is_valid(self):
|
||||
if not self.errors:
|
||||
return True
|
||||
return False
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.title=}, {self.weblink=}"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Request, status
|
||||
from fastapi import APIRouter, Form, Request, status
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
@@ -48,8 +48,11 @@ def edit_comic(db: SessionDep, request: Request, comic_id: str):
|
||||
|
||||
|
||||
@router.post("/comic/edit/{comic_id}")
|
||||
async def validate_comic(request: Request, db: SessionDep, comic_id: str):
|
||||
form = ValidateComicForm(request)
|
||||
async def validate_comic(request: Request, db: SessionDep, comic_id: str, action: str = Form(...), completed: bool = Form(False), current_order: bool = Form(False)):
|
||||
if action == "cancel":
|
||||
return RedirectResponse(f"/comic/comics/{comic_id}", status_code=status.HTTP_303_SEE_OTHER)
|
||||
form = ValidateComicForm(request, comic_id, completed, current_order)
|
||||
logger.info(f"request: {repr(request)}")
|
||||
await form.load_data()
|
||||
logger.info(f"form: {form}")
|
||||
if form.is_valid():
|
||||
|
||||
Reference in New Issue
Block a user