Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ from waitress import serve
|
|
12 |
from simple_salesforce import Salesforce
|
13 |
import requests
|
14 |
|
15 |
-
# Initialize Flask
|
16 |
app = Flask(__name__, template_folder="templates")
|
17 |
app.secret_key = os.urandom(24)
|
18 |
|
@@ -29,6 +29,15 @@ try:
|
|
29 |
except Exception as e:
|
30 |
print(f"β Failed to connect to Salesforce: {str(e)}")
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# β
ROUTE: List All Routes for Debugging
|
33 |
@app.route("/routes", methods=["GET"])
|
34 |
def list_routes():
|
@@ -40,38 +49,79 @@ def list_routes():
|
|
40 |
# β
HOME ROUTE
|
41 |
@app.route("/", methods=["GET"])
|
42 |
def home():
|
43 |
-
return jsonify({"message": "Welcome to Biryani Hub API. Use /register, /login, /menu, /index, or /routes to check available endpoints."})
|
44 |
|
45 |
-
# β
RENDER INDEX.HTML
|
46 |
@app.route("/index", methods=["GET"])
|
47 |
def index_page():
|
48 |
return render_template("index.html")
|
49 |
|
50 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
51 |
@app.route("/register", methods=["POST"])
|
52 |
def register():
|
53 |
print("β‘ Register API hit")
|
54 |
data = request.json
|
55 |
if not data or "name" not in data or "email" not in data or "phone" not in data:
|
56 |
return jsonify({"error": "Missing required fields"}), 400
|
57 |
-
return jsonify({"success": True, "message": "Registration successful"})
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
@app.route("/login", methods=["POST"])
|
61 |
def login():
|
62 |
print("β‘ Login API hit")
|
63 |
data = request.json
|
64 |
if not data or "email" not in data or "phone" not in data:
|
65 |
return jsonify({"error": "Missing email or phone"}), 400
|
66 |
-
return jsonify({"success": True, "message": "Login successful"})
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
@app.route("/menu", methods=["GET"])
|
70 |
def get_menu():
|
71 |
print("β‘ Menu API hit")
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
# β
START SERVER
|
75 |
if __name__ == "__main__":
|
76 |
print("β
Starting Flask API Server on port 7860...")
|
77 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
12 |
from simple_salesforce import Salesforce
|
13 |
import requests
|
14 |
|
15 |
+
# β
Initialize Flask App
|
16 |
app = Flask(__name__, template_folder="templates")
|
17 |
app.secret_key = os.urandom(24)
|
18 |
|
|
|
29 |
except Exception as e:
|
30 |
print(f"β Failed to connect to Salesforce: {str(e)}")
|
31 |
|
32 |
+
# β
Print All Available Routes at Startup
|
33 |
+
@app.before_request
|
34 |
+
def print_routes():
|
35 |
+
if not hasattr(app, 'printed_routes'):
|
36 |
+
app.printed_routes = True
|
37 |
+
print("\nβ
Available Routes:")
|
38 |
+
for rule in app.url_map.iter_rules():
|
39 |
+
print(f"β‘ {rule}")
|
40 |
+
|
41 |
# β
ROUTE: List All Routes for Debugging
|
42 |
@app.route("/routes", methods=["GET"])
|
43 |
def list_routes():
|
|
|
49 |
# β
HOME ROUTE
|
50 |
@app.route("/", methods=["GET"])
|
51 |
def home():
|
52 |
+
return jsonify({"message": "Welcome to Biryani Hub API. Use /register, /login, /menu, /index, /dashboard, or /routes to check available endpoints."})
|
53 |
|
54 |
+
# β
RENDER INDEX.HTML (Register & Login Page)
|
55 |
@app.route("/index", methods=["GET"])
|
56 |
def index_page():
|
57 |
return render_template("index.html")
|
58 |
|
59 |
+
# β
RENDER DASHBOARD
|
60 |
+
@app.route("/dashboard", methods=["GET"])
|
61 |
+
def dashboard():
|
62 |
+
return render_template("dashboard.html")
|
63 |
+
|
64 |
+
# β
REGISTER API: Create a new Customer in Salesforce
|
65 |
@app.route("/register", methods=["POST"])
|
66 |
def register():
|
67 |
print("β‘ Register API hit")
|
68 |
data = request.json
|
69 |
if not data or "name" not in data or "email" not in data or "phone" not in data:
|
70 |
return jsonify({"error": "Missing required fields"}), 400
|
|
|
71 |
|
72 |
+
try:
|
73 |
+
new_customer = sf.Customer_Login__c.create({
|
74 |
+
'Name': data["name"],
|
75 |
+
'Email__c': data["email"],
|
76 |
+
'Phone_Number__c': data["phone"]
|
77 |
+
})
|
78 |
+
return jsonify({"success": True, "message": "Registration successful", "customer_id": new_customer["id"]})
|
79 |
+
except Exception as e:
|
80 |
+
return jsonify({"error": f"Failed to register: {str(e)}"}), 500
|
81 |
+
|
82 |
+
# β
LOGIN API: Validate User Credentials
|
83 |
@app.route("/login", methods=["POST"])
|
84 |
def login():
|
85 |
print("β‘ Login API hit")
|
86 |
data = request.json
|
87 |
if not data or "email" not in data or "phone" not in data:
|
88 |
return jsonify({"error": "Missing email or phone"}), 400
|
|
|
89 |
|
90 |
+
try:
|
91 |
+
query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{data['email']}' AND Phone_Number__c = '{data['phone']}'"
|
92 |
+
result = sf.query(query)
|
93 |
+
|
94 |
+
if result["totalSize"] > 0:
|
95 |
+
user = result["records"][0]
|
96 |
+
session["customer_id"] = user["Id"] # Store in session
|
97 |
+
return jsonify({"success": True, "message": "Login successful", "customer_id": user["Id"], "name": user["Name"]})
|
98 |
+
else:
|
99 |
+
return jsonify({"error": "Invalid login credentials"}), 401
|
100 |
+
except Exception as e:
|
101 |
+
return jsonify({"error": f"Login failed: {str(e)}"}), 500
|
102 |
+
|
103 |
+
# β
MENU API: Fetch Menu Items from Salesforce
|
104 |
@app.route("/menu", methods=["GET"])
|
105 |
def get_menu():
|
106 |
print("β‘ Menu API hit")
|
107 |
+
try:
|
108 |
+
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
109 |
+
result = sf.query(query)
|
110 |
+
|
111 |
+
menu_items = []
|
112 |
+
for item in result["records"]:
|
113 |
+
menu_items.append({
|
114 |
+
"name": item["Name"],
|
115 |
+
"price": item["Price__c"],
|
116 |
+
"ingredients": item["Ingredients__c"],
|
117 |
+
"category": item["Category__c"]
|
118 |
+
})
|
119 |
+
|
120 |
+
return jsonify({"success": True, "menu": menu_items})
|
121 |
+
except Exception as e:
|
122 |
+
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
123 |
|
124 |
+
# β
START PRODUCTION SERVER
|
125 |
if __name__ == "__main__":
|
126 |
print("β
Starting Flask API Server on port 7860...")
|
127 |
serve(app, host="0.0.0.0", port=7860)
|