def signup(): """ Handles user signup. GET Displays signup form POST Creates/Adds new user to DB and redirects home """ form = SignupForm() if form.validate_on_submit(): user_data = generate_user_data(form) try: user = User.signup(user_data) add_and_commit(user) grocery_list = GroceryList.create(user.id) add_and_commit(grocery_list) except IntegrityError as error: db.session.rollback() if (f'(username)=({user.username}) already exists') in error._message(): flash("Username already taken", 'danger') elif (f'(email)=({user.email}) already exists') in error._message(): flash("Email already taken", 'danger') return render_template('users/signup.html', form=form) do_login(user) return redirect("/") else: return render_template('users/signup.html', form=form)
def add_ingredients_to_list(): """ Expects JSON with recipe id Creates grocery list if one is not already associated with this user. Adds ingredients to most recently created grocery list Returns JSON of created list and success message. """ # Check if authorized if not g.user: return abort(401) # Grab id from request id = request.json.get('id', None) # Make a grocery list if user doesn't already have one if not g.user.grocery_list: new_list = GroceryList(user_id=g.user.id) add_and_commit(new_list) # Grab most recent grocery list and recipe being added grocery_list = GroceryList.query.filter_by(user_id=g.user.id).order_by( GroceryList.date_created.desc()).first() recipe = Recipe.query.filter_by(id=id).first() # If recipe hasn't been saved to db, call API for recipe data and save it if not recipe: response = get_recipe(id) data = response.json() recipe = add_recipe_to_db(data) # Add recipe ingredients to grocery list if they're not already there for ingredient in recipe.ingredients: if ingredient not in grocery_list.ingredients: grocery_list.ingredients.append(ingredient) db.session.commit() # Return JSON response response_json = jsonify( grocery_list=grocery_list.serialize(), message="Ingredients Added!") return (response_json, 200)
def setUp(self): """Create test client, add sample data.""" self.client = app.test_client() Recipe.query.delete() GroceryList.query.delete() User.query.delete() u = User.signup({ 'email': "*****@*****.**", 'username': "******", 'password': "******", 'img_url': "www.test.com/test.jpg" }) r = Recipe(title="test recipe", image="test image path") db.session.add_all([u, r]) db.session.commit() g = GroceryList(user_id=u.id) db.session.add(g) db.session.commit() self.u = u self.u_id = u.id self.r = r self.r_id = r.id self.g = g self.g_id = g.id self.email = u.email self.username = u.username self.img_url = u.img_url
def groceryCreate(request, user=None, slug=None): """used to create and edit grocery list""" if user: owner = User.objects.get( username=user ) # setting the owner of the list instead of using request.user because if a list is shared request.user won't be the owner else: owner = request.user # must be coming in to create a new list and not edit another one this is the only way user wouldn't be passed ItemFormSet = inlineformset_factory(GroceryList, GroceryItem, extra=15, formset=GroceryItemFormSet, can_delete=True) if user and slug: # must be editing a list that is already created cur_list = get_object_or_404(GroceryList, author=owner, slug=slug) if owner.id != request.user.id: # if the person is the the owner check if the list is shared to the user shared = GroceryShared.objects.filter( list=cur_list) # check to see if this list is shared if shared: if shared[0].shared_to_id != request.user.id: output = _( 'You do not have permissions to edit this list ') messages.error(request, output) return redirect('grocery_list') else: output = _('You do not have permissions to edit this list ') messages.error(request, output) return redirect('grocery_list') else: cur_list = GroceryList() if request.method == 'POST': form = GroceryListForm(request.POST, instance=cur_list) formset = ItemFormSet(request.POST, instance=cur_list, user=owner) if form.is_valid() and formset.is_valid(): new_list = form.save() instances = formset.save(commit=False) # save the items separately for instance in instances: instance.list_id = new_list.id # set the grocery id foreign key to the this grocery id instance.save() return redirect('grocery_show', user=new_list.author, slug=new_list.slug) else: form = GroceryListForm(instance=cur_list) formset = ItemFormSet(instance=cur_list, user=owner) return render_to_response('list/grocerylist_form.html', { 'form': form, 'formset': formset, 'user': owner }, context_instance=RequestContext(request))
def groceryAddRecipe(request, recipe_slug): """Takes a recipe and adds all the ingredients from that recipe to a grocery list""" if request.method == "POST": # not validating the form since the form is only a prepopulated drop box and can't really be validated if ( request.POST["lists"] == "0" ): # must of selected to create a new list because no id can be set to zero otherwise list = GroceryList() list.title = date.today() list.author = request.user list.save() else: list = get_object_or_404(GroceryList, pk=request.POST["lists"], author=request.user) recipe = get_object_or_404(Recipe, pk=request.POST["recipe_id"]) new_groceryRecipe = GroceryRecipe() # save the recipe added to the grocery list new_groceryRecipe.recipe_id = recipe.id new_groceryRecipe.list_id = list.id new_groceryRecipe.save() for ing in recipe.ingredients.all(): new_item = GroceryItem() new_item.list_id = list.id new_item.item = str(ing.quantity) + " " + str(ing.measurement) + " " + ing.title new_item.save() if recipe.related: new_groceryRecipe = GroceryRecipe() new_groceryRecipe.recipe_id = recipe.related.id new_groceryRecipe.list_id = list.id new_groceryRecipe.save() for ing in recipe.related.ingredients.all(): new_item = GroceryItem() new_item.list_id = list.id new_item.item = str(ing.quantity) + " " + str(ing.measurement) + " " + ing.title new_item.save() return redirect("grocery_edit", user=list.author, slug=list.slug) else: recipe = get_object_or_404(Recipe, slug=recipe_slug) form = GroceryUserList(user=request.user) return render_to_response( "list/grocery_addrecipe.html", {"form": form, "recipe": recipe}, context_instance=RequestContext(request) )
def setUp(self): db.create_all() # Adding Users user = User(name='user1', email="*****@*****.**") user2 = User(name='user2', email="*****@*****.**") user3 = User(name='user3', email="*****@*****.**") db.session.add_all([user, user2, user3]) # Adding Families family = Family(name='Family-name-1', members=[user, user2]) family2 = Family(name='Family-name-2', members=[user, user3]) db.session.add_all([family, family2, user3]) # Adding Fridges fridge = Fridge() fridge2 = Fridge() db.session.add_all([fridge, fridge2]) family.fridge = fridge family2.fridge = fridge2 # Adding Grocery Lists grocery_list = GroceryList(name="listname1") grocery_list2 = GroceryList(name="listname2") grocery_list3 = GroceryList(name="listname3") db.session.add_all([grocery_list, grocery_list2, grocery_list3]) family.grocery_lists.append(grocery_list) family.grocery_lists.append(grocery_list2) family2.grocery_lists.append(grocery_list3) db.session.commit() # Adding items item1 = Item(name="item1", family_pk=family.pk) item2 = Item(name="item2", family_pk=family.pk) item3 = Item(name="item3", family_pk=family.pk) item4 = Item(name="item4", family_pk=family2.pk) item5 = Item(name="item5", family_pk=family2.pk) db.session.add_all([item1, item2, item3, item4, item5]) # Adding items to fridge fridge.items.append(item1) fridge2.items.append(item2) # Adding items to grocery lists grocery_list.items.append(item3) grocery_list3.items.append(item4) grocery_list3.items.append(item5) db.session.commit()
def groceryAddRecipe(request, recipe_slug): """Takes a recipe and adds all the ingredients from that recipe to a grocery list""" if request.method == 'POST': # not validating the form since the form is only a prepopulated drop box and can't really be validated if request.POST[ 'lists'] == '0': # must of selected to create a new list because no id can be set to zero otherwise list = GroceryList() list.title = date.today() list.author = request.user list.save() else: list = get_object_or_404(GroceryList, pk=request.POST['lists'], author=request.user) recipe = get_object_or_404(Recipe, pk=request.POST['recipe_id']) new_groceryRecipe = GroceryRecipe( ) # save the recipe added to the grocery list new_groceryRecipe.recipe_id = recipe.id new_groceryRecipe.list_id = list.id new_groceryRecipe.save() for ing in recipe.ingredients.all(): new_item = GroceryItem() new_item.list_id = list.id new_item.item = str(ing.quantity) + ' ' + str( ing.measurement) + ' ' + ing.title new_item.save() if recipe.related: new_groceryRecipe = GroceryRecipe() new_groceryRecipe.recipe_id = recipe.related.id new_groceryRecipe.list_id = list.id new_groceryRecipe.save() for ing in recipe.related.ingredients.all(): new_item = GroceryItem() new_item.list_id = list.id new_item.item = str(ing.quantity) + ' ' + str( ing.measurement) + ' ' + ing.title new_item.save() return redirect('grocery_edit', user=list.author, slug=list.slug) else: recipe = get_object_or_404(Recipe, slug=recipe_slug) form = GroceryUserList(user=request.user) return render_to_response('list/grocery_addrecipe.html', { 'form': form, 'recipe': recipe }, context_instance=RequestContext(request))