DSatishchandra geethareddy commited on
Commit
267d2cc
·
verified ·
1 Parent(s): 8cfff2f

Update app.py (#12)

Browse files

- Update app.py (1ca882791ca1d5ccea004dc685010c574c6f2529)


Co-authored-by: geetha <[email protected]>

Files changed (1) hide show
  1. app.py +52 -64
app.py CHANGED
@@ -162,80 +162,25 @@ def generate_custom_dish():
162
  except Exception as e:
163
  return jsonify({"success": False, "error": str(e)}), 500
164
 
165
- import re
166
- @app.route("/edit_profile", methods=["GET", "POST"])
167
- def edit_profile():
168
- email = session.get('user_email') # Get logged-in user's email
169
- if not email:
170
- return redirect(url_for("login"))
171
-
172
- try:
173
- # Fetch user details from Salesforce
174
- result = sf.query(f"""
175
- SELECT Id, Name, Email__c, Phone_Number__c, Password__c
176
- FROM Customer_Login__c
177
- WHERE Email__c = '{email}'
178
- """)
179
-
180
- if not result['records']:
181
- return redirect(url_for("login"))
182
-
183
- user = result['records'][0]
184
- user_id = user.get("Id")
185
- user_name = user.get("Name")
186
- user_phone = user.get("Phone_Number__c")
187
- user_email = user.get("Email__c")
188
-
189
- except Exception as e:
190
- print(f"Error fetching user data: {str(e)}")
191
- return jsonify({"success": False, "message": "Error fetching user data"})
192
-
193
- try:
194
- # Process user profile update
195
- new_name = request.form.get('name')
196
- new_email = request.form.get('email')
197
- new_phone = request.form.get('phone')
198
- new_password = request.form.get('password')
199
-
200
- update_data = {
201
- 'Name': new_name,
202
- 'Email__c': new_email,
203
- 'Phone_Number__c': new_phone
204
- }
205
-
206
- if new_password:
207
- update_data['Password__c'] = new_password
208
-
209
- # Update Salesforce record
210
- sf.Customer_Login__c.update(user_id, update_data)
211
-
212
- return redirect(url_for('customer_details'))
213
-
214
- except Exception as e:
215
- return render_template("edit_profile.html", user_name=user_name, user_phone=user_phone, user_email=user_email, error=str(e))
216
-
217
- import re
218
  @app.route("/customer_details", methods=["GET"])
219
  def customer_details():
220
  email = session.get('user_email') # Get logged-in user's email
221
-
222
  if not email:
223
- return redirect(url_for("login")) # If no email is found, redirect to login
224
 
225
  try:
226
  # Fetch customer details from Salesforce based on the email
227
  customer_record = sf.query(f"""
228
- SELECT Name, Email__c, Phone_Number__c, Referral__c, Reward_Points__c
229
  FROM Customer_Login__c
230
  WHERE Email__c = '{email}'
231
  LIMIT 1
232
  """)
233
 
234
- # If no customer record found, handle it
235
  if not customer_record.get("records"):
236
- return jsonify({"success": False, "message": "Customer not found in Salesforce"})
 
237
 
238
- # Get the customer details
239
  customer = customer_record["records"][0]
240
 
241
  # Prepare the data to return to the frontend
@@ -247,16 +192,59 @@ def customer_details():
247
  "reward_points": customer.get("Reward_Points__c", 0)
248
  }
249
 
250
- # Return the customer details as JSON response
251
  return render_template("customer_details.html", customer=customer_data)
252
 
253
  except Exception as e:
254
- print(f"Error fetching customer details: {str(e)}")
255
- return jsonify({"success": False, "message": f"Error fetching customer details: {str(e)}"})
256
 
 
 
 
 
 
257
 
258
- from datetime import datetime
259
- import pytz # Library to handle timezone conversions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
 
261
  @app.route("/order-history", methods=["GET"])
262
  def order_history():
 
162
  except Exception as e:
163
  return jsonify({"success": False, "error": str(e)}), 500
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  @app.route("/customer_details", methods=["GET"])
166
  def customer_details():
167
  email = session.get('user_email') # Get logged-in user's email
 
168
  if not email:
169
+ return redirect(url_for("login"))
170
 
171
  try:
172
  # Fetch customer details from Salesforce based on the email
173
  customer_record = sf.query(f"""
174
+ SELECT Id, Name, Email__c, Phone_Number__c, Referral__c, Reward_Points__c
175
  FROM Customer_Login__c
176
  WHERE Email__c = '{email}'
177
  LIMIT 1
178
  """)
179
 
 
180
  if not customer_record.get("records"):
181
+ flash("Customer not found", "danger")
182
+ return redirect(url_for("login"))
183
 
 
184
  customer = customer_record["records"][0]
185
 
186
  # Prepare the data to return to the frontend
 
192
  "reward_points": customer.get("Reward_Points__c", 0)
193
  }
194
 
 
195
  return render_template("customer_details.html", customer=customer_data)
196
 
197
  except Exception as e:
198
+ flash(f"Error fetching customer details: {str(e)}", "danger")
199
+ return redirect(url_for("login"))
200
 
201
+ @app.route("/update_profile", methods=["POST"])
202
+ def update_profile():
203
+ email = session.get('user_email') # Get logged-in user's email
204
+ if not email:
205
+ return jsonify({'status': 'error', 'message': 'User not logged in'})
206
 
207
+ try:
208
+ # Fetch user details from Salesforce
209
+ result = sf.query(f"""
210
+ SELECT Id, Name, Email__c, Phone_Number__c, Referral__c, Reward_Points__c
211
+ FROM Customer_Login__c
212
+ WHERE Email__c = '{email}'
213
+ """)
214
+
215
+ if not result['records']:
216
+ return jsonify({'status': 'error', 'message': 'User not found'})
217
+
218
+ user = result['records'][0]
219
+ user_id = user.get("Id")
220
+
221
+ # Get updated profile data from the form
222
+ new_name = request.form.get('customerName')
223
+ new_email = request.form.get('email')
224
+ new_phone = request.form.get('phone')
225
+ new_referral_code = request.form.get('referralCode')
226
+ new_reward_points = request.form.get('rewardPoints')
227
+
228
+ # Prepare data for Salesforce update
229
+ update_data = {
230
+ 'Name': new_name,
231
+ 'Email__c': new_email,
232
+ 'Phone_Number__c': new_phone,
233
+ 'Referral__c': new_referral_code,
234
+ 'Reward_Points__c': new_reward_points
235
+ }
236
+
237
+ # Update Salesforce record
238
+ sf.Customer_Login__c.update(user_id, update_data)
239
+
240
+ return jsonify({
241
+ 'status': 'success',
242
+ 'message': 'Profile updated successfully!',
243
+ 'data': update_data
244
+ })
245
+
246
+ except Exception as e:
247
+ return jsonify({'status': 'error', 'message': str(e)})
248
 
249
  @app.route("/order-history", methods=["GET"])
250
  def order_history():