Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,36 +5,35 @@ import pandas as pd
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
# نموذج بيانات الشكوى
|
| 9 |
class Complaint(BaseModel):
|
| 10 |
title: str
|
| 11 |
description: str
|
| 12 |
status: str = "Open"
|
| 13 |
|
| 14 |
-
# قائمة لتخزين الشكاوى مؤقتًا (بديل عن قاعدة بيانات)
|
| 15 |
complaints_db = []
|
|
|
|
| 16 |
|
| 17 |
@app.post("/complaints/")
|
| 18 |
def create_complaint(complaint: Complaint):
|
| 19 |
-
|
| 20 |
-
complaint_id = len(complaints_db) + 1 # تجنب استخدام global
|
| 21 |
new_complaint = {
|
| 22 |
-
"id":
|
| 23 |
"title": complaint.title,
|
| 24 |
"description": complaint.description,
|
| 25 |
"status": complaint.status
|
| 26 |
}
|
| 27 |
complaints_db.append(new_complaint)
|
|
|
|
| 28 |
return {"message": "✅ تم إرسال الشكوى بنجاح!", "complaint": new_complaint}
|
| 29 |
|
| 30 |
-
@app.get("/complaints/")
|
| 31 |
def get_complaints():
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
@app.put("/complaints/{complaint_id}/status/")
|
| 36 |
def update_complaint_status(complaint_id: int, new_status: str):
|
| 37 |
-
"""تحديث حالة الشكوى"""
|
| 38 |
for complaint in complaints_db:
|
| 39 |
if complaint["id"] == complaint_id:
|
| 40 |
complaint["status"] = new_status
|
|
@@ -43,10 +42,13 @@ def update_complaint_status(complaint_id: int, new_status: str):
|
|
| 43 |
|
| 44 |
@app.get("/export/")
|
| 45 |
def export_complaints():
|
| 46 |
-
"""تصدير الشكاوى إلى CSV"""
|
| 47 |
if not complaints_db:
|
| 48 |
raise HTTPException(status_code=404, detail="⚠️ لا توجد بيانات للتصدير.")
|
| 49 |
|
| 50 |
df = pd.DataFrame(complaints_db)
|
| 51 |
df.to_csv("complaints_export.csv", index=False)
|
| 52 |
return {"message": "✅ تم تصدير الشكاوى بنجاح! يمكنك تحميل الملف من السيرفر."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 8 |
class Complaint(BaseModel):
|
| 9 |
title: str
|
| 10 |
description: str
|
| 11 |
status: str = "Open"
|
| 12 |
|
|
|
|
| 13 |
complaints_db = []
|
| 14 |
+
complaint_id_counter = 1
|
| 15 |
|
| 16 |
@app.post("/complaints/")
|
| 17 |
def create_complaint(complaint: Complaint):
|
| 18 |
+
global complaint_id_counter
|
|
|
|
| 19 |
new_complaint = {
|
| 20 |
+
"id": complaint_id_counter,
|
| 21 |
"title": complaint.title,
|
| 22 |
"description": complaint.description,
|
| 23 |
"status": complaint.status
|
| 24 |
}
|
| 25 |
complaints_db.append(new_complaint)
|
| 26 |
+
complaint_id_counter += 1
|
| 27 |
return {"message": "✅ تم إرسال الشكوى بنجاح!", "complaint": new_complaint}
|
| 28 |
|
| 29 |
+
@app.get("/complaints/", response_model=List[dict])
|
| 30 |
def get_complaints():
|
| 31 |
+
if not complaints_db:
|
| 32 |
+
return []
|
| 33 |
+
return complaints_db
|
| 34 |
|
| 35 |
@app.put("/complaints/{complaint_id}/status/")
|
| 36 |
def update_complaint_status(complaint_id: int, new_status: str):
|
|
|
|
| 37 |
for complaint in complaints_db:
|
| 38 |
if complaint["id"] == complaint_id:
|
| 39 |
complaint["status"] = new_status
|
|
|
|
| 42 |
|
| 43 |
@app.get("/export/")
|
| 44 |
def export_complaints():
|
|
|
|
| 45 |
if not complaints_db:
|
| 46 |
raise HTTPException(status_code=404, detail="⚠️ لا توجد بيانات للتصدير.")
|
| 47 |
|
| 48 |
df = pd.DataFrame(complaints_db)
|
| 49 |
df.to_csv("complaints_export.csv", index=False)
|
| 50 |
return {"message": "✅ تم تصدير الشكاوى بنجاح! يمكنك تحميل الملف من السيرفر."}
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
import uvicorn
|
| 54 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|