def run(self, streams): streams[self.datastream] = [] with open(self.file, "rb") as file: yield "Loading json file" records = json.load(file) yield "Parsing records" for recipe_data in tqdm.tqdm(records): recipe = Recipe() recipe.title = recipe_data["title"] recipe.ingredients = [] for item in recipe_data["ingredients"]: ing = Ingredient() ing.name = data.IngredientName(item["name"]) ing.unit = item["unit"] ing.quantity = item["quantity"] recipe.ingredients.append(ing) recipe.steps = recipe_data["instructions"] for item in recipe_data["tags"]: recipe.tags.append(Tag(item)) url = recipe_data["url"] url = url.replace("{", "%7B") url = url.replace("}", "%7D") url = url.replace("^", "%5E") recipe.add_prov("wasDerivedFrom", rdflib.URIRef(url)) streams[self.datastream].append(recipe)
def run(self, streams): streams[self.datastream] = [] recipes = streams[self.datastream] yield "Reading file" with open(self.file, "r", encoding="latin-1") as file: data = file.readlines() yield "Parsing recipes" mxp_recipes = mxp.parse_recipes(data) for mxp_recipe in tqdm.tqdm(mxp_recipes): recipe = Recipe() recipe.title = mxp_recipe.title # * is used to denote a regional tag, apparently for mxp_tag in mxp_recipe.categories: recipe.tags.append(Tag(mxp_tag.replace("*", ""))) for mxp_ing in mxp_recipe.ingredients: ing = Ingredient() ing.name = IngredientName(mxp_ing.ingredient) ing.quantity = mxp_ing.amount ing.unit = mxp_ing.measure ing.comment = mxp_ing.preparation_method recipe.ingredients.append(ing) recipes.append(recipe) yield "Read {0} recipes".format(len(recipes))
def test_recipe_is_created_in_category(self): """Tests if user recipe is added to dictionery""" Category('current_user', 'Pizza') meal = Recipe('current_user', 0, 'Pizza', 'Blah blah blah 2 spoons, and voila cooked') self.assertIn( 'Pizza', recipes[0][meal.recipe_id]['Recipe name'], )
def categories(): """routes to the categories page handles post request from user and adds them to the database in this case a dictionary, adds them to recipe page""" email = session['email'] form = RecipeForm(request.form) if request.method == 'POST' and form.validate(): recipe_name = form.recipe_name.data recipe_type = form.recipe_type.data recipe = form.recipe.data recipe_object = Recipe(email, recipe_name, recipe_type, recipe) flash('New recipe added', 'success') return redirect(url_for('myrecipes')) return render_template('Categories.html', form=form)
def run(self, streams): streams[self.datastream] = [] with open(self.file, "rb") as file: yield "Loading json file" data = json.load(file) yield "Parsing records" for recipe_data in tqdm.tqdm(data): recipe = Recipe() recipe.title = recipe_data["title"] recipe.ingredients = [ RawIngredient(x["text"]) for x in recipe_data["ingredients"] ] recipe.steps = [x["text"] for x in recipe_data["instructions"]] streams[self.datastream].append(recipe) recipe.add_prov("wasDerivedFrom", rdflib.URIRef(recipe_data["url"]))
def recipe(rid=None): """ This is the recipe view Will display ingredients, steps, and similar recipes with diffs """ if rid is None: ValueError("The recipe id is not found.") rid = int(rid) # 404 not found if rid not in recipe_data: return f"I cannot find a reicpe with id {rid}.", 404 rec = Recipe(int(rid)) return render_template("recipe.html", recipe=rec, similar_recs=rec.n_most_similar(n=10))
def edit_recipes(id): """Edit the recipes in the categories""" email = session['email'] category_id = request.args.get('val', '') result = recipes[int(category_id)][int(id)] form = RecipeForm(request.form) if request.method == 'GET': form.recipe_name.data = result['Recipe name'] form.recipe.data = result['Recipe'] if request.method == 'POST' and form.validate(): recipe_name = form.recipe_name.data recipe = form.recipe.data Recipe(email, int(category_id), recipe_name, recipe) del recipes[int(category_id)][int(id)] del all_categories[email][int(category_id)]['Category recipes'][int( id)] flash('Recipe Updated', 'success') return redirect(url_for('myrecipes', id=category_id)) return render_template('edit_recipes.html', form=form)
"""Yummy recipes app for creating,retrieving,updating and deleting recipes""" import os from functools import wraps import uuid from datetime import datetime from flask import Flask, render_template, flash, redirect, url_for, session, request from wtforms import Form, StringField, BooleanField, TextAreaField, SelectField, PasswordField, validators from recipes import recipes from data import Recipe, User, Category, Review app = Flask(__name__) all_recipes = recipes() new_recipe = Recipe() user = User() category = Category() review = Review() #All Recipes @app.route('/') def recipes(): """Display all recipes""" if all_recipes: return render_template('recipes.html', all_recipes=all_recipes, new_recipes=new_recipe.get_recipes()) else: msg = 'No Recipes Found' return render_template('recipes.html', msg=msg)
def setUp(self): app.secret_key = '\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8' self.recipe = Recipe() self.user = User() self.category = Category() self.review = Review() self.upVote = UpVote() self.recipe_data = { 'id': '5XXXXX', 'title': 'Recipe one one', 'category': 'General', 'ingredients': 'Ingredient one', 'steps': 'step onestep one step one', 'create_date': datetime.now(), 'created_by': 'Geofrey', 'private': False, 'votes': 0, 'reviews': 0, 'status': False } self.user_data = { 'id': '5XXXXX', 'name': 'Geofrey', 'email': '*****@*****.**', 'username': '******', 'password': '******', 'confirm': '12345' } self.user_data2 = { 'id': '5XXXXX', 'name': 'Geofrey', 'email': '*****@*****.**', 'username': '******', 'password': '******', 'confirm': '12345' } self.user_data3 = { 'id': '5XXXXX', 'name': 'Geofrey', 'email': '*****@*****.**', 'username': '******', 'password': '******', 'confirm': '12345' } self.cat_data = { 'id': '5XXXXX', 'name': 'General', 'created_by': 'Geofrocker' } self.review_data = { 'id': '1XXXXX', 'recipe_id': '5XXXXX', 'review': 'Coooool', 'created_by': 'Geofrocker', 'create_date': datetime.now() } self.upvote_data = { 'id': '1XXXXX', 'recipe_id': '5XXXXX', 'voted_by': 'Geofrocker' }
import sys import os sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) import data from data import Recipe, Ingredient, Tag from rdflib import Dataset from rdflib import URIRef, Literal r1 = Recipe() r1.title = "Foo" i1_1 = Ingredient() i1_1.name = "Foo_1" t1 = Tag("Baz") r1.ingredients.append(i1_1) r1.tags.append(t1) r1.add_prov("wasDerivedFrom", URIRef("http://recipes.com/r/Foo")) r1.add_pub_info("wasAttributedTo", Literal("Jeff the Data Guy")) summed = Dataset() for quad in r1.__publish__(): summed.add(quad) summed.namespace_manager.bind("np", data.NP, True) summed.namespace_manager.bind("recipe-kb", data.BASE, True)