From 7b992f1e6e246e3ce1fa0ca34231ecda1fa151a0 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Wed, 3 Jun 2020 21:11:31 -0700 Subject: [PATCH 1/8] added flask script in python --- Pipfile | 14 ++ Pipfile.lock | 179 ++++++++++++++++++ .../hello.py | 14 ++ 3 files changed, 207 insertions(+) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 module1-web-application-development-with-flask/hello.py diff --git a/Pipfile b/Pipfile new file mode 100644 index 00000000..776e8a14 --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +flask = "*" +flask-sqlalchemy = "*" +flask-migrate = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 00000000..8645869d --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,179 @@ +{ + "_meta": { + "hash": { + "sha256": "3c1deb6380d9107dcb7ab44fcdad47a0f792c66a16c3b0f2dec2379d6767243f" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "alembic": { + "hashes": [ + "sha256:035ab00497217628bf5d0be82d664d8713ab13d37b630084da8e1f98facf4dbf" + ], + "version": "==1.4.2" + }, + "click": { + "hashes": [ + "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a", + "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc" + ], + "version": "==7.1.2" + }, + "flask": { + "hashes": [ + "sha256:4efa1ae2d7c9865af48986de8aeb8504bf32c7f3d6fdc9353d34b21f4b127060", + "sha256:8a4fdd8936eba2512e9c85df320a37e694c93945b33ef33c89946a340a238557" + ], + "index": "pypi", + "version": "==1.1.2" + }, + "flask-migrate": { + "hashes": [ + "sha256:4dc4a5cce8cbbb06b8dc963fd86cf8136bd7d875aabe2d840302ea739b243732", + "sha256:a69d508c2e09d289f6e55a417b3b8c7bfe70e640f53d2d9deb0d056a384f37ee" + ], + "index": "pypi", + "version": "==2.5.3" + }, + "flask-sqlalchemy": { + "hashes": [ + "sha256:0b656fbf87c5f24109d859bafa791d29751fabbda2302b606881ae5485b557a5", + "sha256:fcfe6df52cd2ed8a63008ca36b86a51fa7a4b70cef1c39e5625f722fca32308e" + ], + "index": "pypi", + "version": "==2.4.3" + }, + "itsdangerous": { + "hashes": [ + "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19", + "sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749" + ], + "version": "==1.1.0" + }, + "jinja2": { + "hashes": [ + "sha256:89aab215427ef59c34ad58735269eb58b1a5808103067f7bb9d5836c651b3bb0", + "sha256:f0a4641d3cf955324a89c04f3d94663aa4d638abe8f733ecd3582848e1c37035" + ], + "version": "==2.11.2" + }, + "mako": { + "hashes": [ + "sha256:8195c8c1400ceb53496064314c6736719c6f25e7479cd24c77be3d9361cddc27", + "sha256:93729a258e4ff0747c876bd9e20df1b9758028946e976324ccd2d68245c7b6a9" + ], + "version": "==1.1.3" + }, + "markupsafe": { + "hashes": [ + "sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473", + "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161", + "sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235", + "sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5", + "sha256:13d3144e1e340870b25e7b10b98d779608c02016d5184cfb9927a9f10c689f42", + "sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff", + "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b", + "sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1", + "sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e", + "sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183", + "sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66", + "sha256:596510de112c685489095da617b5bcbbac7dd6384aeebeda4df6025d0256a81b", + "sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1", + "sha256:6788b695d50a51edb699cb55e35487e430fa21f1ed838122d722e0ff0ac5ba15", + "sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1", + "sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e", + "sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b", + "sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905", + "sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735", + "sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d", + "sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e", + "sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d", + "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c", + "sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21", + "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2", + "sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5", + "sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b", + "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6", + "sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f", + "sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f", + "sha256:cdb132fc825c38e1aeec2c8aa9338310d29d337bebbd7baa06889d09a60a1fa2", + "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7", + "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be" + ], + "version": "==1.1.1" + }, + "python-dateutil": { + "hashes": [ + "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c", + "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a" + ], + "version": "==2.8.1" + }, + "python-editor": { + "hashes": [ + "sha256:1bf6e860a8ad52a14c3ee1252d5dc25b2030618ed80c022598f00176adc8367d", + "sha256:51fda6bcc5ddbbb7063b2af7509e43bd84bfc32a4ff71349ec7847713882327b", + "sha256:5f98b069316ea1c2ed3f67e7f5df6c0d8f10b689964a4a811ff64f0106819ec8" + ], + "version": "==1.0.4" + }, + "six": { + "hashes": [ + "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", + "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" + ], + "version": "==1.15.0" + }, + "sqlalchemy": { + "hashes": [ + "sha256:128bc917ed20d78143a45024455ff0aed7d3b96772eba13d5dbaf9cc57e5c41b", + "sha256:156a27548ba4e1fed944ff9fcdc150633e61d350d673ae7baaf6c25c04ac1f71", + "sha256:27e2efc8f77661c9af2681755974205e7462f1ae126f498f4fe12a8b24761d15", + "sha256:2a12f8be25b9ea3d1d5b165202181f2b7da4b3395289000284e5bb86154ce87c", + "sha256:31c043d5211aa0e0773821fcc318eb5cbe2ec916dfbc4c6eea0c5188971988eb", + "sha256:65eb3b03229f684af0cf0ad3bcc771970c1260a82a791a8d07bffb63d8c95bcc", + "sha256:6cd157ce74a911325e164441ff2d9b4e244659a25b3146310518d83202f15f7a", + "sha256:703c002277f0fbc3c04d0ae4989a174753a7554b2963c584ce2ec0cddcf2bc53", + "sha256:869bbb637de58ab0a912b7f20e9192132f9fbc47fc6b5111cd1e0f6cdf5cf9b0", + "sha256:8a0e0cd21da047ea10267c37caf12add400a92f0620c8bc09e4a6531a765d6d7", + "sha256:8d01e949a5d22e5c4800d59b50617c56125fc187fbeb8fa423e99858546de616", + "sha256:925b4fe5e7c03ed76912b75a9a41dfd682d59c0be43bce88d3b27f7f5ba028fb", + "sha256:9cb1819008f0225a7c066cac8bb0cf90847b2c4a6eb9ebb7431dbd00c56c06c5", + "sha256:a87d496884f40c94c85a647c385f4fd5887941d2609f71043e2b73f2436d9c65", + "sha256:a9030cd30caf848a13a192c5e45367e3c6f363726569a56e75dc1151ee26d859", + "sha256:a9e75e49a0f1583eee0ce93270232b8e7bb4b1edc89cc70b07600d525aef4f43", + "sha256:b50f45d0e82b4562f59f0e0ca511f65e412f2a97d790eea5f60e34e5f1aabc9a", + "sha256:b7878e59ec31f12d54b3797689402ee3b5cfcb5598f2ebf26491732758751908", + "sha256:ce1ddaadee913543ff0154021d31b134551f63428065168e756d90bdc4c686f5", + "sha256:ce2646e4c0807f3461be0653502bb48c6e91a5171d6e450367082c79e12868bf", + "sha256:ce6c3d18b2a8ce364013d47b9cad71db815df31d55918403f8db7d890c9d07ae", + "sha256:e4e2664232005bd306f878b0f167a31f944a07c4de0152c444f8c61bbe3cfb38", + "sha256:e8aa395482728de8bdcca9cc0faf3765ab483e81e01923aaa736b42f0294f570", + "sha256:eb4fcf7105bf071c71068c6eee47499ab8d4b8f5a11fc35147c934f0faa60f23", + "sha256:ed375a79f06cad285166e5be74745df1ed6845c5624aafadec4b7a29c25866ef", + "sha256:f35248f7e0d63b234a109dd72fbfb4b5cb6cb6840b221d0df0ecbf54ab087654", + "sha256:f502ef245c492b391e0e23e94cba030ab91722dcc56963c85bfd7f3441ea2bbe", + "sha256:fe01bac7226499aedf472c62fa3b85b2c619365f3f14dd222ffe4f3aa91e5f98" + ], + "version": "==1.3.17" + }, + "werkzeug": { + "hashes": [ + "sha256:2de2a5db0baeae7b2d2664949077c2ac63fbd16d98da0ff71837f7d1dea3fd43", + "sha256:6c80b1e5ad3665290ea39320b91e1be1e0d5f60652b964a3070216de83d2e47c" + ], + "version": "==1.0.1" + } + }, + "develop": {} +} diff --git a/module1-web-application-development-with-flask/hello.py b/module1-web-application-development-with-flask/hello.py new file mode 100644 index 00000000..c134d063 --- /dev/null +++ b/module1-web-application-development-with-flask/hello.py @@ -0,0 +1,14 @@ +# Simple local flask instance + +from flask import Flask + +app = Flask(__name__) + +@app.route("/") +def index(): + x = 2 + 2 + return f"Hello World! {x}" + +@app.route("/about") +def about(): + return "About me" \ No newline at end of file From 484a039e39697489262c95d0ddd1c16cdc3c16c7 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Wed, 3 Jun 2020 21:30:38 -0700 Subject: [PATCH 2/8] app factory --- .../hello.py | 2 ++ .../web_app/__init__.py | 14 ++++++++ .../web_app/routes/book_routes.py | 36 +++++++++++++++++++ .../web_app/routes/home_routes.py | 12 +++++++ 4 files changed, 64 insertions(+) create mode 100644 module1-web-application-development-with-flask/web_app/__init__.py create mode 100644 module1-web-application-development-with-flask/web_app/routes/book_routes.py create mode 100644 module1-web-application-development-with-flask/web_app/routes/home_routes.py diff --git a/module1-web-application-development-with-flask/hello.py b/module1-web-application-development-with-flask/hello.py index c134d063..d81cdd44 100644 --- a/module1-web-application-development-with-flask/hello.py +++ b/module1-web-application-development-with-flask/hello.py @@ -6,9 +6,11 @@ @app.route("/") def index(): + print("Welcome to this Home Page!") x = 2 + 2 return f"Hello World! {x}" @app.route("/about") def about(): + print("What about?") return "About me" \ No newline at end of file diff --git a/module1-web-application-development-with-flask/web_app/__init__.py b/module1-web-application-development-with-flask/web_app/__init__.py new file mode 100644 index 00000000..127998f5 --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/__init__.py @@ -0,0 +1,14 @@ +from flask import Flask + +from web_app.routes.home_routes import home_routes +from web_app.routes.book_routes import book_routes + +def create_app(): + app = Flask(__name__) + app.register_blueprint(home_routes) + app.register_blueprint(book_routes) + return app + +if __name__ == "__main__": + my_app = create_app() + my_app.run(debug=True) \ No newline at end of file diff --git a/module1-web-application-development-with-flask/web_app/routes/book_routes.py b/module1-web-application-development-with-flask/web_app/routes/book_routes.py new file mode 100644 index 00000000..2d9ab8ac --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/routes/book_routes.py @@ -0,0 +1,36 @@ +from flask import Blueprint, jsonify, request, render_template #, flash, redirect + +book_routes = Blueprint("book_routes", __name__) + +@book_routes.route("/books.json") +def list_books(): + books = [ + {"id": 1, "title": "Book 1"}, + {"id": 2, "title": "Book 2"}, + {"id": 3, "title": "Book 3"}, + ] + return jsonify(books) + +@book_routes.route("/books") +def list_books_for_humans(): + books = [ + {"id": 1, "title": "Book 1"}, + {"id": 2, "title": "Book 2"}, + {"id": 3, "title": "Book 3"}, + ] + return render_template("books.html", message="Here's some books", books=books) + +@book_routes.route("/books/new") +def new_book(): + return render_template("new_book.html") + +@book_routes.route("/books/create", methods=["POST"]) +def create_book(): + print("FORM DATA:", dict(request.form)) + # todo: store in database + return jsonify({ + "message": "BOOK CREATED OK (TODO)", + "book": dict(request.form) + }) + #flash(f"Book '{new_book.title}' created successfully!", "success") + #return redirect(f"/books") \ No newline at end of file diff --git a/module1-web-application-development-with-flask/web_app/routes/home_routes.py b/module1-web-application-development-with-flask/web_app/routes/home_routes.py new file mode 100644 index 00000000..60ca15b9 --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/routes/home_routes.py @@ -0,0 +1,12 @@ +from flask import Blueprint + +home_routes = Blueprint("home_routes", __name__) + +@home_routes.route("/") +def index(): + x = 2 + 2 + return f"Hello World! {x}" + +@home_routes.route("/about") +def about(): + return "About me" \ No newline at end of file From 295447a6dffad8314e0caf7a1ab28a627afd0281 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Wed, 3 Jun 2020 21:51:31 -0700 Subject: [PATCH 3/8] changed app --- .../hello.py | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 module1-web-application-development-with-flask/hello.py diff --git a/module1-web-application-development-with-flask/hello.py b/module1-web-application-development-with-flask/hello.py deleted file mode 100644 index d81cdd44..00000000 --- a/module1-web-application-development-with-flask/hello.py +++ /dev/null @@ -1,16 +0,0 @@ -# Simple local flask instance - -from flask import Flask - -app = Flask(__name__) - -@app.route("/") -def index(): - print("Welcome to this Home Page!") - x = 2 + 2 - return f"Hello World! {x}" - -@app.route("/about") -def about(): - print("What about?") - return "About me" \ No newline at end of file From c41148d6e34396fc68041bc61d529503247f1374 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Sun, 7 Jun 2020 16:05:30 -0700 Subject: [PATCH 4/8] added forms --- .../.vscode/settings.json | 3 ++ .../web_app/templates/books.html | 28 +++++++++++++++++ .../web_app/templates/new_book.html | 30 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 module1-web-application-development-with-flask/.vscode/settings.json create mode 100644 module1-web-application-development-with-flask/web_app/templates/books.html create mode 100644 module1-web-application-development-with-flask/web_app/templates/new_book.html diff --git a/module1-web-application-development-with-flask/.vscode/settings.json b/module1-web-application-development-with-flask/.vscode/settings.json new file mode 100644 index 00000000..8d805d9e --- /dev/null +++ b/module1-web-application-development-with-flask/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\Ravi\\.virtualenvs\\DS-Unit-3-Sprint-3-Productization-and-Clou-pxICk7w4\\Scripts\\python.exe" +} \ No newline at end of file diff --git a/module1-web-application-development-with-flask/web_app/templates/books.html b/module1-web-application-development-with-flask/web_app/templates/books.html new file mode 100644 index 00000000..2c710665 --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/templates/books.html @@ -0,0 +1,28 @@ + + + + + + Document + + + + Books Page + +

Welcome to the Books Page

+ +

{{ message }}

+ + {% if books %} +
    + {% for book in books %} +
  • {{ book["title"] }}
  • + {% endfor %} +
+ + {% else %} +

Books not found.

+ {% endif %} + + + \ No newline at end of file diff --git a/module1-web-application-development-with-flask/web_app/templates/new_book.html b/module1-web-application-development-with-flask/web_app/templates/new_book.html new file mode 100644 index 00000000..da534d1d --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/templates/new_book.html @@ -0,0 +1,30 @@ + + + + + + Document + + + +

New Book Page

+ +

Please fill out the form and submit to create a new book!

+ +
+ + + + + + + + +
+ + + \ No newline at end of file From aa99371c3ca0054c2f01af4170ef109bdff28468 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Sun, 7 Jun 2020 16:51:55 -0700 Subject: [PATCH 5/8] setup dev db --- .../.vscode/settings.json | 2 +- .../migrations/README | 1 + .../migrations/alembic.ini | 45 +++++++++ .../migrations/env.py | 96 +++++++++++++++++++ .../migrations/script.py.mako | 24 +++++ .../migrations/versions/fa25680ea297_.py | 33 +++++++ .../web_app/.gitignore | 4 + .../web_app/__init__.py | 11 +++ .../web_app/models.py | 37 +++++++ 9 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 module1-web-application-development-with-flask/migrations/README create mode 100644 module1-web-application-development-with-flask/migrations/alembic.ini create mode 100644 module1-web-application-development-with-flask/migrations/env.py create mode 100644 module1-web-application-development-with-flask/migrations/script.py.mako create mode 100644 module1-web-application-development-with-flask/migrations/versions/fa25680ea297_.py create mode 100644 module1-web-application-development-with-flask/web_app/.gitignore create mode 100644 module1-web-application-development-with-flask/web_app/models.py diff --git a/module1-web-application-development-with-flask/.vscode/settings.json b/module1-web-application-development-with-flask/.vscode/settings.json index 8d805d9e..3f597dff 100644 --- a/module1-web-application-development-with-flask/.vscode/settings.json +++ b/module1-web-application-development-with-flask/.vscode/settings.json @@ -1,3 +1,3 @@ { - "python.pythonPath": "C:\\Users\\Ravi\\.virtualenvs\\DS-Unit-3-Sprint-3-Productization-and-Clou-pxICk7w4\\Scripts\\python.exe" + "python.pythonPath": "C:\\Users\\Ravi\\.virtualenvs\\module1-web-application-development-with-f-oF58SWF2\\Scripts\\python.exe" } \ No newline at end of file diff --git a/module1-web-application-development-with-flask/migrations/README b/module1-web-application-development-with-flask/migrations/README new file mode 100644 index 00000000..98e4f9c4 --- /dev/null +++ b/module1-web-application-development-with-flask/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/module1-web-application-development-with-flask/migrations/alembic.ini b/module1-web-application-development-with-flask/migrations/alembic.ini new file mode 100644 index 00000000..f8ed4801 --- /dev/null +++ b/module1-web-application-development-with-flask/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/module1-web-application-development-with-flask/migrations/env.py b/module1-web-application-development-with-flask/migrations/env.py new file mode 100644 index 00000000..94521792 --- /dev/null +++ b/module1-web-application-development-with-flask/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +from flask import current_app +config.set_main_option( + 'sqlalchemy.url', + str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/module1-web-application-development-with-flask/migrations/script.py.mako b/module1-web-application-development-with-flask/migrations/script.py.mako new file mode 100644 index 00000000..2c015630 --- /dev/null +++ b/module1-web-application-development-with-flask/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/module1-web-application-development-with-flask/migrations/versions/fa25680ea297_.py b/module1-web-application-development-with-flask/migrations/versions/fa25680ea297_.py new file mode 100644 index 00000000..267da753 --- /dev/null +++ b/module1-web-application-development-with-flask/migrations/versions/fa25680ea297_.py @@ -0,0 +1,33 @@ +"""empty message + +Revision ID: fa25680ea297 +Revises: +Create Date: 2020-06-07 16:45:29.271216 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'fa25680ea297' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('book', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('title', sa.String(length=128), nullable=True), + sa.Column('author_id', sa.String(length=128), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('book') + # ### end Alembic commands ### diff --git a/module1-web-application-development-with-flask/web_app/.gitignore b/module1-web-application-development-with-flask/web_app/.gitignore new file mode 100644 index 00000000..48f60ff1 --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/.gitignore @@ -0,0 +1,4 @@ +## Dev related +migrations +*.db +.vscode diff --git a/module1-web-application-development-with-flask/web_app/__init__.py b/module1-web-application-development-with-flask/web_app/__init__.py index 127998f5..8769420e 100644 --- a/module1-web-application-development-with-flask/web_app/__init__.py +++ b/module1-web-application-development-with-flask/web_app/__init__.py @@ -1,12 +1,23 @@ from flask import Flask +from web_app.models import db, migrate from web_app.routes.home_routes import home_routes from web_app.routes.book_routes import book_routes +DATABASE_URI = "sqlite:///books_repo.db" # using relative filepath +#DATABASE_URI = "sqlite:////Users/Username/Desktop/your-repo-name/web_app_99.db" # using absolute filepath on Mac (recommended) +#DATABASE_URI = "sqlite:///C:\\Users\\Username\\Desktop\\your-repo-name\\web_app_99.db" # using absolute filepath on Windows (recommended) h/t: https://stackoverflow.com/a/19262231/670433 + def create_app(): app = Flask(__name__) + + app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI + db.init_app(app) + migrate.init_app(app, db) + app.register_blueprint(home_routes) app.register_blueprint(book_routes) + return app if __name__ == "__main__": diff --git a/module1-web-application-development-with-flask/web_app/models.py b/module1-web-application-development-with-flask/web_app/models.py new file mode 100644 index 00000000..8b414424 --- /dev/null +++ b/module1-web-application-development-with-flask/web_app/models.py @@ -0,0 +1,37 @@ +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate + +db = SQLAlchemy() + +migrate = Migrate() + +class Book(db.Model): + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String(128)) + author_id = db.Column(db.String(128)) + + def __repr__(self): + return f"" + +def parse_records(database_records): + """ + A helper method for converting a list of database record objects into a list of dictionaries, so they can be returned as JSON + + Param: database_records (a list of db.Model instances) + + Example: parse_records(User.query.all()) + + Returns: a list of dictionaries, each corresponding to a record, like... + [ + {"id": 1, "title": "Book 1"}, + {"id": 2, "title": "Book 2"}, + {"id": 3, "title": "Book 3"}, + ] + """ + parsed_records = [] + for record in database_records: + print(record) + parsed_record = record.__dict__ + del parsed_record["_sa_instance_state"] + parsed_records.append(parsed_record) + return parsed_records From 3f8513315b512752b1a56e2c0587bdb860d6afe4 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Sun, 7 Jun 2020 17:00:06 -0700 Subject: [PATCH 6/8] complete --- .../web_app/routes/book_routes.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/module1-web-application-development-with-flask/web_app/routes/book_routes.py b/module1-web-application-development-with-flask/web_app/routes/book_routes.py index 2d9ab8ac..fc207d20 100644 --- a/module1-web-application-development-with-flask/web_app/routes/book_routes.py +++ b/module1-web-application-development-with-flask/web_app/routes/book_routes.py @@ -1,4 +1,5 @@ from flask import Blueprint, jsonify, request, render_template #, flash, redirect +from web_app.models import db, Book book_routes = Blueprint("book_routes", __name__) @@ -27,9 +28,13 @@ def new_book(): @book_routes.route("/books/create", methods=["POST"]) def create_book(): print("FORM DATA:", dict(request.form)) - # todo: store in database + + new_book = Book(title=request.form["book_title"], author_id=request.form["author_name"]) + db.session.add(new_book) + db.session.commit() + return jsonify({ - "message": "BOOK CREATED OK (TODO)", + "message": "BOOK CREATED OK", "book": dict(request.form) }) #flash(f"Book '{new_book.title}' created successfully!", "success") From 3b834097fdec6d87a0b25908a5889b7fcd22e33f Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Sun, 7 Jun 2020 17:16:01 -0700 Subject: [PATCH 7/8] completed --- .../web_app/routes/book_routes.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/module1-web-application-development-with-flask/web_app/routes/book_routes.py b/module1-web-application-development-with-flask/web_app/routes/book_routes.py index fc207d20..2b616556 100644 --- a/module1-web-application-development-with-flask/web_app/routes/book_routes.py +++ b/module1-web-application-development-with-flask/web_app/routes/book_routes.py @@ -1,24 +1,30 @@ from flask import Blueprint, jsonify, request, render_template #, flash, redirect -from web_app.models import db, Book +from web_app.models import db, Book, parse_records book_routes = Blueprint("book_routes", __name__) @book_routes.route("/books.json") def list_books(): - books = [ - {"id": 1, "title": "Book 1"}, - {"id": 2, "title": "Book 2"}, - {"id": 3, "title": "Book 3"}, - ] + book_records = Book.query.all() + print(book_records) + books = parse_records(book_records) + +# books = [ +# {"id": 1, "title": "Book 1"}, +# {"id": 2, "title": "Book 2"}, +# {"id": 3, "title": "Book 3"},] + return jsonify(books) @book_routes.route("/books") def list_books_for_humans(): books = [ - {"id": 1, "title": "Book 1"}, - {"id": 2, "title": "Book 2"}, - {"id": 3, "title": "Book 3"}, - ] +# {"id": 1, "title": "Book 1"}, +# {"id": 2, "title": "Book 2"}, +# {"id": 3, "title": "Book 3"},] + book_records = Book.query.all() + print(book_records) + books = parse_records(book_records) return render_template("books.html", message="Here's some books", books=books) @book_routes.route("/books/new") From 91d387f82fe9f7a37416e944b650bfa19b005ac7 Mon Sep 17 00:00:00 2001 From: RAV10K1 Date: Tue, 9 Jun 2020 20:05:06 -0700 Subject: [PATCH 8/8] fixed books.query function --- .../web_app/routes/book_routes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module1-web-application-development-with-flask/web_app/routes/book_routes.py b/module1-web-application-development-with-flask/web_app/routes/book_routes.py index 2b616556..53633be2 100644 --- a/module1-web-application-development-with-flask/web_app/routes/book_routes.py +++ b/module1-web-application-development-with-flask/web_app/routes/book_routes.py @@ -21,7 +21,8 @@ def list_books_for_humans(): books = [ # {"id": 1, "title": "Book 1"}, # {"id": 2, "title": "Book 2"}, -# {"id": 3, "title": "Book 3"},] +# {"id": 3, "title": "Book 3"} + ] book_records = Book.query.all() print(book_records) books = parse_records(book_records)