From bcd3895f94010b4d90a746f7bdde03fd8a8a914c Mon Sep 17 00:00:00 2001 From: Tom Conner Date: Sun, 19 Nov 2023 05:59:38 +0000 Subject: [PATCH] order --- .../layouts/shortcodes/register-form.html | 83 +++++++++++++++++-- server/db.py | 32 ++++++- server/server.py | 28 ++++--- 3 files changed, 122 insertions(+), 21 deletions(-) diff --git a/generator/layouts/shortcodes/register-form.html b/generator/layouts/shortcodes/register-form.html index 714e0b1..89a8808 100644 --- a/generator/layouts/shortcodes/register-form.html +++ b/generator/layouts/shortcodes/register-form.html @@ -108,20 +108,27 @@ font-size:0.7em; } +#lookup-code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New'; + color: gray; + font-size: 80%; + margin: 0; + text-align: right; +} +
-

- +

If you have an artificial tree, or no tree, but would like to support Pembroke Troop 105's Scouting program, indicate 0 trees and an additional donation amount.

- +

@@ -142,6 +149,7 @@

--> +

+ +
diff --git a/server/db.py b/server/db.py index c6d078b..4194a8b 100644 --- a/server/db.py +++ b/server/db.py @@ -3,6 +3,8 @@ from pathlib import Path from datetime import datetime import logging +import random + logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) @@ -15,6 +17,9 @@ class Meta: class Pickup(TreeModel): lookup=CharField() + +class Address(TreeModel): + lookup=ForeignKeyField(Pickup, backref='address') name=CharField() address1=CharField() address2=CharField() @@ -22,8 +27,10 @@ class Pickup(TreeModel): state=CharField() class Order(TreeModel): - pass - + lookup=ForeignKeyField(Pickup, backref='Order') + comment=CharField() + trees=IntegerField() + extra=IntegerField() def random_id(): alphabet='ABCDEFGHJKMNPQRTUVWXYZ' @@ -42,7 +49,7 @@ def init_or_connect(): print('initializing database') DB_FILE.parent.mkdir(parents=True, exist_ok=True) database.connect() - database.create_tables([Pickup, Order]) + database.create_tables([Pickup, Address, Order]) database.close() else: print('connecting to database') @@ -57,3 +64,22 @@ def before_request(): def teardown_request(): if not database.is_closed(): database.close() + +def get_pickups(): + logger.debug('get_pickups') + return Pickup.select().dicts() + +def get_pickup(lookup): + logger.debug(f'get_pickup {lookup}') + return Pickup.select().where(Pickup.lookup == lookup).dicts().get() + +def create_pickup(): + logger.debug('create_pickup') + lookup = new_lookup_id() + Pickup.create(lookup=lookup) + return lookup + +def update_pickup(lookup): + logger.debug(f'update_pickup {lookup}') + Pickup.update(updated=datetime.now()).where(Pickup.lookup == lookup).execute() + return lookup diff --git a/server/server.py b/server/server.py index 8598096..c3858e6 100644 --- a/server/server.py +++ b/server/server.py @@ -7,7 +7,6 @@ from dotenv import load_dotenv, find_dotenv import db -from db import Pickup, Order load_dotenv(find_dotenv()) @@ -22,6 +21,12 @@ app = Flask(__name__) +def amount_from_request(data): + app.logger.info("amount_from_request") + app.logger.info(data) + amount = data["trees"] * 15 + data["extra"] + return amount*100 + # This hook ensures that a connection is opened to handle any queries # generated by the request. @app.before_request @@ -37,26 +42,27 @@ def db_teardown_request(exc): @app.route('/api/v1/config', methods=['GET']) def get_config(): app.logger.info('config') - db.Pickup(lookup="ABCD", name='tom', address1='123 main st', address2='apt 1', town='town', state='MA').save() return jsonify({'publishableKey': os.getenv('STRIPE_PUBLISHABLE_KEY')}) @app.route('/api/v1/pickups', methods=['GET']) def get_pickups(): app.logger.info(f"pickup") - pickups = Pickup.select().dicts() - return jsonify([p for p in pickups]) + return jsonify([p for p in db.get_pickups()]) @app.route('/api/v1/pickups/', methods=['GET']) def get_pickup(lookup): app.logger.info(f"pickup {lookup}") - pickup = Pickup.select().where(Pickup.lookup == lookup).dicts().get() - return jsonify(pickup) + return jsonify(db.get_pickup(lookup)) -def amount_from_request(data): - app.logger.info("amount_from_request") - app.logger.info(data) - amount = data["trees"] * 15 + data["extra"] - return amount*100 +@app.route('/api/v1/pickups', methods=['POST']) +def post_pickup(): + app.logger.info(f"post pickup") + return str(db.create_pickup()) + +@app.route('/api/v1/pickups/', methods=['PUT']) +def put_pickup(lookup): + app.logger.info(f"post pickup") + return str(db.update_pickup(lookup)) @app.route('/api/v1/create-payment-intent', methods=['POST']) def create_payment():