Hadeeratef91 commited on
Commit
cd867ca
·
verified ·
1 Parent(s): 07efe6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -50
app.py CHANGED
@@ -1,54 +1,84 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- from typing import List
4
  import pandas as pd
 
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
40
- return {"message": "✅ تم تحديث حالة الشكوى!"}
41
- raise HTTPException(status_code=404, detail="⚠️ الشكوى غير موجودة")
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)
 
1
+ # app.py
2
+ import streamlit as st
 
3
  import pandas as pd
4
+ import os
5
 
6
+ # بيانات المستخدمين (مؤقتًا ثابتة)
7
+ users = {
8
+ "user1": {"password": "1234", "role": "user"},
9
+ "admin1": {"password": "admin", "role": "admin"}
10
+ }
11
+
12
+ CSV_FILE = "complaints.csv"
13
+
14
+ # إنشاء الملف لو مش موجود
15
+ if not os.path.exists(CSV_FILE):
16
+ df = pd.DataFrame(columns=["id", "username", "complaint", "status", "response"])
17
+ df.to_csv(CSV_FILE, index=False)
18
+
19
+ # تحميل البيانات
20
+ def load_data():
21
+ return pd.read_csv(CSV_FILE)
22
+
23
+ # حفظ البيانات
24
+ def save_data(df):
25
+ df.to_csv(CSV_FILE, index=False)
26
+
27
+ # تسجيل الدخول
28
+ def login():
29
+ st.sidebar.title("🔐 Login")
30
+ username = st.sidebar.text_input("Username")
31
+ password = st.sidebar.text_input("Password", type="password")
32
+ if st.sidebar.button("Login"):
33
+ if username in users and users[username]["password"] == password:
34
+ return username, users[username]["role"]
35
+ else:
36
+ st.sidebar.error("❌ Invalid credentials")
37
+ return None, None
38
+ return None, None
39
+
40
+ # واجهة المستخدم العادي
41
+ def user_interface(username):
42
+ st.title("📨 Submit a Complaint")
43
+ complaint = st.text_area("Your Complaint")
44
+ if st.button("Submit"):
45
+ df = load_data()
46
+ new_id = len(df) + 1
47
+ new_row = pd.DataFrame([[new_id, username, complaint, "Open", ""]], columns=df.columns)
48
+ df = pd.concat([df, new_row], ignore_index=True)
49
+ save_data(df)
50
+ st.success("✅ Complaint submitted successfully!")
51
+
52
+ st.subheader("📋 Your Complaints")
53
+ df = load_data()
54
+ user_df = df[df["username"] == username]
55
+ st.table(user_df)
56
+
57
+ # واجهة المسؤول
58
+ def admin_interface():
59
+ st.title("🛠️ Admin Dashboard")
60
+ df = load_data()
61
+
62
+ for i, row in df.iterrows():
63
+ st.markdown(f"**Complaint #{row['id']} by {row['username']}**")
64
+ st.markdown(f"- Complaint: {row['complaint']}")
65
+ st.markdown(f"- Status: `{row['status']}`")
66
+ response = st.text_input(f"Response for Complaint #{row['id']}", value=row['response'], key=f"res_{row['id']}")
67
+ status = st.selectbox("Update Status", ["Open", "In Progress", "Closed"], index=["Open", "In Progress", "Closed"].index(row['status']), key=f"stat_{row['id']}")
68
+ if st.button(f"Update #{row['id']}"):
69
+ df.at[i, "response"] = response
70
+ df.at[i, "status"] = status
71
+ save_data(df)
72
+ st.success(f"✅ Complaint #{row['id']} updated.")
73
+
74
+ # التطبيق الرئيسي
75
+ def main():
76
+ username, role = login()
77
+ if username:
78
+ if role == "user":
79
+ user_interface(username)
80
+ elif role == "admin":
81
+ admin_interface()
82
 
83
  if __name__ == "__main__":
84
+ main()