class Orders(Resource): @jwt_required @api.marshal_with(order_id, mask=None) @api.expect(new_order, validate=True) @api.doc(responses=get_codes(200), security="apiKey", params=auth) def post(self): order: Order = Order(**api.payload, login=get_jwt_identity()).commit recipients: List[str] = [api.payload.get("email")] try: body: str = body_template.format( firstName=order.firstName, id=order.id, date=date.today().strftime("%d %B %Y"), order_amount=order.comment.split()[-1]) msg: Message = Message("HoneyBunny заказ №{}".format(order.id), recipients=recipients, body=body) mail.send(msg) except Exception as err: import logging logging.error(order.comment) logging.error(err) return order @jwt_required @api.marshal_list_with(order_model, mask=None) @api.doc(responses=get_codes(200), security="apiKey", params=auth) def get(self): return Order.all(get_jwt_identity())
class CategoriesById(Resource): @api.doc(responses=get_codes(200, 404)) def delete(self, id: int): return "success" if Category.delete(id=id) else api.abort(404) @api.marshal_with(category_with_id, mask=None) @api.doc(responses=get_codes(200, 404)) def get(self, id: int): category: Category = Category.first(id=id) return category if category else api.abort(404)
class Categories(Resource): @api.expect(category_model, validate=True) @api.doc(responses=get_codes(200, 409)) def post(self): if Category(**api.payload).commit(): return "success" api.abort(409) @api.marshal_list_with(category_with_id, mask=None) @api.doc(responses=get_codes(200)) def get(self): return Category.all()
class Buskets(Resource): @api.expect(item_model, validate=True) @api.doc(responses=get_codes(200, 401, 409), **security) @jwt_required def post(self): return "success" if Busket(**api.payload).commit else api.abort(409) @jwt_required @api.doc(response=get_codes(200, 401), **security) def delete(self): Busket.delete_all( User.query.filter_by(login=get_jwt_identity()).first().id) return "success"
class BusketsByItemId(Resource): @api.expect(item_amount, validate=True) @api.doc(responses=get_codes(200, 401, 404), **security) @jwt_required def put(self, item_id): item: Optional[Busket] = Busket.query.filter_by(id=item_id).first() if item: item.update(api.payload["amount"]) return "success" api.abort(404) @api.doc(responses=get_codes(200, 404), **security) def delete(self, item_id): return "success" if Busket.delete(id=item_id) else api.abort(404)
class Goods(Resource): @api.marshal_list_with(good_with_id, mask=None) @api.doc(responses=get_codes(200)) def get(self): return Good.all() @api.expect(good_model, validate=True) @api.doc(responses=get_codes(200, 404, 409)) def post(self): category_name = api.payload.pop("category") category = Category.first(name=category_name) if not category: api.abort(404, "Category doesn't exist") good: Good = Good(category=category, **api.payload) return "success" if good.commit() else api.abort(409)
class BusketsByUserId(Resource): @api.marshal_list_with(item_with_good, mask=None) @api.doc(responses=get_codes(404), **security) def get(self, user_id: str): return [{ "id": good[0], "amount": good[1], "good": { "id": good[2], "name": good[3], "weight": good[4], "description": good[5], "measure": good[6], "price": good[7], "link": good[8], "category": good[9] } } for good in db.session.execute( """SELECT "Busket".id as item_id, "Busket".amount as amount, "Good".id as "id", "Good".name as name, "Good".weight as weight, "Good".description as description, "Good".measure as measure, "Good".price as price, "Good".link as link, "Category".name as category FROM "Busket" JOIN "Good" ON "Busket".good_id="Good".id JOIN "Category" ON "Good".category_id="Category".id WHERE "Busket".user_id={} ORDER BY item_id""".format(user_id))]
class GoodsById(Resource): @api.doc(responses=get_codes(200, 404)) def delete(self, id: int): return "success" if Good.delete(id=id) else api.abort(404) @api.expect(good_model) @api.doc(params={"id": "id"}, responses=get_codes(200, 404)) def put(self, id: int): good: Optional[Good] = Good.first(id=id) good.update(**api.payload) if good else api.abort(404) return "success" @api.marshal_with(good_with_id, mask=None) @api.doc(responses=get_codes(200, 404)) def get(self, id: int): good: Optional[Good] = Good.first(id=id) return good if good else api.abort(404)
class Login(Resource): @api.marshal_with(login_response_model) @api.expect(login_model, validate=True) @api.doc(responses=get_codes(401)) def post(self): user = User.log_in(**api.payload) return { "user": user, "accessToken": create_access_token(identity=user.login, expires_delta=datetime.timedelta(days=365)) } if user else api.abort(401, "Wrong email or password.")
class Auth(Resource): @api.expect(registration_model, validate=True) @api.doc(responses=get_codes(409)) @api.marshal_with(auth_response_model) def post(self): if User.is_exists_login(api.payload["login"]): return api.abort(409, "That login is taken. Try another.") if User.is_exists_email(api.payload.get("email")): return api.abort(409, "That email is taken. Try another.") user_id: int = User(**api.payload).commit.id identity: str = api.payload["login"] return { "accessToken": create_access_token(identity=identity, expires_delta=datetime.timedelta(days=365)), "id": user_id, "firstName": api.payload["firstName"], "lastName": api.payload["lastName"] }
class GoodsByCategory(Resource): @api.marshal_list_with(good_with_id, mask=None) @api.doc(responses=get_codes(200)) def get(self, category: str): return Category.get_all_goods(category)