From a6b151bc4087875bb4cc091fad026a27aee164bb Mon Sep 17 00:00:00 2001 From: Florian Vahl Date: Sun, 28 Jun 2020 16:11:02 +0200 Subject: [PATCH 1/5] Add way to access orders and items for a user --- apos/app.py | 6 ++++-- apos/resources/items.py | 8 ++++++++ apos/resources/orders.py | 7 +++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/apos/app.py b/apos/app.py index c4ca3ee..0e09141 100644 --- a/apos/app.py +++ b/apos/app.py @@ -1,7 +1,7 @@ from apos.extensions import app, api from apos.resources.coupons import CouponResource, CouponListResource -from apos.resources.orders import OrderListResource, OrderResource, OrderActiveListResource -from apos.resources.items import ItemListResource, ItemResource +from apos.resources.orders import OrderListResource, OrderUserListResource, OrderResource, OrderActiveListResource +from apos.resources.items import ItemListResource, ItemUserListResource, ItemResource from apos.resources.auth import Auth, Signup api.add_resource(CouponResource, '/api/v1/coupons/') @@ -11,6 +11,8 @@ api.add_resource(OrderResource, '/api/v1/orders/') api.add_resource(ItemListResource, '/api/v1/orders//items') api.add_resource(ItemResource, '/api/v1/orders//items/') +api.add_resource(OrderUserListResource, '/api/v1/user/orders') +api.add_resource(ItemUserListResource, '/api/v1/user/items') api.add_resource(Auth, '/api/v1/auth') api.add_resource(Signup, '/api/v1/signup') diff --git a/apos/resources/items.py b/apos/resources/items.py index 1c5011c..44488ec 100644 --- a/apos/resources/items.py +++ b/apos/resources/items.py @@ -35,6 +35,14 @@ def put(self, order_id): db.session.commit() return item.serialize, 201 + +class ItemUserListResource(Resource): + @jwt_required + def get(self): + items = Item.query.filter_by(Item.user_id == get_jwt_identity()).all() + return [item.serialize for item in items] + + class ItemResource(Resource): @jwt_required def get(self, order_id, item_id): diff --git a/apos/resources/orders.py b/apos/resources/orders.py index 31163e3..32b7e8c 100644 --- a/apos/resources/orders.py +++ b/apos/resources/orders.py @@ -51,6 +51,13 @@ def get(self): return [order.serialize for order in orders] +class OrderUserListResource(Resource): + @jwt_required + def get(self): + orders = Order.query.filter(Order.owner == get_jwt_identity()).all() + return [order.serialize for order in orders] + + class OrderResource(Resource): @jwt_required def get(self, order_id): From 951f4935557021f9f5a19fc1f69dfa3240727c32 Mon Sep 17 00:00:00 2001 From: Florian Vahl Date: Sun, 28 Jun 2020 17:12:06 +0200 Subject: [PATCH 2/5] Serialize oder metadata in item --- apos/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/apos/models.py b/apos/models.py index 7b0fcb1..5633dff 100644 --- a/apos/models.py +++ b/apos/models.py @@ -75,6 +75,7 @@ def serialize(self): item = { 'id': self.id, 'order_id': self.order.id, + 'order': self.order.serialize, 'user': self.user.serialize, 'name': self.name, 'tip_percent': self.tip_percent, From d4baf9db0e09a12a87188c38a0d40efcc8e16770 Mon Sep 17 00:00:00 2001 From: Florian Vahl Date: Sun, 28 Jun 2020 17:12:20 +0200 Subject: [PATCH 3/5] Weird fix --- apos/resources/items.py | 2 +- apos/resources/orders.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apos/resources/items.py b/apos/resources/items.py index 44488ec..f917d58 100644 --- a/apos/resources/items.py +++ b/apos/resources/items.py @@ -39,7 +39,7 @@ def put(self, order_id): class ItemUserListResource(Resource): @jwt_required def get(self): - items = Item.query.filter_by(Item.user_id == get_jwt_identity()).all() + items = Item.query.filter_by(user_id=get_jwt_identity()).all() return [item.serialize for item in items] diff --git a/apos/resources/orders.py b/apos/resources/orders.py index 32b7e8c..7cb18eb 100644 --- a/apos/resources/orders.py +++ b/apos/resources/orders.py @@ -54,7 +54,7 @@ def get(self): class OrderUserListResource(Resource): @jwt_required def get(self): - orders = Order.query.filter(Order.owner == get_jwt_identity()).all() + orders = Order.query.filter(Order.owner_id == get_jwt_identity()).all() return [order.serialize for order in orders] From 9d46d327baa81079569023398cde8558f67fc629 Mon Sep 17 00:00:00 2001 From: Florian Vahl Date: Mon, 29 Jun 2020 23:04:56 +0200 Subject: [PATCH 4/5] Fix tip --- apos/resources/items.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apos/resources/items.py b/apos/resources/items.py index f917d58..050594a 100644 --- a/apos/resources/items.py +++ b/apos/resources/items.py @@ -9,11 +9,13 @@ item_create_parser = reqparse.RequestParser() item_create_parser.add_argument('name', type=str, required=True) item_create_parser.add_argument('tip_percent', type=int, required=False) +item_create_parser.add_argument('tip_absolute', type=int, required=False) item_create_parser.add_argument('price', type=int, required=False) # Why is the price false? item_patch_parser = reqparse.RequestParser() item_patch_parser.add_argument('name', type=str, required=False) item_patch_parser.add_argument('tip_percent', type=int, required=False) +item_patch_parser.add_argument('tip_absolute', type=int, required=False) item_patch_parser.add_argument('price', type=int, required=False) @@ -30,6 +32,8 @@ def put(self, order_id): order = Order.query.get(order_id) if order.deadline < datetime.utcnow(): abort(422, message="The order is expired, so no items can be added") + if args.get("tip_absolute", None) and args.get("tip_percent", None): + abort(422, message="The tip can only be provided in percent OR a absolute value") item = Item(order_id=order_id, user_id=get_jwt_identity(), **args) db.session.add(item) db.session.commit() @@ -70,6 +74,7 @@ def patch(self, order_id, item_id): if item.order.deadline < datetime.utcnow(): abort(422, message="The order is expired, so no items can be modified") item.update(args) + if item.tip_absolute and item.tip_percent: + abort(422, message="The tip can only be provided in percent OR a absolute value. Take care that not both are set in the db.") db.session.commit() return item.serialize - From 98bbffd2819ba567b1ba7c54117412d02329b593 Mon Sep 17 00:00:00 2001 From: Flova Date: Wed, 1 Jul 2020 10:56:58 +0200 Subject: [PATCH 5/5] Update apos/resources/items.py Co-authored-by: Timon Engelke --- apos/resources/items.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apos/resources/items.py b/apos/resources/items.py index 050594a..b3f3557 100644 --- a/apos/resources/items.py +++ b/apos/resources/items.py @@ -33,7 +33,7 @@ def put(self, order_id): if order.deadline < datetime.utcnow(): abort(422, message="The order is expired, so no items can be added") if args.get("tip_absolute", None) and args.get("tip_percent", None): - abort(422, message="The tip can only be provided in percent OR a absolute value") + abort(422, message="The tip can only be provided in percent OR absolute value") item = Item(order_id=order_id, user_id=get_jwt_identity(), **args) db.session.add(item) db.session.commit()