# Flask example with SQLite database
from flask import Flask, request, render_template
import sqlite3
app = Flask(__name__)
# Function to check coupon validity and return discount amount
def validate_coupon(coupon_code):
# Connect to the database
conn = sqlite3.connect('your_database.db')
cursor = conn.cursor()
# Query the database for the coupon code
cursor.execute("SELECT * FROM coupons WHERE code=?", (coupon_code,))
coupon = cursor.fetchone()
# Close the database connection
conn.close()
# Validate coupon and return discount amount
if coupon and coupon[2] >= datetime.now(): # Assuming the third column is the expiration date
return coupon[1] # Assuming the second column is the discount amount
else:
return None
# Route to handle coupon submission
@app.route('/apply_coupon', methods=['POST'])
def apply_coupon():
coupon_code = request.form.get('coupon_code')
# Validate the coupon
discount_amount = validate_coupon(coupon_code)
if discount_amount is not None:
# Apply the discount to the order total
# Your logic to update the order total goes here
return render_template('checkout.html', discount_applied=True)
else:
return render_template('checkout.html', discount_applied=False)
if __name__ == '__main__':
app.run(debug=True)