nagasurendra commited on
Commit
5e4ad36
·
verified ·
1 Parent(s): 67d2793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -42,6 +42,13 @@ def generate_referral_code(length=8):
42
  def home():
43
  return render_template("index.html")
44
 
 
 
 
 
 
 
 
45
  @app.route("/signup", methods=["GET", "POST"])
46
  def signup():
47
  if request.method == "POST":
@@ -53,45 +60,50 @@ def signup():
53
  generated_referral_code = generate_referral_code()
54
 
55
  try:
56
- ref=0
 
57
  # Check if a referral code is entered
58
  if referral_code:
59
  # Query Salesforce to check if the referral code exists
60
- referral_query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Referral__c = '{referral_code}'"
61
  referral_result = sf.query(referral_query)
62
- ref=50
63
 
64
  if not referral_result['records']:
65
- # If referral code does not exist, show an error message
66
  return render_template("signup.html", error="Invalid referral code!")
67
 
68
- # If referral exists, get the first matching record
69
  referrer = referral_result['records'][0]
70
- referrer_id = referrer['Id']
71
- # Fetch the existing reward points, defaulting to 0 if None
72
- existing_reward_points = referrer.get('Reward_Points__c') or 0 # Default to 0 if the field is None
73
- updated_reward_points = existing_reward_points + 100
74
- # Update the referrer's Reward_Points__c field in Salesforce
75
- sf.Customer_Login__c.update(referrer_id, {
76
- "Reward_Points__c": updated_reward_points
 
 
 
77
  })
78
-
79
  # Create the new customer record in Salesforce
80
  sf.Customer_Login__c.create({
81
  "Name": name,
82
  "Phone_Number__c": phone,
83
  "Email__c": email,
84
  "Password__c": password,
85
- "Reward_Points__c": ref,
86
  "Referral__c": generated_referral_code
87
  })
 
88
  return redirect(url_for("login"))
89
 
90
  except Exception as e:
91
  return render_template("signup.html", error=f"Error: {str(e)}")
 
92
  return render_template("signup.html")
93
 
94
 
 
95
  @app.route("/login", methods=["GET", "POST"])
96
  def login():
97
  if request.method == "POST":
 
42
  def home():
43
  return render_template("index.html")
44
 
45
+ from datetime import datetime
46
+
47
+ def generate_coupon_code(length=10):
48
+ """Generates a random alphanumeric coupon code"""
49
+ characters = string.ascii_uppercase + string.digits # A-Z, 0-9
50
+ return ''.join(random.choice(characters) for _ in range(length))
51
+
52
  @app.route("/signup", methods=["GET", "POST"])
53
  def signup():
54
  if request.method == "POST":
 
60
  generated_referral_code = generate_referral_code()
61
 
62
  try:
63
+ ref=0 # Default reward points for new user
64
+
65
  # Check if a referral code is entered
66
  if referral_code:
67
  # Query Salesforce to check if the referral code exists
68
+ referral_query = f"SELECT Id, Email__c FROM Customer_Login__c WHERE Referral__c = '{referral_code}'"
69
  referral_result = sf.query(referral_query)
 
70
 
71
  if not referral_result['records']:
 
72
  return render_template("signup.html", error="Invalid referral code!")
73
 
74
+ # Get referrer's details
75
  referrer = referral_result['records'][0]
76
+ referrer_email = referrer.get('Email__c')
77
+
78
+ # Generate a unique coupon code
79
+ new_coupon_code = generate_coupon_code()
80
+
81
+ # Create a new referral coupon entry in Salesforce
82
+ sf.Referral_Coupon__c.create({
83
+ "Referral_Email__c": referrer_email,
84
+ "Coupon_Code__c": new_coupon_code,
85
+ "Created_Date__c": datetime.utcnow().isoformat() # Optional timestamp
86
  })
87
+
88
  # Create the new customer record in Salesforce
89
  sf.Customer_Login__c.create({
90
  "Name": name,
91
  "Phone_Number__c": phone,
92
  "Email__c": email,
93
  "Password__c": password,
94
+ "Reward_Points__c": ref, # No points added, only coupon is created
95
  "Referral__c": generated_referral_code
96
  })
97
+
98
  return redirect(url_for("login"))
99
 
100
  except Exception as e:
101
  return render_template("signup.html", error=f"Error: {str(e)}")
102
+
103
  return render_template("signup.html")
104
 
105
 
106
+
107
  @app.route("/login", methods=["GET", "POST"])
108
  def login():
109
  if request.method == "POST":