Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,9 @@ import time
|
|
| 11 |
from typing import Optional
|
| 12 |
from pydantic import BaseModel
|
| 13 |
import os
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Configure logging
|
| 16 |
logging.basicConfig(
|
|
@@ -101,7 +104,7 @@ class TokenManager:
|
|
| 101 |
self.last_refresh = time.time()
|
| 102 |
logger.info("Successfully refreshed admin token")
|
| 103 |
return token
|
| 104 |
-
|
| 105 |
wait_time = min(5, (attempt + 1) * TOKEN_RETRY_DELAY)
|
| 106 |
logger.warning(f"Attempt {attempt + 1} failed, retrying in {wait_time}s...")
|
| 107 |
await asyncio.sleep(wait_time)
|
|
@@ -118,88 +121,44 @@ token_manager = TokenManager()
|
|
| 118 |
@app.get("/")
|
| 119 |
def root():
|
| 120 |
logger.debug("Root endpoint accessed")
|
| 121 |
-
return {"message": "
|
| 122 |
|
| 123 |
@app.post("/login")
|
| 124 |
async def redirect_login(request: Request):
|
| 125 |
logger.info("Redirecting /login to /auth/login")
|
| 126 |
return RedirectResponse(url="/auth/login", status_code=307)
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
try:
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
specialty
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
"
|
| 146 |
-
"
|
| 147 |
}
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
async with aiohttp.ClientSession() as session:
|
| 152 |
-
async with session.post(
|
| 153 |
-
doctor_url,
|
| 154 |
-
json=payload.dict(),
|
| 155 |
-
headers=headers,
|
| 156 |
-
timeout=15
|
| 157 |
-
) as response:
|
| 158 |
-
logger.debug(f"Doctor creation response status: {response.status}, URL: {response.url}")
|
| 159 |
-
if response.status == 201:
|
| 160 |
-
return "β
Doctor created successfully!"
|
| 161 |
-
elif response.status == 401:
|
| 162 |
-
logger.warning("Token expired, attempting refresh...")
|
| 163 |
-
token = await token_manager.refresh_token()
|
| 164 |
-
headers["Authorization"] = f"Bearer {token}"
|
| 165 |
-
async with session.post(
|
| 166 |
-
doctor_url,
|
| 167 |
-
json=payload.dict(),
|
| 168 |
-
headers=headers,
|
| 169 |
-
timeout=15
|
| 170 |
-
) as retry_response:
|
| 171 |
-
logger.debug(f"Retry doctor creation response status: {retry_response.status}, URL: {retry_response.url}")
|
| 172 |
-
if retry_response.status == 201:
|
| 173 |
-
return "β
Doctor created successfully!"
|
| 174 |
-
error_detail = await retry_response.text()
|
| 175 |
-
return f"β Error: {error_detail} (Status: {retry_response.status})"
|
| 176 |
-
|
| 177 |
-
error_detail = await response.text()
|
| 178 |
-
return f"β Error: {error_detail} (Status: {response.status})"
|
| 179 |
-
|
| 180 |
-
except HTTPException as e:
|
| 181 |
-
logger.error(f"Doctor creation failed: {str(e)}")
|
| 182 |
-
return f"β Error: {str(e)}"
|
| 183 |
-
except Exception as e:
|
| 184 |
-
logger.error(f"Doctor creation failed: {str(e)}")
|
| 185 |
-
return f"β System Error: {str(e)}"
|
| 186 |
-
|
| 187 |
-
def sync_create_doctor(full_name: str, email: str, license_number: str, specialty: str, password: str):
|
| 188 |
-
return asyncio.run(async_create_doctor(full_name, email, license_number, specialty, password))
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
email=payload.email,
|
| 196 |
-
license_number=payload.license_number,
|
| 197 |
-
specialty=payload.specialty,
|
| 198 |
-
password=payload.password
|
| 199 |
-
)
|
| 200 |
-
return {"result": result}
|
| 201 |
-
|
| 202 |
-
# Test endpoint to verify backend connectivity
|
| 203 |
@app.get("/test-backend")
|
| 204 |
async def test_backend():
|
| 205 |
try:
|
|
|
|
| 11 |
from typing import Optional
|
| 12 |
from pydantic import BaseModel
|
| 13 |
import os
|
| 14 |
+
from db.mongo import users_collection
|
| 15 |
+
from core.security import hash_password
|
| 16 |
+
from datetime import datetime
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(
|
|
|
|
| 104 |
self.last_refresh = time.time()
|
| 105 |
logger.info("Successfully refreshed admin token")
|
| 106 |
return token
|
| 107 |
+
|
| 108 |
wait_time = min(5, (attempt + 1) * TOKEN_RETRY_DELAY)
|
| 109 |
logger.warning(f"Attempt {attempt + 1} failed, retrying in {wait_time}s...")
|
| 110 |
await asyncio.sleep(wait_time)
|
|
|
|
| 121 |
@app.get("/")
|
| 122 |
def root():
|
| 123 |
logger.debug("Root endpoint accessed")
|
| 124 |
+
return {"message": "\ud83d\ude80 FastAPI with MongoDB + JWT is running."}
|
| 125 |
|
| 126 |
@app.post("/login")
|
| 127 |
async def redirect_login(request: Request):
|
| 128 |
logger.info("Redirecting /login to /auth/login")
|
| 129 |
return RedirectResponse(url="/auth/login", status_code=307)
|
| 130 |
|
| 131 |
+
# NEW PUBLIC DOCTOR CREATION ENDPOINT (NO AUTH REQUIRED)
|
| 132 |
+
@app.post("/public/create-doctor")
|
| 133 |
+
async def create_doctor_public(payload: DoctorPayload):
|
| 134 |
try:
|
| 135 |
+
logger.info(f"[PUBLIC] Creating doctor: {payload.email}")
|
| 136 |
+
email = payload.email.strip().lower()
|
| 137 |
+
existing = await users_collection.find_one({"email": email})
|
| 138 |
+
if existing:
|
| 139 |
+
raise HTTPException(status_code=409, detail="Email already exists")
|
| 140 |
+
|
| 141 |
+
hashed_pw = hash_password(payload.password)
|
| 142 |
+
doctor_doc = {
|
| 143 |
+
"email": email,
|
| 144 |
+
"full_name": payload.full_name.strip(),
|
| 145 |
+
"password": hashed_pw,
|
| 146 |
+
"role": "doctor",
|
| 147 |
+
"specialty": payload.specialty,
|
| 148 |
+
"license_number": payload.license_number,
|
| 149 |
+
"created_at": datetime.utcnow().isoformat(),
|
| 150 |
+
"updated_at": datetime.utcnow().isoformat(),
|
| 151 |
+
"device_token": ""
|
| 152 |
}
|
| 153 |
+
result = await users_collection.insert_one(doctor_doc)
|
| 154 |
+
logger.info(f"[PUBLIC] Doctor inserted successfully: {email}")
|
| 155 |
+
return {"status": "success", "id": str(result.inserted_id), "email": email}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
except HTTPException:
|
| 158 |
+
raise
|
| 159 |
+
except Exception as e:
|
| 160 |
+
logger.error(f"[PUBLIC] Failed to create doctor: {str(e)}")
|
| 161 |
+
raise HTTPException(status_code=500, detail="Failed to create doctor")point to verify backend connectivity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
@app.get("/test-backend")
|
| 163 |
async def test_backend():
|
| 164 |
try:
|