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 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 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 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 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' }
class DataTestCase(unittest.TestCase): 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' } def test_add_recipe(self): '''test add recipe''' response = self.recipe.set_recipe(self.recipe_data) self.assertEqual(response, "Recipe created successfully", msg="Can't create recipe") def test_edit_recipe(self): '''test edit recipe''' self.recipe.set_recipe(self.recipe_data) response = self.recipe.edit_recipe('5XXXXX', self.recipe_data) response2 = self.recipe.edit_recipe('5XERROR', self.recipe_data) self.assertEqual(response, "Recipe does not exist", msg="Can't edit recipe") self.assertEqual(response2, "Recipe does not exist", msg="Can't edit recipe") def test_delete_recipe(self): '''test delete recipe''' self.recipe.set_recipe(self.recipe_data) response = self.recipe.delete_recipe('5XXXXX') response2 = self.recipe.delete_recipe('5XERROR') self.assertEqual(response, "Recipe deleted", msg="Can't delete recipe") self.assertEqual(response2, "Recipe does not exist", msg="Can't delete recipe") def test_get_recipes(self): '''test get recipes''' response = self.recipe.get_recipes() self.assertIsInstance(response, list, msg="Can't get recipes") self.assertIsInstance(response[0], dict, msg="Can't get recipes") def test_get_recipe(self): response = self.recipe.get_recipe('5XXXXX') self.assertIsInstance(response, dict, msg="Can't get recipes") def get_user_recipes(self): '''test get user recipes''' response = self.recipe.get_user_recipes('Geofrocker') self.assertIsInstance(response, list, msg="Can't get recipes") self.assertIsInstance(response[0], dict, msg="Can't get recipes") def get_recipe_titles(self): '''test get recipe titles''' response = self.recipe.get_recipe_titles('Geofrocker', 'Recipe one') self.assertIsInstance(response, list, msg="Can't get recipes") self.assertIsInstance(response[0], dict, msg="Can't get recipes") def test_reg_user(self): '''test register user''' response = self.user.register_user(self.user_data) self.assertEqual(response, "Your are now registered and can log in", msg="Can't create recipe") def test_check_user_name(self): '''test username check''' self.user.register_user(self.user_data) response = self.user.check_user_name('Geofrocker') self.assertIsInstance(response, list, msg="Can't get username") self.assertIsInstance(response[0], dict, msg="Can't get username") def test_check_user_email(self): '''test user email check''' self.user.register_user(self.user_data) response = self.user.check_user_email('*****@*****.**') self.assertIsInstance(response, list, msg="Can't get user email") self.assertIsInstance(response[0], dict, msg="Can't get user email") def test_login_user(self): '''test user login''' response = self.user.login_user('Geofrocker', '12345') self.assertIsInstance(response, list, msg="Can't login user") self.assertIsInstance(response[0], dict, msg="Can't login user") def test_set_category(self): '''test set category''' response = self.category.set_category(self.cat_data) self.assertEqual(response, "Category created successfully", msg="Can't create category") def test_get_category(self): '''test get actegory''' self.category.set_category(self.cat_data) response = self.category.get_category('5XXXXX') self.assertEqual(response, "General", msg="Can't get category") def test_edit_category(self): '''test edit category''' self.category.set_category(self.cat_data) response = self.category.edit_category('5XXXXX', self.cat_data) self.assertEqual(response, "Category edited successfully", msg="Can't edit category") def test_delete_category(self): '''test delete category''' self.category.set_category(self.cat_data) response = self.category.delete_category('5XXXXX') self.assertEqual(response, "Category deleted Successfully", msg="Can't delete category") def test_get_categories(self): '''test get categories''' response = self.category.get_categories() self.assertIsInstance(response, list, msg="Can't get categories") self.assertIsInstance(response[0], dict, msg="Can't get categories") def test_get_user_categories(self): '''test get user categories''' response = self.category.get_user_categories('Geofrocker') self.assertIsInstance(response, list, msg="Can't get user categories") self.assertIsInstance(response[0], dict, msg="Can't get user categories") def test_get_category_name(self): '''test get category name''' response = self.category.get_category_name('Geofrocker', 'General') self.assertIsInstance(response, list, msg="Can't get category name") self.assertIsInstance(response[0], dict, msg="Can't get category name") def test_set_review(self): '''test set review''' response = self.review.set_review(self.review_data) self.assertEqual(response, "Review created successfully", msg="Can't create review") def test_get_reviews(self): '''test get reviews''' self.review.set_review(self.review_data) response = self.review.get_reviews('5XXXXX') self.assertIsInstance(response, list, msg="Can't get review") self.assertIsInstance(response[0], dict, msg="Can't get reviews") def test_get_num_reviews(self): '''test get number of reviews''' self.review.set_review(self.review_data) response = self.review.get_num_reviews('5XXXXX') self.assertIsInstance(response, int, msg="Can't get number reviews") def test_set_upvote(self): '''test set upvote''' response = self.upVote.set_upvote(self.upvote_data) self.assertEqual(response, "Recipe upvoted Successfully", msg="Can't upvote recipe") def test_check_upvote(self): '''test check upvotes''' self.upVote.set_upvote(self.upvote_data) response = self.upVote.check_upvote('Geofrocker', '5XXXXX') self.assertIsInstance(response, list, msg="Can't get upvote") self.assertIsInstance(response[0], dict, msg="Can't get upvote") def test_get_upVotes(self): '''test get number of upvotes''' self.upVote.set_upvote(self.upvote_data) response = self.upVote.get_upVotes('5XXXXX') self.assertIsInstance(response, int, msg="Can't get votes") #++++++++++++++++++++++Testing end points++++++++++++++++++++++++++++++++++++++++++++++++++++++ def test_home_page_header(self): """Test home page""" client = app.test_client() rsp = client.get('/') self.assertIn('Recipe one', str(rsp.data)) def test_login_page_header(self): """Test login_page""" client = app.test_client(self) rsp = client.post('/login', content_type='application/x-www-form-urlencoded', data={ 'username': '******', 'password': '******' }, follow_redirects=True) self.assertIn('User not found', str(rsp.data)) rsp = client.post('/login', content_type='application/x-www-form-urlencoded', data=self.user_data, follow_redirects=True) self.assertIn('Your are now logged in', str(rsp.data)) def test_logout_header(self): """Test dashboard_page""" with app.test_client() as c: with c.session_transaction() as sess: sess.clear() rsp = c.get('/logout', follow_redirects=True) self.assertTrue(rsp, msg='Cant logout') def test_register_page_header(self): """Test register_page""" client = app.test_client(self) rsp = client.post('/register', content_type='application/x-www-form-urlencoded', data=self.user_data, follow_redirects=True) self.assertIn('Username already taken', str(rsp.data)) rsp = client.post('/register', content_type='application/x-www-form-urlencoded', data=self.user_data3, follow_redirects=True) self.assertIn('Email already exists', str(rsp.data)) rsp = client.post('/register', content_type='application/x-www-form-urlencoded', data=self.user_data2, follow_redirects=True) self.assertIn('Your are now registered and can log in', str(rsp.data)) def test_dashboard_page_header(self): """Test dashboard_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.get('/dashboard') self.assertIn('Recipe one', str(rsp.data)) def test_add_recipe_page_header(self): """Test edit_recipe_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/add_recipe', content_type='application/x-www-form-urlencoded', data=self.recipe_data, follow_redirects=True) self.assertIn('Recipe created successfully', str(rsp.data)) def test_edit_recipe_page_header(self): """Test edit_recipe_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/edit_recipe/5XXXXX', content_type='application/x-www-form-urlencoded', data=self.recipe_data, follow_redirects=True) self.assertIn('Recipe does not exist', str(rsp.data)) def test_delete_recipe_page_header(self): """Test delete_recipe_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/delete_recipe/5XXXXX', follow_redirects=True) self.assertIn('Recipe does not exist', str(rsp.data)) def test_add_category_page_header(self): """Test add_category_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/add_category', content_type='application/x-www-form-urlencoded', data=self.cat_data, follow_redirects=True) self.assertIn('Category created successfully', str(rsp.data)) def test_edit_category_page_header(self): """Test edit_category_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/edit_category/5XXXXX', content_type='application/x-www-form-urlencoded', data=self.cat_data, follow_redirects=True) self.assertIn('Category edited successfully', str(rsp.data)) def test_delete_category_page_header(self): """Test delete_category_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/delete_category/1XXXXX', follow_redirects=True) self.assertIn('', str(rsp.data)) def test_add_review_page_header(self): """Test add_review_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/recipe/5XXXXX', content_type='application/x-www-form-urlencoded', data=self.review_data, follow_redirects=True) self.assertIn('Review created successfully', str(rsp.data)) def test_add_upvote_page_header(self): """Test upvote_page""" with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' sess['logged_in'] = True rsp = c.post('/up_vote/5XXXXX', content_type='application/x-www-form-urlencoded', data=self.upvote_data, follow_redirects=True) self.assertIn('Recipe upvoted Successfully', str(rsp.data))
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)