搭建期货量化管理后台 MVP,支持行情查看、博弈分析、持仓录入、品种合约管理
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from app.database import get_db
|
||||
from app.models import Product, Contract
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
|
||||
EXCHANGES = ["CZCE", "DCE", "SHFE", "CFFEX", "INE"]
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
def admin_page(request: Request, db: Session = Depends(get_db)):
|
||||
products = db.query(Product).order_by(Product.code).all()
|
||||
|
||||
product_data = []
|
||||
for p in products:
|
||||
contracts = (
|
||||
db.query(Contract)
|
||||
.filter(Contract.product_id == p.id)
|
||||
.order_by(Contract.code)
|
||||
.all()
|
||||
)
|
||||
product_data.append({
|
||||
"id": p.id,
|
||||
"code": p.code,
|
||||
"name": p.name,
|
||||
"exchange": p.exchange,
|
||||
"contracts": [
|
||||
{"id": c.id, "code": c.code, "name": c.name, "is_active": c.is_active}
|
||||
for c in contracts
|
||||
],
|
||||
})
|
||||
|
||||
template = request.app.state.templates.get_template("admin.html")
|
||||
return HTMLResponse(
|
||||
template.render(
|
||||
request=request,
|
||||
active_nav="admin",
|
||||
products=product_data,
|
||||
exchanges=EXCHANGES,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@router.post("/product")
|
||||
def create_product(
|
||||
request: Request,
|
||||
code: str = Form(...),
|
||||
name: str = Form(...),
|
||||
exchange: str = Form(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
existing = db.query(Product).filter(Product.code == code.upper()).first()
|
||||
if not existing:
|
||||
p = Product(code=code.upper(), name=name, exchange=exchange)
|
||||
db.add(p)
|
||||
db.commit()
|
||||
return RedirectResponse("/admin/", status_code=303)
|
||||
|
||||
|
||||
@router.post("/contract")
|
||||
def create_contract(
|
||||
request: Request,
|
||||
product_id: int = Form(...),
|
||||
code: str = Form(...),
|
||||
name: str = Form(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
existing = db.query(Contract).filter(Contract.code == code.upper()).first()
|
||||
if not existing:
|
||||
c = Contract(
|
||||
product_id=product_id,
|
||||
code=code.upper(),
|
||||
name=name,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(c)
|
||||
db.commit()
|
||||
return RedirectResponse("/admin/", status_code=303)
|
||||
|
||||
|
||||
@router.post("/contract/{contract_id}/toggle")
|
||||
def toggle_contract(contract_id: int, db: Session = Depends(get_db)):
|
||||
c = db.query(Contract).filter(Contract.id == contract_id).first()
|
||||
if c:
|
||||
c.is_active = not c.is_active
|
||||
db.commit()
|
||||
return RedirectResponse("/admin/", status_code=303)
|
||||
|
||||
|
||||
@router.post("/contract/{contract_id}/delete")
|
||||
def delete_contract(contract_id: int, db: Session = Depends(get_db)):
|
||||
c = db.query(Contract).filter(Contract.id == contract_id).first()
|
||||
if c:
|
||||
db.delete(c)
|
||||
db.commit()
|
||||
return RedirectResponse("/admin/", status_code=303)
|
||||
|
||||
|
||||
@router.post("/product/{product_id}/delete")
|
||||
def delete_product(product_id: int, db: Session = Depends(get_db)):
|
||||
p = db.query(Product).filter(Product.id == product_id).first()
|
||||
if p:
|
||||
db.delete(p)
|
||||
db.commit()
|
||||
return RedirectResponse("/admin/", status_code=303)
|
||||
Reference in New Issue
Block a user