def update_image(): if get_jwt_header()['type'] == "Chef": chef = Chef.get_or_none(Chef.email == get_jwt_identity()) if request.content_length == 0: return jsonify(message="No images passed", status="failed"), 400 elif request.files['chef_image']: file = request.files.get('chef_image') s3.upload_fileobj( file, "foodapp-new", f"chefs/{chef.id}/{file.filename}", ExtraArgs={ "ACL": "public-read", "ContentType": file.content_type } ) update = Chef.update({Chef.image_path:f"https://foodapp-new.s3-ap-southeast-1.amazonaws.com/chefs/{chef.id}/{file.filename}"}).where(Chef.username == chef.username).execute() updated_chef = Chef.get(Chef.id == chef.id) return jsonify({ "message": "Successfully updated chefs's profile image", "user_id": updated_chef.id, "image": updated_chef.image_path, }), 200 else: return jsonify(message="You are not logged in as Chef"), 400
def new_like(): user_id = request.json.get("user_id", None) existing_user = User.get_or_none(User.id == user_id) chef_id = request.json.get("chef_id", None) existing_chef = Chef.get_or_none(Chef.id == chef_id) existing_like = Like.get_or_none(Like.user_id == user_id, Like.chef_id == chef_id) if existing_user == None: return jsonify({ "message": "User does not exist", "status": "failed" }), 400 if existing_chef == None: return jsonify({ "message": "Chef does not exist", "status": "failed" }), 400 if existing_user and existing_chef: if existing_like: return jsonify({ "message": "User already liked this chef", "status": "failed" }), 400 else: new_like = Like(user=user_id, chef=chef_id) if new_like.save(): return jsonify({ "message": "Liked successfully", "status": "Success" })
def chef_approve(booking_id): if get_jwt_header()['type'] == "Chef": current_chef = Chef.get_or_none(Chef.email == get_jwt_identity()) booking = Booking.get_or_none(Booking.id == booking_id) if booking: if booking.chef_id == current_chef.id: booking.confirmed = True if booking.save(): return jsonify({ "booking_id": booking_id, "completed": booking.completed, "payment_status": booking.payment_status, "confirmed": booking.confirmed, "active": booking.active, "cancelled": booking.cancelled, "status": "success" }), 200 else: return jsonify({ "message": "You are logged in as another chef", "booking_chef_id": booking.chef_id, "status": "failed" }), 400 else: return jsonify({ "message": "Booking does not exist", "status": "Failed" }), 400 else: return jsonify({ "message": "You have to log in as Chef, not user", "status": "Failed" }), 400
def new_menu(chef_id): chef = Chef.get_or_none(Chef.id== chef_id) if chef: food_category = request.json.get('food_category', None) appetiser = request.json.get('appetiser', None) main = request.json.get('main', None) starter = request.json.get('starter', None) dessert = request.json.get('dessert', None) description = request.json.get('description', None) create_menu = ChefMenu(chef=chef_id, food_category=food_category, appetiser=appetiser, main=main, starter=starter, dessert=dessert, description=description) if create_menu.save(): return jsonify({ "message":"Successfully created new menu", "menu":{ "menu_id":create_menu.id, "chef": create_menu.chef_id, "food_category": create_menu.food_category, "appetiser" : create_menu.appetiser, "main": create_menu.main, "starter": create_menu.starter, "dessert": create_menu.dessert, "description": create_menu.description } }), 200 else: return jsonify({ "message": "Unable to create new menu", "status": "failed" }), 400 else: return jsonify({ "message":"This chef does not exist", "status":"failed" }), 400
def chef_category_menu(chef_id, food_category): chef = Chef.get_or_none(Chef.id == chef_id) if chef==None: return jsonify({"message": "This chef does not exist", "status": "failed"}), 400 else: menu = ChefMenu.get_or_none(ChefMenu.chef_id == chef_id, ChefMenu.food_category == food_category) if menu: return jsonify({ "menu_id": menu.id, "results": { "chef_id": menu.chef_id, "food_category": menu.food_category, "appetiser": menu.appetiser, "main": menu.main, "starter": menu.starter, "dessert": menu.dessert, "description": menu.description } }), 200 else: existing_food_category = FoodCategory.get_or_none(FoodCategory.chef_id == chef_id, FoodCategory.category == food_category) if existing_food_category: return jsonify({"results": []}), 400 else: return jsonify(message="This chef does not have this food category yet, create one first", status="Failed"), 400
def chef_login(): if request.content_length == 0: return jsonify(message="Nothing is passed to log in", status="Failed"), 400 else: password = request.json.get("password", None) email = request.json.get("email", None) chef = Chef.get_or_none(Chef.email == email) if chef: result = check_password_hash(chef.password_hash, password) access_token = create_access_token(identity=chef.email, expires_delta=datetime.timedelta(minutes=60), additional_headers={'type':'Chef'}) if result: return jsonify({ "auth_token": access_token, "message": "successfully signed in", "status": "success", "user": { "id": chef.id, "username": chef.username, "email": chef.email, "phone": chef.phone, "profileImage": chef.image_path } }) else: return jsonify({ "message": "Wrong password", "status": "failed" }), 400 else: return jsonify({ "message": "This account doesn't exist", "status": "failed" }), 400
def approved_bookings(): # Returns approved orders by Chef if get_jwt_header()['type'] == "Chef": current_chef = Chef.get_or_none(Chef.username == get_jwt_identity()) bookings = Booking.select().where(Booking.chef_id == current_chef.id, Booking.active == True, Booking.confirmed == True) if bookings: return jsonify({ "count": bookings.count(), "results": [ { # not complete information "booking_id": booking.id, "user_id": booking.user_id, "chef_id": booking.chef_id, "price": booking.price, "proposed_date": booking.proposed_date, "booking_completed": booking.completed, "booking_confirmed": booking.confirmed, "booking_active": booking.active, } for booking in bookings ] }) else: return jsonify({ "message": "There are no confirmed bookings for this chef", "status": "Failed" }), 400 # Returns confirmed bookings for User else: current_user = User.get_or_none(User.username == get_jwt_identity()) bookings = Booking.select().where(Booking.user_id == current_user.id, Booking.active == True, Booking.confirmed == True) if bookings: return jsonify({ "count": bookings.count(), "results": [ { # not complete information "booking_id": booking.id, "user_id": booking.user_id, "chef_id": booking.chef_id, "price": booking.price, "proposed_date": booking.proposed_date, "booking_completed": booking.completed, "booking_confirmed": booking.confirmed, "booking_active": booking.active, } for booking in bookings ] }) else: return jsonify({ "message": "There are no confirmed bookings for this user", "status": "Failed" }), 400
def delete_jwt(): if get_jwt_identity(): current_chef = get_jwt_identity() chef = Chef.get_or_none(Chef.email == current_chef) if chef: if chef.delete_instance(): return jsonify({"message": "Successfully deleted this chef", "chef_id": chef.id, "status": "success"}), 200 else: return jsonify({"message": "Unable to delete, chef no longer exist", "status": "failed"}), 400
def chef_new(): if request.content_length == 0: return jsonify(message="Nothing is passed, all fields are required!",status="failed"), 400 else: username = request.json.get("username", None) password = request.json.get("password", None) email = request.json.get("email", None) phone = request.json.get("phone", None) image_path = request.json.get("profileImage", None) if Chef.get_or_none(Chef.username == username): return jsonify(message="Username already exist", status="failed"), 400 elif Chef.get_or_none(Chef.email == email) : return jsonify(message="Email already exist", status="failed"), 400 else: newChef = Chef(username=username, email=email, password_hash=generate_password_hash(password), phone=phone, image_path=image_path) if newChef.save(): newChef = Chef.get(Chef.username == username, Chef.email == email) access_token = create_access_token(identity=newChef.email, expires_delta=datetime.timedelta(minutes=60), additional_headers={'type':'Chef'}) success_response = [{ "message": "Successfully created a user and signed in", "status": "success", "auth_token": access_token, "user": { "id": newChef.id, "username": newChef.username, "email": newChef.email, "phone": newChef.phone, "profileImage": newChef.image_path } }] return jsonify(success_response), 200
def update(): current_chef = get_jwt_identity() chef = Chef.get_or_none(Chef.email==current_chef) if get_jwt_header()['type'] == 'Chef': params = request.get_json() if params: params.update({'updated_at': datetime.datetime.now()}) for each in params: update = Chef.update(params).where(Chef.id == chef.id) if update.execute(): return jsonify({ "message": "Successfully updated", "menu_id": chef.id, "updated_at": chef.updated_at, "updated_column": [each for each in params] }) elif params == None: return jsonify({ "message": "No fields are passed", "status": "failed" }), 400 else: return jsonify(message="You are logged in as user instead of chef", status= "failed"), 400
def new_menu_image(chef_menu_id): existing_chef = Chef.get_or_none(Chef.email == get_jwt_identity()) if existing_chef: image_path = request.json.get("image_path", None) # breakpoint() new_menu_image = MenuImage(chef=existing_chef.id, chef_menu=chef_menu_id, image_path=image_path) if new_menu_image.save(): image = MenuImage.get(MenuImage.id == new_menu_image.id) return jsonify({ "message": "Successfully posted this menu's image", "chef_id": image.chef_id, "menu_id": image.chef_menu_id, "image_path": image.image_path, "menu_image_id": image.id }), 200
def index(chef_id): chef = Chef.get_or_none(Chef.id == chef_id) if chef: food_categories = FoodCategory.select( FoodCategory.chef_id, FoodCategory.category).where( FoodCategory.chef_id == chef_id).distinct() if food_categories: food_categories = { "count": food_categories.count(), "chef_id": chef_id, "food_category": [each.category for each in food_categories] } return jsonify(food_categories), 200 else: return jsonify([]), 200 else: return jsonify(message="This chef does not exist", status="failed"), 400
def review_chef(chef_id): existing_chef = Chef.get_or_none(Chef.id == chef_id) if existing_chef: reviews = Review.select().where(Review.chef == chef_id) if reviews: review = { "count": reviews.count(), "chef_reviews": [{ "id": review.id, "user_id": review.user_id, "chef_id" : review.chef_id, "comment" : review.comment, "rating" : review.rating } for review in reviews]} return jsonify(review), 200 else: return jsonify([]), 200 else: return jsonify(message="This chef does not exist", status="Failed"), 400
def index(): chefs = Chef.select() if chefs: all_chefs = { "_status": "success", "_no_of_chefs": chefs.count(), "results": [{ "profileImage": chef.image_path, "ratingAverage": chef.overall_rating, "createdAt": chef.created_at, "_id": chef.id, "name": chef.username, "email": chef.email, "phone": chef.phone } for chef in chefs] } return jsonify(all_chefs), 200 else: return jsonify({"results": []}), 200
def chef_id(id): chef = Chef.get_or_none(Chef.id == id) if chef: chef_profile = { "_status": "success", "chef_profile": { "profileImage": chef.image_path, "ratingAverage": chef.overall_rating, "createdAt": chef.created_at, "_id": chef.id, "name": chef.username, "email": chef.email, "phone": chef.phone }} return jsonify(chef_profile) else: return jsonify({ "message": "User does not exist", "status": "failed" }), 400
def review_new(): user = User.get_or_none(User.email == get_jwt_identity()) if request.is_json: chef_id = request.json.get("chef", None) comment = request.json.get("comment", None) rating = request.json.get("rating", None) existing_chef = Chef.get_or_none(Chef.id == chef_id) if existing_chef: review = Review(user=user.id, chef=existing_chef.id, comment=comment, rating=rating) if review.save(): return jsonify({ "message": "successfully submitted a review", "status": "success" }), 200 elif existing_chef==None: return jsonify(message ="Chef does not exist", status="failed"), 400 else: return jsonify(message="Nothing is passed"), 400
def chef_bookings(): existing_chef = Chef.get_or_none(Chef.email== get_jwt_identity()) if existing_chef: all_bookings = Booking.select().where(Booking.chef == existing_chef.id) if all_bookings: booking = { "status": "success", "count": all_bookings.count(), "results": [{ "booking_id": booking.id, "user": booking.user_id, "chef": booking.chef_id, "address": booking.address, "service_type": booking.service_type, "pax": booking.pax, "meal_type": booking.meal_type, "menu_type": booking.menu_type, "hob_type": booking.hob_type, "no_of_hob": booking.no_of_hob, "oven": booking.oven, "price": booking.price, "diet_restrictions": booking.diet_restrictions, "proposed_date": booking.proposed_date, "message": booking.message, "completed": booking.completed, "payment_status": booking.payment_status, "confirmed": booking.confirmed, "active": booking.active, "cancelled": booking.cancelled } for booking in all_bookings] } return jsonify(booking), 200 else: return jsonify({"results": []}), 200 else: return jsonify({ "message": "This chef does not exist", "status": "Failed" }), 400
def my_profile(): if get_jwt_identity(): current_chef = get_jwt_identity() if get_jwt_header()['type'] == 'Chef': chef = Chef.get_or_none(Chef.email == current_chef) if chef: return jsonify({ "_id": chef.id, "createdAt": chef.created_at, "email": chef.email, "name": chef.username, "phone": chef.phone, "profileImage": chef.image_path, "bio": chef.bio, "price": chef.price, "payment_info": chef.payment_info }), 200 else: return jsonify({"message": "You are logged in as chef, not user"}), 400 else: return jsonify({ "message": "User does not exist" }), 400