def kitchen_home(request, username): # check whether the person trying to access the kitchen is the owner of the username # used to request for the kitchen, if not just render the kitchen of the requested chef. # if the person requesting is the owner of the username, and they are a verified chef, # create a # kitchen for them context = {} chef = get_object_or_none(Chef, username=username) if chef.username == username: kitchen = get_object_or_none(Kitchen, owner=chef) if kitchen is None: kitchen_form = KitchenForm() context = {"kitchen_form": kitchen_form, "chef": chef} return render(request, "kitchen_reg_form.html", context=context) else: context = set_kitchen_context(chef, str(username)) else: if chef is not None: user = get_object_or_none(Follower, follower_id=request.user.id) kitchen = get_object_or_none(Kitchen, owner=chef) if kitchen is not None: if user is None: context = set_kitchen_context(chef) else: context = set_kitchen_context(chef, user) return render(request, "kitchen_layout.html", context=context)
def add_kitchen_menu(request, username): menu_form = MenuForm(request.POST or None) chef = get_object_or_none(Chef, username=username) kitchen = get_object_or_none(Kitchen, owner=chef) if request.method == 'POST': if menu_form.is_valid(): menu = menu_form.save(commit=False) menu.owner = kitchen menu.save() menu_form.save_m2m() return redirect("/chef/kitchen/{}/".format(username)) context = {"menu_form": menu_form, "username": username} return render(request, "menu_add_form.html", context=context)
def add_kitchen_recipe(request, username): recipe_form = RecipeForm(request.POST or None) chef = get_object_or_none(Chef, username=username) kitchen = get_object_or_none(Kitchen, owner=chef) if request.method == 'POST': if recipe_form.is_valid(): recipe = recipe_form.save(commit=False) recipe.owner = kitchen recipe.save() recipe_form.save_m2m() return redirect("/chef/kitchen/{}/".format(username)) context = {"recipe_form": recipe_form, "username": username} return render(request, "recipe_add_form.html", context=context)
def follow_kitchen(request, username): # get the list of follower/followers of a kitchen follower = get_object_or_none(Follower, follower=request.user) # check if the follower/followers list has the person trying to follow the kitchen # then check if the person requesting the kitchen is the owner if follower is None and request.user.username != username: # initialize/create the follower, add the user then save them follower = Follower(follower=request.user) follower.follower = request.user follower.save() return redirect("/kitchen/{}/".format(username))
def save_user_as_chef(request_user): if not get_object_or_none(Chef, pk=request_user.pk): chef = Chef() chef.pk = request_user.pk chef.id = request_user.id chef.owner = request_user.user chef.email_address = request_user.email chef.is_chef = True chef.save() else: pass
def chef_home(request): # check if the requesting user is an existing chef chef = get_object_or_none(Chef, owner_id=request.user.id) print(chef) # if not an existing chef render a form to save the rest of his info if not chef: chef_form = ChefForm(instance=request.user) return render(request, 'chef_reg_form.html', context={"chef_form": chef_form}) # if an existing chef redirect him to his profile page return render(request, "chef_home.html", context={"chef": chef})
def create_kitchen(request, username): kitchen_form = KitchenForm(request.POST or None) chef = get_object_or_none(Chef, username=username) if request.method == 'POST': if kitchen_form.is_valid(): kitchen = kitchen_form.save(commit=False) kitchen.owner = chef kitchen.save() kitchen_form.save_m2m() return redirect("/chef/kitchen/{}/".format(username)) context = {"kitchen_form": kitchen_form, "chef": chef} return render(request, "kitchen_reg_form.html", context=context)
def set_kitchen_context(chef, *args): kitchen = get_object_or_none(Kitchen, owner=chef) menus = filter_object_or_none(Menu, owner_id=kitchen.owner_id) recipes = filter_object_or_none(Recipe, owner_id=kitchen.owner_id) print(recipes) k_t = dict(KITCHEN_TYPE_CHOICES)[kitchen.kitchen_type] b_t = dict(BUSINESS_TYPE_CHOICES)[kitchen.business_type] context = {"chef": chef, "kitchen": kitchen, "dishes": menus, "recipes": recipes, "kitchen_type": k_t, "business_type": b_t, "username": args} return context
def unfollow_kitchen(request, username): f = get_object_or_none(Follower, follower=request.user) if f is not None: f.delete() return redirect("/kitchen/{}/".format(username))