From ee19861056d6e53eb87028875f3e1d8762c2665b Mon Sep 17 00:00:00 2001 From: rebel_skum Date: Sat, 9 Dec 2017 15:53:15 -0800 Subject: [PATCH 1/2] refactored completed SQLAlchemy pt I --- .../04-flask-crud copy/app.py | 53 +++++++++++++ .../04-flask-crud copy/readme.md | 14 ++++ .../04-flask-crud copy/test.py | 59 +++++++++++++++ Unit-01/06-sql-alchemy-1/app.py | 75 +++++++++++++++++++ Unit-01/06-sql-alchemy-1/templates/base.html | 13 ++++ Unit-01/06-sql-alchemy-1/templates/edit.html | 15 ++++ Unit-01/06-sql-alchemy-1/templates/index.html | 16 ++++ Unit-01/06-sql-alchemy-1/templates/new.html | 15 ++++ Unit-01/06-sql-alchemy-1/templates/show.html | 13 ++++ 9 files changed, 273 insertions(+) create mode 100644 Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py create mode 100644 Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md create mode 100644 Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py create mode 100644 Unit-01/06-sql-alchemy-1/app.py create mode 100644 Unit-01/06-sql-alchemy-1/templates/base.html create mode 100644 Unit-01/06-sql-alchemy-1/templates/edit.html create mode 100644 Unit-01/06-sql-alchemy-1/templates/index.html create mode 100644 Unit-01/06-sql-alchemy-1/templates/new.html create mode 100644 Unit-01/06-sql-alchemy-1/templates/show.html diff --git a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py new file mode 100644 index 0000000..a74a290 --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py @@ -0,0 +1,53 @@ +from flask import Flask, render_template, redirect, url_for, request +from flask_modus import Modus +from snack import Snack + + +app = Flask(__name__) +modus = Modus(app) + +snickers = Snack(name = "snickers", kind = "candy bar") +cheetos = Snack(name = 'cheetos', kind = 'chips') +skittles = Snack(name = 'skittles', kind = 'candy') +peanuts = Snack(name = 'peanuts', kind = 'nuts') + +snack_list = [snickers, cheetos, skittles, peanuts] + + +@app.route('/') +def root(): + return redirect(url_for('index')) + +@app.route('/snacks', methods=["GET", "POST"]) +def index(): + if request.method == "POST": + snack_list.append(Snack(request.form['name'], request.form['kind'])) # name from type in new.html# + return redirect(url_for('index')) + return render_template('index.html', snacks=snack_list) + +@app.route('/snacks/new') +def new(): + return render_template('new.html') + +@app.route('/snacks/', methods=["GET", "PATCH", "DELETE"]) +def show(id): + found_snack = next(snack for snack in snack_list if snack.id == id) + + if request.method == b"PATCH": + found_snack.name = request.form['name'] + return redirect(url_for('index')) + + if request.method == b"DELETE": + snack_list.remove(found_snack) + return redirect(url_for('index')) + + return render_template('show.html', snack=found_snack) + +@app.route('/snacks//edit') +def edit(id): + #refactored using a list comprehension + found_snack = next(snack for snack in snack_list if snack.id == id) + return render_template('edit.html', snack=found_snack) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md new file mode 100644 index 0000000..89b6c18 --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md @@ -0,0 +1,14 @@ +# Flask CRUD + +For this application you will be building CRUD on the resouce `snacks`. Your application should: + +- display all the `snacks` +- allow a user to create `snacks` + - each snack should have a `name` and `kind` +- allow a user to edit a snack +- allow a user to delete a snack + +You should use a list to store your snacks and make a class for a snack to create instances from. + +**BONUS** Make your app look amazing with some CSS! +**BONUS** If you go to the show page for an invalid id, your applicaiton will break. Create a 404 page and redirect to it in the event that a user tries to go to the show or edit pages for a snack with an invalid id. diff --git a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py new file mode 100644 index 0000000..c910cdf --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py @@ -0,0 +1,59 @@ +from snack import Snack +from app import app, snack_list +import unittest + +class TestSnackMethods(unittest.TestCase): + + def setUp(self): + snack_list.append(Snack('snickers', 'chocolate')) + snack_list.append(Snack('skittles', 'candy')) + + def tearDown(self): + snack_list.clear() + Snack.id = 1 + + def test_index(self): + tester = app.test_client(self) + response = tester.get('/snacks', content_type='html/text') + self.assertEqual(response.status_code, 200) + + def test_new(self): + tester = app.test_client(self) + response = tester.get('/snacks/new', content_type='html/text') + self.assertEqual(response.status_code, 200) + + def test_edit(self): + tester = app.test_client(self) + response = tester.get('/snacks/1', content_type='html/text') + self.assertEqual(response.status_code, 200) + + def test_show(self): + tester = app.test_client(self) + response = tester.get('/snacks/1/edit', content_type='html/text') + self.assertEqual(response.status_code, 200) + + def test_creating_snack(self): + tester = app.test_client(self) + tester.post('/snacks', + data=dict(name="hersheys", kind="chocolate"), follow_redirects = True) + self.assertEqual(snack_list[2].id, 3) + self.assertEqual(snack_list[2].name, 'hersheys') + self.assertEqual(snack_list[2].kind, 'chocolate') + self.assertEqual(len(snack_list), 3) + + def test_editing_snack(self): + tester = app.test_client(self) + tester.post('/snacks/1?_method=PATCH', + data=dict(name="almond_snickers", kind="almonds_and_chocolate"), follow_redirects = True) + self.assertEqual(snack_list[0].name, 'almond_snickers') + self.assertEqual(snack_list[0].kind, 'almonds_and_chocolate') + self.assertEqual(len(snack_list), 2) + + def test_deleting_snack(self): + tester = app.test_client(self) + tester.post('/snacks/1?_method=DELETE', follow_redirects = True) + tester.post('/snacks/2?_method=DELETE', follow_redirects = True) + self.assertEqual(len(snack_list), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/Unit-01/06-sql-alchemy-1/app.py b/Unit-01/06-sql-alchemy-1/app.py new file mode 100644 index 0000000..4d11374 --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/app.py @@ -0,0 +1,75 @@ +from flask import Flask, render_template, redirect, url_for, request +from flask_sqlalchemy import SQLAlchemy #step 1: pip install flask_sqlalchemy psycopg2 +from flask_modus import Modus + + + +app = Flask(__name__) +#step 2: app.config to cofig to correct database +app.config['SQLALCHEMY_DATABASE_URI'] = "postgres://localhost/flask-snack-app" +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +modus = Modus(app) +db = SQLAlchemy(app) + + +#set up our table +class Snack(db.Model): + + __tablename__ = "snacks" + + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.Text) + kind = db.Column(db.Text) + + def __init__(self, name, kind): + self.name = name + self.kind = kind + + +# snack_list = [Snack('snickers', 'candy bar'), Snack('cheetos', 'chips'), Snack('skittles', 'candy'), Snack('peanuts', 'nuts')] + + +@app.route('/') +def root(): + return redirect(url_for('index')) + +@app.route('/snacks', methods=["GET", "POST"]) +def index(): + if request.method == "POST": + new_snack = Snack(request.form['name'], request.form['kind']) # name from type in new.html# + db.session.add(new_snack) + db.session.commit() + return redirect(url_for('index')) + return render_template('index.html', snacks=Snack.query.all()) + +@app.route('/snacks/new') +def new(): + return render_template('new.html') + +@app.route('/snacks/', methods=["GET", "PATCH", "DELETE"]) +def show(id): + found_snack = Snack.query.get(id) + + if request.method == b"PATCH": + found_snack.name = request.form['name'] + found_snack.kind = request.form['kind'] + db.session.add(found_snack) + db.session.commit() + return redirect(url_for('index')) + + if request.method == b"DELETE": + db.session.delete(found_snack) + db.session.commit() + return redirect(url_for('index')) + return render_template('show.html', snack=found_snack) + + return render_template('show.html', snack=found_snack) + +@app.route('/snacks//edit') +def edit(id): + #refactored using a list comprehension + found_snack = Snack.query.get(id) + return render_template('edit.html', snack=found_snack) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/Unit-01/06-sql-alchemy-1/templates/base.html b/Unit-01/06-sql-alchemy-1/templates/base.html new file mode 100644 index 0000000..3e55c67 --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/templates/base.html @@ -0,0 +1,13 @@ + + + + + + + Snacks CRUD App + + + {% block content %} + {% endblock %} + + diff --git a/Unit-01/06-sql-alchemy-1/templates/edit.html b/Unit-01/06-sql-alchemy-1/templates/edit.html new file mode 100644 index 0000000..6bccdfb --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/templates/edit.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% block content %} + +
+ + + +
+ +
+ + +
+ +{% endblock %} diff --git a/Unit-01/06-sql-alchemy-1/templates/index.html b/Unit-01/06-sql-alchemy-1/templates/index.html new file mode 100644 index 0000000..32b301b --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/templates/index.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% block content %} + +

