def delete_from_cart(self, recipe): from models.subscription_recipe import Subscription_Recipe import datetime return Subscription_Recipe.delete().where( Subscription_Recipe.user == self.id, Subscription_Recipe.recipe == recipe.id, Subscription_Recipe.created_at >= datetime.date.today()).execute()
def view_cart(user_id): user = User.get_or_none(User.id == user_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == current_user.id, Subscription_Recipe.created_at >= datetime.date.today()) return render_template("users/checkout.html", subscription_recipes=subscription_recipes, user=current_user)
def index(): subscription_recipes = Subscription_Recipe.select() return jsonify([{ "id": s.id, "user": s.user.id, "subscription": s.subscription.id, "recipe": s.recipe.id } for s in subscription_recipes])
def user(): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id) return jsonify([{ "id": s.id, "user": s.user.id, "subscription": s.subscription.id, "recipe": s.recipe.id } for s in subscription_recipes])
def reminder_sunday(): user_recipes = Subscription_Recipe.select().where( Subscription_Recipe.created_at.between( date.today() - timedelta(days=7), date.today())) user_id = [u.user.id for u in user_recipes] to_send_email_id = User.select().where(User.id.not_in(user_id)) to_send_email_list_sunday = [u.email for u in to_send_email_id] for email in to_send_email_list_sunday: msg = Message('Meals Reminder', recipients=[email]) msg.body = "Hi! We noticed that you have not complete selecting your meals for the following week. Last order will be Today 2359." mail.send(msg)
def checkout(user_id): user = User.get_or_none(User.id == user_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == current_user.id, Subscription_Recipe.created_at >= datetime.date.today()) for s in subscription_recipes: order_checkout = OrderCheckout(subscription_recipe=s.id, user=user.id) order_checkout.save() if order_checkout.save(): send_msg_checkout.delay(email=current_user.email, name=current_user.name) flash("Successfully ordered", "success") return redirect(url_for('home')) else: flash("Error occured during confirmation. Try again", "danger") return redirect(url_for('users.view_cart', user_id=current_user.id))
def checkout(): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id, Subscription_Recipe.created_at >= datetime.date.today(), Subscription_Recipe.is_checkedout == 0) for s in subscription_recipes: order_checkout = OrderCheckout(subscription_recipe=s.id, user=user.id) order_checkout.save() s.is_checkedout = 1 s.save() if order_checkout.save() and s.save(): send_msg_checkout.delay(email=user.email, name=user.name) return jsonify({"message": "Successfully checked out"}) else: return jsonify({"message": "Error occured, try again."})
def today(): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id, Subscription_Recipe.created_at >= datetime.date.today(), Subscription_Recipe.is_checkedout == 0) return jsonify([{ "id": s.id, "user": s.user.id, "subscription": s.subscription.id, "recipe": s.recipe.id, "recipe_image_path": app.config.get("S3_LOCATION") + s.recipe.image_url, "recipe_name": s.recipe.recipe_name } for s in subscription_recipes])
def user_add_cart(recipe_id): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) ingredients = request.json["selectedIngredients"] recipe = Recipe.get_or_none(Recipe.id == recipe_id) subscription_recipes = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id, Subscription_Recipe.created_at.between( fn.date_trunc('week', date.today()), date.today() + timedelta(days=1))) temp = Subscription_Recipe.select().where( Subscription_Recipe.user == user.id, Subscription_Recipe.created_at >= datetime.date.today(), Subscription_Recipe.is_checkedout == 0, Subscription_Recipe.recipe == recipe.id) if temp: return jsonify({"message": "Item is already in the cart"}) else: if len(subscription_recipes) >= (user.subscription.amount_of_meals): return jsonify({ "message": "You have reached the maximum amount of meals selected in a week" }) else: new_subscription_recipe = Subscription_Recipe( user=user.id, subscription=user.subscription.id, recipe=recipe.id) new_subscription_recipe.save() for ingredient in ingredients: user_recipe = Subscription_Recipe.select().where( Subscription_Recipe.recipe == recipe.id, Subscription_Recipe.user == user.id).order_by( Subscription_Recipe.created_at.desc()).get() order = Order(subscription_recipe=user_recipe.id, ingredient=ingredient) order.save() if order.save(): return jsonify({"message": "Successfully added to cart"}) else: return jsonify({"message": "Error occured"})
def add_to_cart(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) ingredients = request.form.getlist('ingredients') subscription_recipe = Subscription_Recipe( user=current_user.id, subscription=current_user.subscription, recipe=recipe.id) subscription_recipe.save() user_recipe = Subscription_Recipe.select().where( Subscription_Recipe.recipe == recipe.id, Subscription_Recipe.user == current_user.id).order_by( Subscription_Recipe.created_at.desc()).get() for ingredient in ingredients: order = Order(subscription_recipe=user_recipe.id, ingredient=ingredient) order.save() if subscription_recipe.save() and order.save(): flash("Successfully added to cart", "success") return redirect(url_for('recipes.show')) else: flash("Failed to add to cart", "danger") return redirect(url_for('recipes.show'))