Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module retro #178

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.6"
3 changes: 3 additions & 0 deletions module1-web-application-development-with-flask/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
migrations/

test_db.db
14 changes: 14 additions & 0 deletions module1-web-application-development-with-flask/Pipfile
Original file line number Diff line number Diff line change
@@ -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.6"
179 changes: 179 additions & 0 deletions module1-web-application-development-with-flask/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions module1-web-application-development-with-flask/web_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask import Flask

from web_app.routes.home_routes import home_routes
from web_app.routes.book_routes import book_routes
from web_app.routes.user_routes import user_routes
from web_app.models import db, migrate


DATABASE_URI = "sqlite:///test_db.db"


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)
app.register_blueprint(user_routes)
return app


if __name__ == "__main__":
my_app = create_app()
my_app.run(debug=True)
27 changes: 27 additions & 0 deletions module1-web-application-development-with-flask/web_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


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))


class User(db.Model):
# __table_name__ = "user"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(128))


class Tweet(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
text = db.Column(db.String(500))
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

from flask import Blueprint, jsonify, request, render_template, redirect, flash
from pdb import set_trace as st
from web_app.models import Book, db

book_routes = Blueprint("book_routes", __name__)


@book_routes.route("/books.json")
def list_books_json():
book_records = Book.query.all()
return jsonify(book_records)


@book_routes.route("/books")
def list_books():
book_records = Book.query.all()
return render_template("books.html", message="Here's some books", books=book_records)


@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))
new_book = Book(title=request.form["book_title"], author_id=request.form["author_name"])

db.session.add(new_book)
db.session.commit()
return redirect(f"/books")
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

from flask import Blueprint, jsonify, request, render_template, redirect, flash
from pdb import set_trace as st
from web_app.models import Tweet, User, db
from sqlalchemy.orm import joinedload


user_routes = Blueprint("user_routes", __name__)


@user_routes.route("/users/")
def list_users():
user_records = User.query.all()
return render_template("users.html", users=user_records)


@user_routes.route("/users/<username>/")
def list_user_tweets(username=None):
user = User.query.filter_by(username=username).first()
user_id = user.id
user_tweets = Tweet.query.filter_by(user_id=user_id).all()
# st()
return render_template("user_tweets.html", username=username, user_tweets=user_tweets)


@user_routes.route("/users/<username>/new_tweet", methods=["POST"])
def user_create_new_tweet(username=None):
user = User.query.filter_by(username=username).first()
# st()
user_id = user.id
new_tweet = Tweet(user_id=user_id, text=request.form["tweet_text"])
db.session.add(new_tweet)
db.session.commit()
return redirect(f"/users/{username}/")


@user_routes.route("/users/new/")
def new_user():
return render_template("new_user.html")


@user_routes.route("/users/create", methods=["POST"])
def create_user():
new_user = User(username=request.form["username"])
print("FORM DATA:", dict(request.form))
db.session.add(new_user)
db.session.commit()
# flash("asdf")
return redirect("/users")
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

{% if books %}
<ul>
{% for book in books %}
<p>
<li>{{ book.title }}</li>
</p>
{% endfor %}
</ul>
{% else %}
<p>no books!</p>
{% endif %}


</body>
</html>
Loading