def userProfile(): form = SearchForm() searchWord = request.args.get('search', "") try: page = int(request.args.get('page', 1)) if page < 1: page = 1 except ValueError: page = 1 if searchWord == "": count = Recipe.objects(user__id=current_user.id).count() else: count = Recipe.objects( user__id=current_user.id).search_text(searchWord).count() maxPage = count / MAX_RECIPES_PER_PAGE if maxPage > 0 and page > maxPage: page = math.ceil(maxPage) if searchWord == "": recipes = Recipe.objects(user__id=current_user.id).order_by( "-id").limit(MAX_RECIPES_PER_PAGE).skip(MAX_RECIPES_PER_PAGE * (page - 1)) else: recipes = Recipe.objects(user__id=current_user.id).search_text( searchWord).limit(MAX_RECIPES_PER_PAGE).skip(MAX_RECIPES_PER_PAGE * (page - 1)) return render_template('index.html', form=form, searchWord=searchWord, recipes=recipes, count=count, maxPage=maxPage, page=page, recipes_per_page=MAX_RECIPES_PER_PAGE)
def recipe(recipeID): try: recipe = Recipe.objects(id=recipeID).first() except ValidationError: abort(404) if not recipe: abort(404) return render_template('recipe.html', recipe=recipe)
def recipesByID(recipeID): try: recipe = Recipe.objects(id=recipeID).first() return Response(recipe.to_json(), mimetype='application/json', status=200) except Error as e: return error(str(e))
def recipes(): data = request.json try: if request.method == "GET": recipe = Recipe.objects().to_json() return Response(recipe, mimetype='application/json', status=200) elif request.method == "POST": recipe = Recipe.create(data) return Response(recipe.to_json(), mimetype='application/json', status=200) elif request.method == "PUT": recipe = Recipe.update(data) return Response(recipe.to_json(), mimetype='application/json', status=200) elif request.method == "DELETE": Recipe.delete(data) response = {"Result": "Recipe deleted"} return Response(json.dumps(response), mimetype='application/json', status=200) except Error as e: return error(str(e))
def downloadRecipe(recipeID): try: recipe = Recipe.objects(id=recipeID).first() except ValidationError: abort(404) if not recipe: abort(404) chefIcon = image_file_path_to_base64_string(current_app.root_path + '/static/icon/chef_white.png') html = render_template('download.html', recipe=recipe, chefIcon=chefIcon) css = [ "application/static/css/header.css", "application/static/css/download.css" ] pdf = pdfkit.from_string(html, False, css=css) response = make_response(pdf) response.headers['Content-Type'] = 'application/pdf' response.headers[ 'Content-Disposition'] = 'inline; filename=%s.pdf' % recipe.title return response
def editRecipe(recipeID): recipe = Recipe.objects(id=recipeID).first() if recipe.user.id != current_user.id: return redirect(url_for("main.home")) if not recipe: abort(404) return render_template('addRecipe.html', recipe=recipe)