feat: Add promo codes
All checks were successful
Build Docker / Build Image (push) Successful in 21s

This commit is contained in:
2024-02-15 11:58:22 +11:00
parent 9b7089fe7c
commit f5f4addd74
10 changed files with 476 additions and 26 deletions

127
server.py
View File

@@ -7,6 +7,7 @@ import json
import render
import payments
import threading
import account
app = Flask(__name__)
dotenv.load_dotenv()
@@ -170,13 +171,41 @@ def cart():
else:
cart = '[]'
promo_message = ''
if 'promo' in cookies:
promo = cookies['promo']
else:
promo = ''
cart = json.loads(cart)
cartHtml = render.cart(cart)
total_usd = render.total_usd(cart)
total_hns = render.total_hns(cart)
if promo != '':
promo = payments.getPromo(promo)
if not promo:
promo_message = 'Invalid promo code'
promo = ''
else:
promo_message = f'Promo code {promo["id"]} applied'
return render_template('cart.html', year=year, cart=cartHtml, total_usd=total_usd, total_hns=total_hns)
total_usd = render.total_usd(cart,promo)
total_hns = render.total_hns(cart,promo)
promoCode = ''
if promo:
promoCode = promo["id"]
else:
total_usd = render.total_usd(cart, False)
total_hns = render.total_hns(cart, False)
promoCode = ''
return render_template('cart.html', year=year, cart=cartHtml, total_usd=total_usd, total_hns=total_hns,promo=promoCode, promo_message=promo_message)
@app.route('/cart/promo', methods=['POST'])
def cart_promo():
data = request.form
response = make_response(redirect('/cart'))
response.set_cookie('promo', data['promo'])
return response
@app.route('/payment')
def payment():
@@ -194,8 +223,17 @@ def payment():
cart = json.loads(cart)
cartHtml = render.cart_total(cart)
total_usd = render.total_usd(cart)
total_hns = render.total_hns(cart)
if 'promo' in cookies:
promo = cookies['promo']
promo = payments.getPromo(promo)
total_usd = render.total_usd(cart,promo)
total_hns = render.total_hns(cart,promo)
else:
promo = ''
total_usd = render.total_usd(cart, False)
total_hns = render.total_hns(cart, False)
return render_template('payment.html', year=year, cart=cartHtml, total_usd=total_usd, total_hns=total_hns)
@@ -212,8 +250,16 @@ def payment_post():
return redirect('/cart')
cart = json.loads(cart)
total_usd = render.total_usd(cart)
total_hns = render.total_hns(cart)
if 'promo' in cookies:
promo = cookies['promo']
promo = payments.getPromo(promo)
total_usd = render.total_usd(cart,promo)
total_hns = render.total_hns(cart,promo)
else:
promo = False
total_usd = render.total_usd(cart, False)
total_hns = render.total_hns(cart, False)
data = request.form
if 'email' in data:
@@ -242,15 +288,13 @@ def payment_post():
cartHtml = render.cart_total(cart)
if email == '' or address == '' or name == '' or country == '' or mobile == '':
return render_template('payment.html', error='Please fill all fields',
email=email, address=address, name=name, mobile=mobile, country=country,
cart=cartHtml, total_usd=total_usd, total_hns=total_hns)
# All good, check out
payment = payments.generate_payment(name, email, mobile,address,country, cart, render.total_hns(cart, True))
payment = payments.generate_payment(name, email, mobile,address,country, cart, render.total_hns(cart, promo,True), promo)
if payment == False:
return render_template('payment.html', error='There was an error processing your payment', email=email, address=address, name=name,
@@ -265,9 +309,67 @@ def payment_post():
responce = make_response(render_template('payment_info.html',cart=cartHtml, total_hns=finalPrice,address=HNSaddress, year=datetime.datetime.now().year, qr=qr))
responce.set_cookie('cart', '[]')
return responce
# Promo routes
@app.route('/promo')
def promo():
year = datetime.datetime.now().year
# Check user is logged in
token = request.cookies.get('token')
if not token:
return redirect('/login')
if not account.token(token):
return redirect('/login')
promos = payments.getPromos()
promos = render.promoList(promos)
return render_template('promo.html', year=year, promos=promos)
@app.route('/promo', methods=['POST'])
def promo_post():
data = request.form
token = request.cookies.get('token')
if not token:
return redirect('/login')
if not account.token(token):
return redirect('/login')
id = data['code']
constant = data['constant']
percent = data['percent']
uses = data['uses']
payments.addPromo(id, constant, percent, uses)
return redirect('/promo')
@app.route('/promo/delete/<path:path>')
def promo_delete(path):
token = request.cookies.get('token')
if not token:
return redirect('/login')
if not account.token(token):
return redirect('/login')
payments.deletePromo(path)
return redirect('/promo')
@app.route('/login', methods=['POST'])
def login():
data = request.form
token = account.login(data['email'], data['password'])
if token:
response = make_response(redirect('/promo'))
response.set_cookie('token', token)
return response
return redirect('/login')
# Main routes
@app.route('/')
def index():
@@ -305,7 +407,4 @@ def check_payments():
payments.check_payments()
if __name__ == '__main__':
# Set timer for payments
repeat_check_payments()
app.run(debug=True, port=5000, host='0.0.0.0')