INNER CODE UNIT · Python
authors_add
marty331/fasthtmx · main.py:42
def authors_add(request: Request):
return templates.TemplateResponse('authors/partials/add_authors_form.html', {"request": request})
@app.post("/authors/add")
def create_author(request: Request, email: str = Form(...), first_name: str = Form(...), last_name: str = Form(...), db: Session = Depends(get_db)):
db_author = dbs.get_author_by_email(db, email=email)
if db_author:
raise HTTPException(status_code=400, detail="Email already registered")
author = schema.AuthorCreate(last_name=last_name, first_name=first_name, email=email)
dbs.create_author(db=db, author=author)
url = request.headers.get('HX-Current-URL').split('/')[-1]
if request.headers.get('HX-Request') and url == 'authors':
return templates.TemplateResponse('authors/partials/show_add_author_form.html', {"request": request})
elif request.headers.get('HX-Request') and url == '':
return templates.TemplateResponse('books/partials/show_add_form.html', {"request": request})
else:
pass