Snacks App

+ + Add a new snack! + + + +{% endblock %} diff --git a/Unit-01/06-sql-alchemy-1/templates/new.html b/Unit-01/06-sql-alchemy-1/templates/new.html new file mode 100644 index 0000000..a2acd2e --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/templates/new.html @@ -0,0 +1,15 @@ +{% extends 'base.html' %} +{% block content %} + +

Add Yo' Snack!

+ +
+ + + + + + +
+ + {% endblock %} diff --git a/Unit-01/06-sql-alchemy-1/templates/show.html b/Unit-01/06-sql-alchemy-1/templates/show.html new file mode 100644 index 0000000..413369d --- /dev/null +++ b/Unit-01/06-sql-alchemy-1/templates/show.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% block content %} + +

More about this snack!

+

+ The name of the snack is {{snack.name}} - it's a tastey one! +

+ + See all the snacks +
+ Edit this snack + +{% endblock %} From dd650656be315ae50ac4123906df8038f7ab3e7b Mon Sep 17 00:00:00 2001 From: rebel_skum Date: Sat, 9 Dec 2017 16:31:57 -0800 Subject: [PATCH 2/2] correcting git and files --- .../04-flask-crud copy/app.py | 53 ----------------- .../04-flask-crud copy/readme.md | 14 ----- .../04-flask-crud copy/test.py | 59 ------------------- 3 files changed, 126 deletions(-) delete mode 100644 Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py delete mode 100644 Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md delete mode 100644 Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py diff --git a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py deleted file mode 100644 index a74a290..0000000 --- a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/app.py +++ /dev/null @@ -1,53 +0,0 @@ -from flask import Flask, render_template, redirect, url_for, request -from flask_modus import Modus -from snack import Snack - - -app = Flask(__name__) -modus = Modus(app) - -snickers = Snack(name = "snickers", kind = "candy bar") -cheetos = Snack(name = 'cheetos', kind = 'chips') -skittles = Snack(name = 'skittles', kind = 'candy') -peanuts = Snack(name = 'peanuts', kind = 'nuts') - -snack_list = [snickers, cheetos, skittles, peanuts] - - -@app.route('/') -def root(): - return redirect(url_for('index')) - -@app.route('/snacks', methods=["GET", "POST"]) -def index(): - if request.method == "POST": - snack_list.append(Snack(request.form['name'], request.form['kind'])) # name from type in new.html# - return redirect(url_for('index')) - return render_template('index.html', snacks=snack_list) - -@app.route('/snacks/new') -def new(): - return render_template('new.html') - -@app.route('/snacks/', methods=["GET", "PATCH", "DELETE"]) -def show(id): - found_snack = next(snack for snack in snack_list if snack.id == id) - - if request.method == b"PATCH": - found_snack.name = request.form['name'] - return redirect(url_for('index')) - - if request.method == b"DELETE": - snack_list.remove(found_snack) - return redirect(url_for('index')) - - return render_template('show.html', snack=found_snack) - -@app.route('/snacks//edit') -def edit(id): - #refactored using a list comprehension - found_snack = next(snack for snack in snack_list if snack.id == id) - return render_template('edit.html', snack=found_snack) - -if __name__ == '__main__': - app.run(debug=True) diff --git a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md deleted file mode 100644 index 89b6c18..0000000 --- a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/readme.md +++ /dev/null @@ -1,14 +0,0 @@ -# Flask CRUD - -For this application you will be building CRUD on the resouce `snacks`. Your application should: - -- display all the `snacks` -- allow a user to create `snacks` - - each snack should have a `name` and `kind` -- allow a user to edit a snack -- allow a user to delete a snack - -You should use a list to store your snacks and make a class for a snack to create instances from. - -**BONUS** Make your app look amazing with some CSS! -**BONUS** If you go to the show page for an invalid id, your applicaiton will break. Create a 404 page and redirect to it in the event that a user tries to go to the show or edit pages for a snack with an invalid id. diff --git a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py b/Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py deleted file mode 100644 index c910cdf..0000000 --- a/Unit-01/06-sql-alchemy-1/04-flask-crud copy/test.py +++ /dev/null @@ -1,59 +0,0 @@ -from snack import Snack -from app import app, snack_list -import unittest - -class TestSnackMethods(unittest.TestCase): - - def setUp(self): - snack_list.append(Snack('snickers', 'chocolate')) - snack_list.append(Snack('skittles', 'candy')) - - def tearDown(self): - snack_list.clear() - Snack.id = 1 - - def test_index(self): - tester = app.test_client(self) - response = tester.get('/snacks', content_type='html/text') - self.assertEqual(response.status_code, 200) - - def test_new(self): - tester = app.test_client(self) - response = tester.get('/snacks/new', content_type='html/text') - self.assertEqual(response.status_code, 200) - - def test_edit(self): - tester = app.test_client(self) - response = tester.get('/snacks/1', content_type='html/text') - self.assertEqual(response.status_code, 200) - - def test_show(self): - tester = app.test_client(self) - response = tester.get('/snacks/1/edit', content_type='html/text') - self.assertEqual(response.status_code, 200) - - def test_creating_snack(self): - tester = app.test_client(self) - tester.post('/snacks', - data=dict(name="hersheys", kind="chocolate"), follow_redirects = True) - self.assertEqual(snack_list[2].id, 3) - self.assertEqual(snack_list[2].name, 'hersheys') - self.assertEqual(snack_list[2].kind, 'chocolate') - self.assertEqual(len(snack_list), 3) - - def test_editing_snack(self): - tester = app.test_client(self) - tester.post('/snacks/1?_method=PATCH', - data=dict(name="almond_snickers", kind="almonds_and_chocolate"), follow_redirects = True) - self.assertEqual(snack_list[0].name, 'almond_snickers') - self.assertEqual(snack_list[0].kind, 'almonds_and_chocolate') - self.assertEqual(len(snack_list), 2) - - def test_deleting_snack(self): - tester = app.test_client(self) - tester.post('/snacks/1?_method=DELETE', follow_redirects = True) - tester.post('/snacks/2?_method=DELETE', follow_redirects = True) - self.assertEqual(len(snack_list), 0) - -if __name__ == '__main__': - unittest.main()