Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -45,6 +45,42 @@ def generate_coupon_code(length=10):
|
|
45 |
"""Generate a unique alphanumeric coupon code."""
|
46 |
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
50 |
@app.route("/signup", methods=["GET", "POST"])
|
@@ -76,22 +112,8 @@ def signup():
|
|
76 |
|
77 |
# Step 3: Check if a referral code was provided
|
78 |
if referral_code:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
if len(referral_result['records']) == 0:
|
83 |
-
return "Referral not found. No coupon generated."
|
84 |
-
else:
|
85 |
-
referrer = referral_result["records"][0]
|
86 |
-
# Generate a unique coupon code
|
87 |
-
coupon_code = generate_coupon_code()
|
88 |
-
|
89 |
-
sf.Referral_Coupon__c.create({
|
90 |
-
"Referral_Name__c": referrer["Name"],
|
91 |
-
"Referral_Email__c": referrer["Email__c"],
|
92 |
-
"Coupon_Code__c": coupon_code,
|
93 |
-
"Coupon_Status__c": "Active"
|
94 |
-
})
|
95 |
return redirect(url_for("login"))
|
96 |
except Exception as e:
|
97 |
return render_template("signup.html", error=f"Error: {str(e)}")
|
|
|
45 |
"""Generate a unique alphanumeric coupon code."""
|
46 |
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
|
47 |
|
48 |
+
def process_referral_code(referral_code):
|
49 |
+
"""
|
50 |
+
Process the referral code by creating a referral coupon if the referrer is found.
|
51 |
+
|
52 |
+
Args:
|
53 |
+
referral_code (str): The referral code provided by the user.
|
54 |
+
|
55 |
+
Returns:
|
56 |
+
str: A message indicating the result of the referral code processing.
|
57 |
+
"""
|
58 |
+
try:
|
59 |
+
# Query for the referrer using the referral code
|
60 |
+
query = f"SELECT Id, Name, Email__c FROM Customer_Login__c WHERE Customer_Referral_Code__c = '{referral_code}'"
|
61 |
+
referral_result = sf.query(query)
|
62 |
+
|
63 |
+
if len(referral_result['records']) == 0:
|
64 |
+
return "Referral not found. No coupon generated."
|
65 |
+
else:
|
66 |
+
# Fetch the referrer's details
|
67 |
+
referrer = referral_result["records"][0]
|
68 |
+
|
69 |
+
# Generate a unique coupon code
|
70 |
+
coupon_code = generate_coupon_code()
|
71 |
+
|
72 |
+
# Create a referral coupon in Salesforce
|
73 |
+
sf.Referral_Coupon__c.create({
|
74 |
+
"Referral_Name__c": referrer["Name"],
|
75 |
+
"Referral_Email__c": referrer["Email__c"],
|
76 |
+
"Coupon_Code__c": coupon_code,
|
77 |
+
"Coupon_Status__c": "Active"
|
78 |
+
})
|
79 |
+
|
80 |
+
return f"Coupon code '{coupon_code}' generated for referrer '{referrer['Name']}' ({referrer['Email__c']})."
|
81 |
+
except Exception as e:
|
82 |
+
return f"Error processing referral code: {str(e)}"
|
83 |
+
|
84 |
|
85 |
|
86 |
@app.route("/signup", methods=["GET", "POST"])
|
|
|
112 |
|
113 |
# Step 3: Check if a referral code was provided
|
114 |
if referral_code:
|
115 |
+
referral_message = process_referral_code(referral_code)
|
116 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
return redirect(url_for("login"))
|
118 |
except Exception as e:
|
119 |
return render_template("signup.html", error=f"Error: {str(e)}")
|