Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -120,68 +120,78 @@ def signup():
|
|
120 |
|
121 |
|
122 |
|
123 |
-
@app.route("/
|
124 |
-
def
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
try:
|
131 |
-
# Fetch reward points
|
132 |
-
customer_record = sf.query(f"""
|
133 |
-
SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'
|
134 |
-
""")
|
135 |
-
customer = customer_record.get("records", [])[0] if customer_record else None
|
136 |
-
|
137 |
-
if not customer:
|
138 |
-
return jsonify({"success": False, "message": "User not found"})
|
139 |
-
|
140 |
-
reward_points = customer.get("Reward_Points__c", 0)
|
141 |
-
|
142 |
-
if reward_points < 500:
|
143 |
-
return jsonify({"success": False, "message": "Not enough reward points to redeem a coupon"})
|
144 |
-
|
145 |
-
# Generate a 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 |
-
|
154 |
-
#
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
-
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
"
|
164 |
-
})
|
165 |
-
print(f"β
Coupon appended for existing user: {email}")
|
166 |
-
else:
|
167 |
-
# β Case 2: No record found β Create a new record in Referral_Coupon__c
|
168 |
-
sf.Referral_Coupon__c.create({
|
169 |
-
"Referral_Email__c": email,
|
170 |
-
"Coupon_Code__c": new_coupon_code
|
171 |
-
})
|
172 |
-
print(f"β
New Referral_Coupon__c record created for {email}")
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
"Reward_Points__c": new_reward_points
|
178 |
-
})
|
179 |
-
print(f"β
Reward points updated: New balance = {new_reward_points}")
|
180 |
|
181 |
-
|
182 |
|
183 |
-
except Exception as e:
|
184 |
-
return jsonify({"success": False, "error": str(e)})
|
185 |
|
186 |
@app.route("/menu", methods=["GET", "POST"])
|
187 |
def menu():
|
|
|
120 |
|
121 |
|
122 |
|
123 |
+
@app.route("/login", methods=["GET", "POST"])
|
124 |
+
def login():
|
125 |
+
if request.method == "POST":
|
126 |
+
email = request.form.get("email")
|
127 |
+
password = request.form.get("password")
|
128 |
+
print(f"Login attempt with email: {email}") # Debug log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
+
try:
|
131 |
+
# Fetch user details, including Reward_Points__c
|
132 |
+
query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
|
133 |
+
result = sf.query(query)
|
134 |
+
|
135 |
+
if result["records"]:
|
136 |
+
user = result["records"][0]
|
137 |
+
session['user_id'] = user['Id']
|
138 |
+
session['user_email'] = email
|
139 |
+
print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
|
140 |
+
|
141 |
+
reward_points = user.get("Reward_Points__c", 0)
|
142 |
+
|
143 |
+
if reward_points >= 500:
|
144 |
+
print(f"User {email} has {reward_points} reward points. Generating coupon...")
|
145 |
+
|
146 |
+
# Generate a new coupon code
|
147 |
+
new_coupon_code = generate_coupon_code()
|
148 |
+
|
149 |
+
# Check if user already has a record in Referral_Coupon__c
|
150 |
+
coupon_query = sf.query(f"""
|
151 |
+
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
152 |
+
""")
|
153 |
+
|
154 |
+
if coupon_query["records"]:
|
155 |
+
# If record exists, append the new coupon
|
156 |
+
coupon_record = coupon_query["records"][0]
|
157 |
+
referral_coupon_id = coupon_record["Id"]
|
158 |
+
existing_coupons = coupon_record.get("Coupon_Code__c", "")
|
159 |
+
|
160 |
+
# Append the new coupon on the next line
|
161 |
+
updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
|
162 |
+
|
163 |
+
# Update the Referral_Coupon__c record
|
164 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
165 |
+
"Coupon_Code__c": updated_coupons
|
166 |
+
})
|
167 |
+
print(f"Updated existing coupon record for {email}. New Coupon: {new_coupon_code}")
|
168 |
+
else:
|
169 |
+
# If no record exists, create a new one
|
170 |
+
sf.Referral_Coupon__c.create({
|
171 |
+
"Referral_Email__c": email,
|
172 |
+
"Coupon_Code__c": new_coupon_code
|
173 |
+
})
|
174 |
+
print(f"Created new coupon record for {email}. Coupon: {new_coupon_code}")
|
175 |
+
|
176 |
+
# Subtract 500 reward points from user's account
|
177 |
+
new_reward_points = reward_points - 500
|
178 |
+
sf.Customer_Login__c.update(user['Id'], {
|
179 |
+
"Reward_Points__c": new_reward_points
|
180 |
+
})
|
181 |
+
print(f"Coupon {new_coupon_code} generated and 500 points deducted. New balance: {new_reward_points}")
|
182 |
|
183 |
+
return redirect(url_for("menu"))
|
184 |
|
185 |
+
else:
|
186 |
+
print("Invalid credentials!")
|
187 |
+
return render_template("login.html", error="Invalid credentials!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
+
except Exception as e:
|
190 |
+
print(f"Error during login: {str(e)}")
|
191 |
+
return render_template("login.html", error=f"Error: {str(e)}")
|
|
|
|
|
|
|
192 |
|
193 |
+
return render_template("login.html")
|
194 |
|
|
|
|
|
195 |
|
196 |
@app.route("/menu", methods=["GET", "POST"])
|
197 |
def menu():
|