4
4
from fastapi import APIRouter , Request , Response
5
5
from passlib .hash import bcrypt , bcrypt_sha256
6
6
from tortoise .transactions import atomic , in_transaction
7
+ from pydantic import BaseModel
8
+ from typing import Optional
7
9
8
10
import pwncore .containerASD as containerASD
9
11
from pwncore .config import config
@@ -51,14 +53,21 @@ async def _del_cont(id: str):
51
53
await container .delete ()
52
54
53
55
56
+ class AdminResponse (BaseModel ):
57
+ success : bool
58
+ message : Optional [str ] = None
59
+
60
+
54
61
@atomic ()
55
- @router .get ("/union" )
62
+ @router .get ("/union" ,
63
+ response_model = AdminResponse ,
64
+ )
56
65
async def calculate_team_coins (
57
66
response : Response , req : Request
58
67
): # Inefficient, anyways will be used only once
59
68
if not bcrypt_sha256 .verify ((await req .body ()).strip (), config .admin_hash ): # Use config.admin_hash
60
69
response .status_code = 401
61
- return
70
+ return AdminResponse ( success = False , message = "Authentication failed" )
62
71
async with in_transaction ():
63
72
logging .info ("Calculating team points form pre-event CTFs:" )
64
73
team_ids = await Team .filter ().values_list ("id" , flat = True )
@@ -82,14 +91,18 @@ async def calculate_team_coins(
82
91
logging .info (f"{ team .id } ) { team .name } : { team .coins } " )
83
92
await team .save ()
84
93
94
+ return AdminResponse (success = True , message = "Team coins updated successfully" )
85
95
86
- @router .get ("/create" )
96
+
97
+ @router .get ("/create" ,
98
+ response_model = AdminResponse
99
+ )
87
100
async def init_db (
88
101
response : Response , req : Request
89
102
): # Inefficient, anyways will be used only once
90
103
if not bcrypt_sha256 .verify ((await req .body ()).strip (), config .admin_hash ):
91
104
response .status_code = 401
92
- return
105
+ return AdminResponse ( success = False , message = "Authentication failed" )
93
106
await Problem .create (
94
107
name = "Invisible-Incursion" ,
95
108
description = "Chod de tujhe se na ho paye" ,
@@ -207,3 +220,5 @@ async def init_db(
207
220
await SolvedProblem .create (team_id = 2 , problem_id = 1 )
208
221
await SolvedProblem .create (team_id = 2 , problem_id = 2 )
209
222
await SolvedProblem .create (team_id = 1 , problem_id = 2 )
223
+
224
+ return AdminResponse (success = True , message = "Database initialized with sample data" )
0 commit comments