WTForms, SQLAlchemy
This commit is contained in:
parent
5f328d82d3
commit
03e4c63d38
14 changed files with 227 additions and 22 deletions
|
@ -13,7 +13,7 @@ class MainPaths(paths.Paths):
|
|||
def add_paths(self) -> None:
|
||||
|
||||
@self.app.get('/')
|
||||
def index(req: Request) -> Response:
|
||||
async def index(req: Request) -> Response:
|
||||
return respond.with_tmpl(
|
||||
'index.html',
|
||||
request=req,
|
||||
|
|
67
app/paths/table.py
Normal file
67
app/paths/table.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
from sqlalchemy.orm import Session
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi import Request, Response
|
||||
|
||||
from starlette_wtf import csrf_protect
|
||||
|
||||
from . import paths
|
||||
from .. import respond
|
||||
from ..sql import db
|
||||
from ..sql import crud
|
||||
from ..sql import schemas
|
||||
from ..forms.users import AddUserForm
|
||||
|
||||
LIMIT = 10
|
||||
|
||||
|
||||
class TablePaths(paths.Paths):
|
||||
|
||||
def add_paths(self) -> None:
|
||||
|
||||
@self.app.get('/db')
|
||||
def list_users(
|
||||
req: Request,
|
||||
page: int = 0,
|
||||
db: Session = Depends(db.get_db)) -> Response:
|
||||
|
||||
return respond.with_tmpl(
|
||||
'table.html',
|
||||
request=req,
|
||||
rows=crud.get_users(
|
||||
db=db,
|
||||
skip=(page * LIMIT),
|
||||
limit=LIMIT,
|
||||
),
|
||||
)
|
||||
|
||||
@self.app.get('/add')
|
||||
@self.app.post('/add')
|
||||
@csrf_protect
|
||||
async def add_form(
|
||||
req: Request,
|
||||
db_s: Session = Depends(db.get_db)) -> Response:
|
||||
|
||||
form = await AddUserForm.from_formdata(request=req)
|
||||
|
||||
if await form.validate_on_submit():
|
||||
|
||||
if form.pswd.data != '1234':
|
||||
return respond.with_text('Incorrect password')
|
||||
|
||||
crud.create_user(
|
||||
db=db_s,
|
||||
user=schemas.UserCreate(
|
||||
email=form.email.data,
|
||||
name=form.name.data,
|
||||
age=form.age.data or 0,
|
||||
),
|
||||
)
|
||||
|
||||
return respond.with_redirect('/db')
|
||||
|
||||
return respond.with_tmpl(
|
||||
'admin.html',
|
||||
request=req,
|
||||
form=form,
|
||||
)
|
Loading…
Add table
Reference in a new issue