Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
main.py
CHANGED
|
@@ -53,7 +53,7 @@ site_settings = {
|
|
| 53 |
|
| 54 |
users = {}
|
| 55 |
sessions = {}
|
| 56 |
-
|
| 57 |
data_lock = threading.Lock()
|
| 58 |
|
| 59 |
# -------------------------
|
|
@@ -101,6 +101,7 @@ class UserCreate(BaseModel):
|
|
| 101 |
username: str
|
| 102 |
password: str
|
| 103 |
profile_picture: Optional[str]
|
|
|
|
| 104 |
ip_address: str
|
| 105 |
cnic_no: str
|
| 106 |
gender: str
|
|
@@ -143,6 +144,18 @@ class EndSession(BaseModel):
|
|
| 143 |
lat: float
|
| 144 |
lng: float
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# -------------------------
|
| 147 |
# USER ENDPOINTS (7)
|
| 148 |
# -------------------------
|
|
@@ -165,6 +178,7 @@ def add_user(data: UserCreate):
|
|
| 165 |
|
| 166 |
"username": data.username,
|
| 167 |
"password": data.password,
|
|
|
|
| 168 |
"profile_picture": img_url,
|
| 169 |
"ip_address": data.ip_address,
|
| 170 |
"cnic_no": data.cnic_no,
|
|
@@ -243,6 +257,15 @@ def set_admin(username: str, value: bool):
|
|
| 243 |
|
| 244 |
return {"msg": "admin updated"}
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
# -------------------------
|
| 247 |
# SESSION ENDPOINTS (6)
|
| 248 |
# -------------------------
|
|
@@ -392,6 +415,37 @@ def end_session(data: EndSession):
|
|
| 392 |
return {"total_km": total}
|
| 393 |
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
@app.get("/session/user")
|
| 396 |
def get_all_sessions_of_user(username: str):
|
| 397 |
|
|
@@ -406,6 +460,69 @@ def get_all_sessions_of_user(username: str):
|
|
| 406 |
|
| 407 |
return result
|
| 408 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
# -------------------------
|
| 410 |
# SETTINGS ENDPOINTS (2)
|
| 411 |
# -------------------------
|
|
@@ -465,10 +582,13 @@ def fetch_update_json(method: str = "fetch"):
|
|
| 465 |
if "sessions" in data:
|
| 466 |
sessions.clear()
|
| 467 |
sessions.update(data["sessions"])
|
|
|
|
|
|
|
|
|
|
| 468 |
return {"msg": "json files updated with fetched settings"}
|
| 469 |
#this will update driveledger.json by vps_main.py on vps
|
| 470 |
elif method == "update":
|
| 471 |
-
payload = {"site_settings": site_settings, "users": users, "sessions": sessions}
|
| 472 |
rs = requests.post(f"{base_url_info}/driverledge/sync?method=update", json=payload)
|
| 473 |
if rs.status_code != 200:
|
| 474 |
raise HTTPException(500, "remote update failed")
|
|
|
|
| 53 |
|
| 54 |
users = {}
|
| 55 |
sessions = {}
|
| 56 |
+
fuel_record = {}
|
| 57 |
data_lock = threading.Lock()
|
| 58 |
|
| 59 |
# -------------------------
|
|
|
|
| 101 |
username: str
|
| 102 |
password: str
|
| 103 |
profile_picture: Optional[str]
|
| 104 |
+
vehicle_number: Optional[str]
|
| 105 |
ip_address: str
|
| 106 |
cnic_no: str
|
| 107 |
gender: str
|
|
|
|
| 144 |
lat: float
|
| 145 |
lng: float
|
| 146 |
|
| 147 |
+
|
| 148 |
+
class EnergyRecord(BaseModel):
|
| 149 |
+
fuel_type: str # #Petrol/Diesel #default Petrol
|
| 150 |
+
km: int
|
| 151 |
+
liters: Optional[float] = None
|
| 152 |
+
price_per_liter: Optional[float] = None
|
| 153 |
+
total_cost: Optional[float] = None
|
| 154 |
+
station: Optional[str] = None
|
| 155 |
+
photo: Optional[str] = None
|
| 156 |
+
lat: Optional[float] = None
|
| 157 |
+
lng: Optional[float] = None
|
| 158 |
+
|
| 159 |
# -------------------------
|
| 160 |
# USER ENDPOINTS (7)
|
| 161 |
# -------------------------
|
|
|
|
| 178 |
|
| 179 |
"username": data.username,
|
| 180 |
"password": data.password,
|
| 181 |
+
"vehicle_number": data.vehicle_number,
|
| 182 |
"profile_picture": img_url,
|
| 183 |
"ip_address": data.ip_address,
|
| 184 |
"cnic_no": data.cnic_no,
|
|
|
|
| 257 |
|
| 258 |
return {"msg": "admin updated"}
|
| 259 |
|
| 260 |
+
|
| 261 |
+
@app.post("/users/update")
|
| 262 |
+
def update_user(username: str, field: str, value):
|
| 263 |
+
"""Generic admin update for arbitrary user fields."""
|
| 264 |
+
if username not in users:
|
| 265 |
+
raise HTTPException(404, "User not found")
|
| 266 |
+
users[username][field] = value
|
| 267 |
+
return {"msg": "updated"}
|
| 268 |
+
|
| 269 |
# -------------------------
|
| 270 |
# SESSION ENDPOINTS (6)
|
| 271 |
# -------------------------
|
|
|
|
| 415 |
return {"total_km": total}
|
| 416 |
|
| 417 |
|
| 418 |
+
# administrative helpers for sessions/points
|
| 419 |
+
@app.delete("/session/delete")
|
| 420 |
+
def delete_session(session_id: str):
|
| 421 |
+
if session_id not in sessions:
|
| 422 |
+
raise HTTPException(404, "Session not found")
|
| 423 |
+
del sessions[session_id]
|
| 424 |
+
return {"msg": "deleted"}
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
@app.post("/session/point/update")
|
| 428 |
+
def update_point(session_id: str, index: int, field: str, value):
|
| 429 |
+
if session_id not in sessions:
|
| 430 |
+
raise HTTPException(404, "Session not found")
|
| 431 |
+
pts = sessions[session_id].get("points", [])
|
| 432 |
+
if index < 0 or index >= len(pts):
|
| 433 |
+
raise HTTPException(404, "Point not found")
|
| 434 |
+
pts[index][field] = value
|
| 435 |
+
return {"msg": "updated"}
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
@app.delete("/session/point/delete")
|
| 439 |
+
def delete_point(session_id: str, index: int):
|
| 440 |
+
if session_id not in sessions:
|
| 441 |
+
raise HTTPException(404, "Session not found")
|
| 442 |
+
pts = sessions[session_id].get("points", [])
|
| 443 |
+
if index < 0 or index >= len(pts):
|
| 444 |
+
raise HTTPException(404, "Point not found")
|
| 445 |
+
pts.pop(index)
|
| 446 |
+
return {"msg": "deleted"}
|
| 447 |
+
|
| 448 |
+
|
| 449 |
@app.get("/session/user")
|
| 450 |
def get_all_sessions_of_user(username: str):
|
| 451 |
|
|
|
|
| 460 |
|
| 461 |
return result
|
| 462 |
|
| 463 |
+
# -------------------------
|
| 464 |
+
# FUEL / ENERGY RECORDS
|
| 465 |
+
# -------------------------
|
| 466 |
+
|
| 467 |
+
@app.post("/fuel/add")
|
| 468 |
+
def add_fuel(username: str, record: EnergyRecord, timestamp: Optional[str] = None):
|
| 469 |
+
if username not in users:
|
| 470 |
+
raise HTTPException(404, "User not found")
|
| 471 |
+
if username not in fuel_record:
|
| 472 |
+
fuel_record[username] = {}
|
| 473 |
+
if not timestamp:
|
| 474 |
+
tz = get_timezone()
|
| 475 |
+
timestamp = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 476 |
+
rec = record.dict()
|
| 477 |
+
# auto-calc total cost if needed
|
| 478 |
+
if rec.get("liters") is not None and rec.get("price_per_liter") is not None and rec.get("total_cost") is None:
|
| 479 |
+
rec["total_cost"] = rec["liters"] * rec["price_per_liter"]
|
| 480 |
+
fuel_record[username][timestamp] = rec
|
| 481 |
+
return {"timestamp": timestamp}
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
@app.get("/fuel/get")
|
| 485 |
+
def get_fuel(username: Optional[str] = None):
|
| 486 |
+
"""Return fuel records.
|
| 487 |
+
|
| 488 |
+
- If *username* is provided the records for that user are returned.
|
| 489 |
+
In a real system you would verify the caller is either that user or
|
| 490 |
+
an admin before returning someone else's data. For now the caller
|
| 491 |
+
must supply the desired username explicitly.
|
| 492 |
+
- If *username* is omitted the whole dataset is returned (admins can
|
| 493 |
+
use this to inspect all records).
|
| 494 |
+
"""
|
| 495 |
+
if username:
|
| 496 |
+
return fuel_record.get(username, {})
|
| 497 |
+
return fuel_record
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
# mirror the session/user endpoint for fuel records
|
| 501 |
+
@app.get("/fuel/user")
|
| 502 |
+
def get_fuel_of_user(username: str):
|
| 503 |
+
"""Return fuel records for a single user.
|
| 504 |
+
|
| 505 |
+
This makes it easy for the frontend to fetch a user's own data. Admins
|
| 506 |
+
can still call `/fuel/get` without a username to retrieve everything.
|
| 507 |
+
"""
|
| 508 |
+
if username not in users:
|
| 509 |
+
raise HTTPException(404, "User not found")
|
| 510 |
+
return fuel_record.get(username, {})
|
| 511 |
+
|
| 512 |
+
@app.post("/fuel/update")
|
| 513 |
+
def update_fuel(username: str, timestamp: str, field: str, value):
|
| 514 |
+
if username not in fuel_record or timestamp not in fuel_record[username]:
|
| 515 |
+
raise HTTPException(404, "Record not found")
|
| 516 |
+
fuel_record[username][timestamp][field] = value
|
| 517 |
+
return {"msg": "updated"}
|
| 518 |
+
|
| 519 |
+
@app.delete("/fuel/delete")
|
| 520 |
+
def delete_fuel(username: str, timestamp: str):
|
| 521 |
+
if username not in fuel_record or timestamp not in fuel_record[username]:
|
| 522 |
+
raise HTTPException(404, "Record not found")
|
| 523 |
+
del fuel_record[username][timestamp]
|
| 524 |
+
return {"msg": "deleted"}
|
| 525 |
+
|
| 526 |
# -------------------------
|
| 527 |
# SETTINGS ENDPOINTS (2)
|
| 528 |
# -------------------------
|
|
|
|
| 582 |
if "sessions" in data:
|
| 583 |
sessions.clear()
|
| 584 |
sessions.update(data["sessions"])
|
| 585 |
+
if "fuel_record" in data:
|
| 586 |
+
fuel_record.clear()
|
| 587 |
+
fuel_record.update(data["fuel_record"])
|
| 588 |
return {"msg": "json files updated with fetched settings"}
|
| 589 |
#this will update driveledger.json by vps_main.py on vps
|
| 590 |
elif method == "update":
|
| 591 |
+
payload = {"site_settings": site_settings, "users": users, "sessions": sessions, "fuel_record": fuel_record}
|
| 592 |
rs = requests.post(f"{base_url_info}/driverledge/sync?method=update", json=payload)
|
| 593 |
if rs.status_code != 200:
|
| 594 |
raise HTTPException(500, "remote update failed")
|