Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -128,23 +128,68 @@ def login():
|
|
| 128 |
print(f"Login attempt with email: {email}") # Debug log
|
| 129 |
|
| 130 |
try:
|
| 131 |
-
query = f"SELECT Id, Name, Email__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
| 132 |
result = sf.query(query)
|
| 133 |
|
| 134 |
if result["records"]:
|
| 135 |
-
|
|
|
|
| 136 |
session['user_email'] = email
|
| 137 |
print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
return redirect(url_for("menu"))
|
|
|
|
| 140 |
else:
|
| 141 |
print("Invalid credentials!")
|
| 142 |
return render_template("login.html", error="Invalid credentials!")
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
print(f"Error during login: {str(e)}")
|
| 145 |
return render_template("login.html", error=f"Error: {str(e)}")
|
| 146 |
|
| 147 |
return render_template("login.html")
|
|
|
|
|
|
|
|
|
|
| 148 |
@app.route("/menu", methods=["GET", "POST"])
|
| 149 |
def menu():
|
| 150 |
selected_category = request.args.get("category", "All")
|
|
|
|
| 128 |
print(f"Login attempt with email: {email}") # Debug log
|
| 129 |
|
| 130 |
try:
|
| 131 |
+
query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
| 132 |
result = sf.query(query)
|
| 133 |
|
| 134 |
if result["records"]:
|
| 135 |
+
user = result["records"][0]
|
| 136 |
+
session['user_id'] = user['Id']
|
| 137 |
session['user_email'] = email
|
| 138 |
print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
|
| 139 |
+
|
| 140 |
+
reward_points = user.get("Reward_Points__c", 0)
|
| 141 |
+
|
| 142 |
+
if reward_points >= 500:
|
| 143 |
+
print(f"User {email} has {reward_points} reward points. Generating coupon...")
|
| 144 |
+
|
| 145 |
+
# Generate new coupon code
|
| 146 |
+
new_coupon_code = generate_coupon_code()
|
| 147 |
+
|
| 148 |
+
# Check if user already has a record in Referral_Coupon__c
|
| 149 |
+
coupon_query = sf.query(f"""
|
| 150 |
+
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
if coupon_query["records"]:
|
| 154 |
+
# Append the new coupon to existing ones
|
| 155 |
+
coupon_record = coupon_query["records"][0]
|
| 156 |
+
referral_coupon_id = coupon_record["Id"]
|
| 157 |
+
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
| 158 |
+
|
| 159 |
+
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
| 160 |
+
|
| 161 |
+
# Update the Referral_Coupon__c record
|
| 162 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 163 |
+
"Coupon_Code__c": updated_coupons
|
| 164 |
+
})
|
| 165 |
+
else:
|
| 166 |
+
# Create a new record for the user
|
| 167 |
+
sf.Referral_Coupon__c.create({
|
| 168 |
+
"Referral_Email__c": email,
|
| 169 |
+
"Coupon_Code__c": new_coupon_code
|
| 170 |
+
})
|
| 171 |
+
|
| 172 |
+
# Subtract 500 reward points from user's account
|
| 173 |
+
new_reward_points = reward_points - 500
|
| 174 |
+
sf.Customer_Login__c.update(user['Id'], {
|
| 175 |
+
"Reward_Points__c": new_reward_points
|
| 176 |
+
})
|
| 177 |
+
print(f"Coupon {new_coupon_code} generated and 500 points deducted. New balance: {new_reward_points}")
|
| 178 |
+
|
| 179 |
return redirect(url_for("menu"))
|
| 180 |
+
|
| 181 |
else:
|
| 182 |
print("Invalid credentials!")
|
| 183 |
return render_template("login.html", error="Invalid credentials!")
|
| 184 |
+
|
| 185 |
except Exception as e:
|
| 186 |
print(f"Error during login: {str(e)}")
|
| 187 |
return render_template("login.html", error=f"Error: {str(e)}")
|
| 188 |
|
| 189 |
return render_template("login.html")
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
return render_template("login.html")
|
| 193 |
@app.route("/menu", methods=["GET", "POST"])
|
| 194 |
def menu():
|
| 195 |
selected_category = request.args.get("category", "All")
|