Update app.py
Browse files
app.py
CHANGED
@@ -61,6 +61,63 @@ def home():
|
|
61 |
session.modified = True
|
62 |
return redirect(url_for("menu.menu")) # Redirect to menu directly
|
63 |
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
@app.route("/logout")
|
65 |
def logout():
|
66 |
# Retrieve table number before clearing session
|
|
|
61 |
session.modified = True
|
62 |
return redirect(url_for("menu.menu")) # Redirect to menu directly
|
63 |
return render_template("index.html")
|
64 |
+
@app.route("/login", methods=["GET", "POST"])
|
65 |
+
def login():
|
66 |
+
if request.method == "POST":
|
67 |
+
email = request.form.get("email")
|
68 |
+
password = request.form.get("password")
|
69 |
+
print(f"Login attempt with email: {email}") # Debug log
|
70 |
+
|
71 |
+
try:
|
72 |
+
# Fetch user details from Salesforce
|
73 |
+
query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
74 |
+
result = sf.query(query)
|
75 |
+
|
76 |
+
if result["records"]:
|
77 |
+
user = result["records"][0]
|
78 |
+
session['user_id'] = user['Id']
|
79 |
+
|
80 |
+
# ✅ Always store or update session email
|
81 |
+
if 'user_email' not in session or session['user_email'] != email:
|
82 |
+
session['user_email'] = email
|
83 |
+
session['user_name'] = user.get("Name", "")
|
84 |
+
print(f"✅ Session email updated: {session['user_email']}")
|
85 |
+
|
86 |
+
reward_points = user.get("Reward_Points__c") or 0
|
87 |
+
|
88 |
+
# Coupon generation logic (if reward points >= 500)
|
89 |
+
if reward_points >= 500:
|
90 |
+
new_coupon_code = generate_coupon_code()
|
91 |
+
coupon_query = sf.query(f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'")
|
92 |
+
|
93 |
+
if coupon_query["records"]:
|
94 |
+
coupon_record = coupon_query["records"][0]
|
95 |
+
referral_coupon_id = coupon_record["Id"]
|
96 |
+
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
97 |
+
|
98 |
+
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
99 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {"Coupon_Code__c": updated_coupons})
|
100 |
+
else:
|
101 |
+
sf.Referral_Coupon__c.create({
|
102 |
+
"Referral_Email__c": email,
|
103 |
+
"Name": user.get("Name", ""),
|
104 |
+
"Coupon_Code__c": new_coupon_code
|
105 |
+
})
|
106 |
+
|
107 |
+
new_reward_points = reward_points - 500
|
108 |
+
sf.Customer_Login__c.update(user['Id'], {"Reward_Points__c": new_reward_points})
|
109 |
+
|
110 |
+
return redirect(url_for("menu"))
|
111 |
+
|
112 |
+
else:
|
113 |
+
print("Invalid credentials!")
|
114 |
+
return render_template("login.html", error="Invalid credentials!")
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
print(f"Error during login: {str(e)}")
|
118 |
+
return render_template("login.html", error=f"Error: {str(e)}")
|
119 |
+
|
120 |
+
return render_template("login.html")
|
121 |
@app.route("/logout")
|
122 |
def logout():
|
123 |
# Retrieve table number before clearing session
|