50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
import logging
|
|
logger = logging.getLogger(__name__)
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from app.services.auth import auth_is_admin
|
|
from sqlmodel import Session, select
|
|
import sqlalchemy.exc as exc
|
|
from app.model.models import *
|
|
from app.services.database import get_session, add_and_refresh
|
|
|
|
debug_router = APIRouter(
|
|
prefix="/api/v1/debug",
|
|
tags=["Debug items - maybe dont show this in UI"],
|
|
dependencies=[Depends(auth_is_admin)],
|
|
)
|
|
|
|
@debug_router.put("/addcard/")
|
|
def add_card_manually(groupid: int, card_key: str, name: str, enabled: bool, db: Session=Depends(get_session)):
|
|
"""Add cards manually (you also have to delete them manually)"""
|
|
logger.critical(f"Manual db change: adding a card with key: {card_key} to group: {groupid}")
|
|
card = Card(
|
|
group_id=groupid,
|
|
key=card_key,
|
|
name=name,
|
|
enabled=enabled,
|
|
card_serial="00:00:00:00:00:00:00"
|
|
)
|
|
add_and_refresh(db, card)
|
|
return card
|
|
|
|
@debug_router.get("/rmcard/{card_id}")
|
|
def remove_card_manually(card_id: str, db: Session = Depends(get_session)):
|
|
try:
|
|
card = db.exec(select(Card).where(Card.id == card_id)).one()
|
|
except exc.NoResultFound:
|
|
raise HTTPException(status_code=500, detail="No card with that id found.")
|
|
logger.critical(f"Manual db change: removing a card with attrs: {card}")
|
|
db.delete(card)
|
|
db.commit()
|
|
return {"message": "Card deleted successfully"}
|
|
|
|
|
|
@debug_router.put("/getcards")
|
|
def list_all_cards(db: Session = Depends(get_session)):
|
|
logger.info(f"Debug Setting: Getting cards.")
|
|
cards = db.exec(select(Card)).all()
|
|
print(cards)
|
|
out = []
|
|
for i in cards:
|
|
out.append({i.key: i.group.name})
|
|
return out |