Update login.py
Browse files
login.py
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 2 |
+
def login():
|
| 3 |
+
if request.method == "POST":
|
| 4 |
+
email = request.form.get("email")
|
| 5 |
+
password = request.form.get("password")
|
| 6 |
+
print(f"Login attempt with email: {email}") # Debug log
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
# Fetch user details from Salesforce
|
| 10 |
+
query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
| 11 |
+
result = sf.query(query)
|
| 12 |
+
|
| 13 |
+
if result["records"]:
|
| 14 |
+
user = result["records"][0]
|
| 15 |
+
session['user_id'] = user['Id']
|
| 16 |
+
|
| 17 |
+
# ✅ Always store or update session email
|
| 18 |
+
if 'user_email' not in session or session['user_email'] != email:
|
| 19 |
+
session['user_email'] = email
|
| 20 |
+
session['user_name'] = user.get("Name", "")
|
| 21 |
+
print(f"✅ Session email updated: {session['user_email']}")
|
| 22 |
+
|
| 23 |
+
reward_points = user.get("Reward_Points__c") or 0
|
| 24 |
+
|
| 25 |
+
# Coupon generation logic (if reward points >= 500)
|
| 26 |
+
if reward_points >= 500:
|
| 27 |
+
new_coupon_code = generate_coupon_code()
|
| 28 |
+
coupon_query = sf.query(f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'")
|
| 29 |
+
|
| 30 |
+
if coupon_query["records"]:
|
| 31 |
+
coupon_record = coupon_query["records"][0]
|
| 32 |
+
referral_coupon_id = coupon_record["Id"]
|
| 33 |
+
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
| 34 |
+
|
| 35 |
+
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
| 36 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {"Coupon_Code__c": updated_coupons})
|
| 37 |
+
else:
|
| 38 |
+
sf.Referral_Coupon__c.create({
|
| 39 |
+
"Referral_Email__c": email,
|
| 40 |
+
"Name": user.get("Name", ""),
|
| 41 |
+
"Coupon_Code__c": new_coupon_code
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
new_reward_points = reward_points - 500
|
| 45 |
+
sf.Customer_Login__c.update(user['Id'], {"Reward_Points__c": new_reward_points})
|
| 46 |
+
|
| 47 |
+
return redirect(url_for("menu"))
|
| 48 |
+
|
| 49 |
+
else:
|
| 50 |
+
print("Invalid credentials!")
|
| 51 |
+
return render_template("login.html", error="Invalid credentials!")
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error during login: {str(e)}")
|
| 55 |
+
return render_template("login.html", error=f"Error: {str(e)}")
|
| 56 |
+
|
| 57 |
+
return render_template("login.html")
|