def add_recipe(): """Function for adding a recipe""" form = RecipeForm(request.form) category = Category() all_categories = category.get_categories() if all_categories: form.category.choices = [(cat[1], cat[1]) for cat in all_categories] else: form.category.choices = [('General', 'General')] if request.method == 'POST' and form.validate(): title = form.title.data category = form.category.data ingredients = form.ingredients.data steps = form.steps.data status = form.status.data if status: private = True else: private = False if session['username']: created_by = session['username'] else: created_by = 'Anonymous' newrecip = [ str(uuid.uuid4()), title, category, ingredients, steps, datetime.now(), created_by, private ] new_recipe.set_recipe(newrecip) flash('Recipe created successfully', 'success') return redirect(url_for('dashboard')) return render_template('add_recipe.html', form=form)
def add_recipe(): """Function for adding a recipe""" form = RecipeForm(request.form) category = Category() all_categories = category.get_user_categories(session['username']) if all_categories: form.category.choices = [(cat['name'], cat['name']) for cat in all_categories] else: form.category.choices = [('General', 'General')] if request.method == 'POST' and form.validate(): recipe_data = {} title = form.title.data category = form.category.data ingredients = form.ingredients.data steps = form.steps.data status = form.status.data if status: private = True else: private = False if session['username']: created_by = session['username'] else: created_by = 'Anonymous' if new_recipe.get_recipe_titles(created_by, title): #flash message flash('Title already exists', 'danger') #redirect to home page return redirect(url_for('add_recipe')) recipe_data = { 'id': str(uuid.uuid4()), 'title': title, 'category': category, 'ingredients': ingredients, 'steps': steps, 'create_date': datetime.now(), 'created_by': created_by, 'private': private, 'votes': 0, 'reviews': 0 } response = new_recipe.set_recipe(recipe_data) flash(response, 'success') return redirect(url_for('dashboard')) return render_template('add_recipe.html', form=form)
def newEntry(bot): entry = Category(bot) Entries.entries.append(entry) Entries.unsaved_entries.append(entry) Entries.currentEntry = entry Entries._notify(Entries.UPDATE.ALL) return entry
def test_category_is_created(self): """This tests if a category is created""" meal = Category('current_user', 'Pizza') self.assertIn('current_user', all_categories, msg='Category was added') self.assertIn( 'Pizza', all_categories['current_user'][meal.category_id]['Category name'], )
def classify_pose(self, person: Person) -> List[Category]: """Run classification on an input. Args: person: A data.Person instance. Returns: A list of classification result (data.Category). Sorted by probability descending. """ # Check if all keypoints are detected before running the classifier. # If there's a keypoint below the threshold, return zero probability for all # class. min_score = min([keypoint.score for keypoint in person.keypoints]) if min_score < self.score_threshold: return [ Category(label=class_name, score=0) for class_name in self.pose_class_names ] # Flatten the input and add an extra dimension to match with the requirement # of the TFLite model. input_tensor = [[ keypoint.coordinate.y, keypoint.coordinate.x, keypoint.score ] for keypoint in person.keypoints] input_tensor = np.array(input_tensor).flatten().astype(np.float32) input_tensor = np.expand_dims(input_tensor, axis=0) # Set the input and run inference. self._interpreter.set_tensor(self._input_index, input_tensor) self._interpreter.invoke() # Extract the output and squeeze the batch dimension output = self._interpreter.get_tensor(self._output_index) output = np.squeeze(output, axis=0) # Sort output by probability descending. prob_descending = sorted(range(len(output)), key=lambda k: output[k], reverse=True) prob_list = [ Category(label=self.pose_class_names[idx], score=output[idx]) for idx in prob_descending ] return prob_list
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 edit_recipe(id): """Ëdit function for the recipe""" #get form form = RecipeForm(request.form) data = new_recipe.get_recipe(id) category = Category() all_categories = category.get_user_categories(session['username']) if all_categories: form.category.choices = [(cat['name'], cat['name']) for cat in all_categories] form.category.choices = [(data['category'], data['category'])] #populate form fields if new_recipe.get_recipe(id): form.title.data = data['title'] form.ingredients.data = data['ingredients'] form.steps.data = data['steps'] if request.method == 'POST' and form.validate(): title = request.form['title'] category = request.form['category'] ingredients = request.form['ingredients'] steps = request.form['steps'] private = form.status.data if session['username']: created_by = session['username'] else: created_by = 'Anonymous' recipe_data = { 'id': id, 'title': title, 'category': category, 'ingredients': ingredients, 'steps': steps, 'create_date': datetime.now(), 'created_by': created_by, 'private': private } response = new_recipe.edit_recipe(id, recipe_data) flash(response, 'success') return redirect(url_for('dashboard')) return render_template('edit_recipe.html', form=form)
def create_categories(): """routes to the categories creation page handles post request from user and adds them to the database in this case a dictionary, adds them to category page""" email = session['email'] form = CategoryForm(request.form) if request.method == 'POST' and form.validate(): category_name = form.category_name.data Category(email, category_name) flash('New Category added', 'success') return redirect(url_for('create')) return render_template('Create_categories.html', form=form)
def edit_categories(id): email = session['email'] result = all_categories[email][int(id)] form = CategoryForm(request.form) if request.method == 'GET': form.category_name.data = result['Category name'] if request.method == 'POST' and form.validate(): category_name = form.category_name.data Category(email, category_name) del all_categories[email][int(id)] flash('Category Updated', 'success') return redirect(url_for('create')) return render_template('edit_categories.html', form=form)
def loadEntries(bot): Entries.entries = [] if hasattr(bot, "_id"): categories = Database.find(Database.COLLECTIONS.CATEGORY, {"bot": bot._id}) for data in categories: c = Category(bot, data) Entries.entries.append(c) if (len(Entries.entries) > 0): Entries.currentEntry = Entries.entries[0] else: Entries.currentEntry = Entries.newEntry(bot) Entries._notify(Entries.UPDATE.ALL)
def edit_recipe(id): """Ëdit function for the recipe""" #get form form = RecipeForm(request.form) category = Category() all_categories = category.get_categories() if all_categories: form.category.choices = [(cat, cat) for cat in all_categories] form.category.choices = [('General', 'General')] #populate form fields if new_recipe.get_recipe(id): data = new_recipe.get_recipe(id) form.title.data = data[1] form.ingredients.data = data[3] form.steps.data = data[4] form.status.data = data[7] if request.method == 'POST' and form.validate(): title = request.form['title'] cat = request.form['category'] ingredients = request.form['ingredients'] steps = request.form['steps'] private = form.status.data if session['username']: created_by = session['username'] else: created_by = 'Anonymous' newrecip = [ id, title, cat, ingredients, steps, datetime.now(), created_by, private ] new_recipe.edit_recipe(id, newrecip) flash('Recipe edited successfully', 'success') return redirect(url_for('dashboard')) return render_template('edit_recipe.html', form=form)
def getEntryData(self): data = Category(None) data.name = self.editWidget.getName() data.domain = self.editWidget.getDomain() data.patterns = [] for i in range(self.editWidget.keywordList.count()): item = self.editWidget.keywordList.item(i) w = self.editWidget.keywordList.itemWidget(item) data.patterns.append(w.layout().itemAt(1).widget().text()) data.sentences = [] for i in range(self.editWidget.answerList.topLevelItemCount()): data.sentences.append(self.editWidget.answerList.topLevelItem(i).sentence) return data
"""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))