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/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, diff --git a/apos/resources/items.py b/apos/resources/items.py index 1c5011c..b3f3557 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,11 +32,21 @@ 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 absolute value") item = Item(order_id=order_id, user_id=get_jwt_identity(), **args) db.session.add(item) db.session.commit() return item.serialize, 201 + +class ItemUserListResource(Resource): + @jwt_required + def get(self): + items = Item.query.filter_by(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): @@ -62,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 - diff --git a/apos/resources/orders.py b/apos/resources/orders.py index 31163e3..7cb18eb 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_id == get_jwt_identity()).all() + return [order.serialize for order in orders] + + class OrderResource(Resource): @jwt_required def get(self, order_id):