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

DSPT4 Unit 3 Sprint 3 Jessica Kimbril #176

Open
wants to merge 5 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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,16 @@ venv.bak/

# mypy
.mypy_cache/

# vscode
.vscode

# gitignore
.gitignore

# pipfile
Pipfile
Pipfile.lock

# database stuff
migrations
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 @@
# The init file indicates that the folder lives inside is like a python module
# and facilitates the importing of certain files within that module within that directory.
# The other role that it plays, is it's the entry point. It's the first place to look into that module directory

# Imports
from flask import Flask
from web_app.models import db, migrate
from web_app.routes.tweets_routes import tweets_routes
from web_app.routes.home_routes import home_routes

DATABASE_URI = "sqlite:///C:\\Users\\jessi\\OneDrive\\Documents\\School\\Python\\Unit_3\\Sprint_3\\DS-Unit-3-Sprint-3-Productization-and-Cloud\\twitoff_development.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(tweets_routes)
app.register_blueprint(home_routes)

return app

if __name__ == "__main__":
my_app = create_app()
my_app.run(debug=True)
13 changes: 13 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,13 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

db = SQLAlchemy()

migrate = Migrate()

class Tweets(db.Model):
id = db.Column(db.Integer, primary_key=True)
tweet = db.Column(db.String(128))
user_id = db.Column(db.String(128))

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# web_app/routes/book_routes.py

from flask import Blueprint, jsonify, request, render_template #, flash, redirect

home_routes = Blueprint("home_routes", __name__)

@home_routes.route("/")
def hello():
return "Hello everyone and welcome to Twitoff!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# web_app/routes/book_routes.py

from flask import Blueprint, jsonify, request, render_template, flash, redirect
from web_app.models import db, Tweets

tweets_routes = Blueprint("tweets_routes", __name__)

@tweets_routes.route("/tweets.json")
def list_tweets(): # my love for the office is vast lmao
tweets = [
{"id": 1, "Dwight Shrute": "Identiity theft is not a joke JIM."},
{"id": 2, "Jim Halpert": "Bears. Beets. Battlestar Galactica."},
{"id": 3, "Michael Scott": "DWIGHT. YOU IGNORANT SLUT"},
]
return jsonify(tweets)

@tweets_routes.route("/tweets/new")
def new_tweet_form():
return render_template("new_tweet.html")

@tweets_routes.route("/tweets/create", methods="POST")
def create_tweet():
print("FORM DATA:", dict(request.form)) #> {"book_title": "___", "author_name": "____"}

new_tweet = Tweets(tweet=request.form["tweets"], author_id=request.form["author_id"])
db.session.add(new_tweet)
db.session.commit()

#return jsonify({
# "message": "TWEET CREATE OK",
# "book": dict(request.form)
#})
flash(f"Tweet '{new_tweet.title}' created successfully!", "dark")
return redirect("/tweets")
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!doctype html>
<html>
<head>
{% block tweet %}
<title>My Starter Web App | Helps students learn how to use the Flask Python package.</title>
{% endblock %}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>

<body>

<!-- FLASH MESSAGING -->
<!-- see: http://flask.pocoo.org/docs/1.0/patterns/flashing/#flashing-with-categories -->
<!-- see: https://getbootstrap.com/docs/4.3/components/alerts/ -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }} alert-dismissible" role="alert" style="margin-bottom:0px;">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}

<!-- SITE NAVIGATION -->
<!-- see: https://jinja.palletsprojects.com/en/2.11.x/tricks/ -->
<!-- see: https://getbootstrap.com/docs/4.0/components/navbar/ -->
{% set nav_links = [
('/', 'prediction_form', 'Make a Prediction'),
('/tweets', 'tweets', 'tweets'),
('/tweets/new', 'new_tweet', 'New Tweet')
] -%}
{% set active_page = active_page|default('index') -%}
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand" href="/">My Web App</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<ul class="nav navbar-nav ml-auto">
{% for href, id, link_text in nav_links %}
{% if id == active_page %}
{% set is_active = "active" -%}
{% else %}
{% set is_active = "" -%}
{% endif %}
<li class="nav-item">
<a class="nav-link {{ is_active }}" href="{{href}}">{{link_text}}</a>
</li>
{% endfor %}
</div>
</ul>
</nav>


<div class="container" style="margin-top:2em;">
<!-- PAGE CONTENTS -->
<div id="content">
{% block content %}
{% endblock %}
</div>

<!-- FOOTER -->
<div id="footer">
<hr>
&copy; Copyright 2020 Jessica Kimbril |
<a href="https://github.com/jessicakimbril/">source</a>
</div>
</div>

<!-- JAVASCRIPT SECTION -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script type="text/javascript">

console.log("Thanks for the page visit!")

// closes data-dismiss="alert" flash messages
// see: https://getbootstrap.com/docs/4.3/components/alerts/#javascript-behavior
//$().alert('close')

</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!-- web_app/templates/layout.html -->

<!doctype html>
<html>
<head>
<title>My Starter Web App | Helps students learn how to use the Flask Python package.</title>
</head>

<body>

<!-- SITE NAVIGATION -->
<div class="container">
<div id="nav">
<h1><a href="/">My Web App</a></h1>
<ul>
<li><a href="/tweets">Tweets Page</a></li>
<li><a href="/tweets/new">New tweets Form</a></li>
</ul>
</div>
<hr>

<!-- PAGE CONTENTS -->
<div id="content">
{% block content %}
{% endblock %}
</div>

<!-- FOOTER -->
<div id="footer">
<hr>
&copy; Copyright 2020 Jessica Kimbril |
<a href="https://github.com/jessicakimbril">source</a>
</div>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- web_app/templates/new_book.html -->

{% extends "layout.html" %}

{% block content %}

<h1>New Tweet Page</h1>

<p>Please fill out the form and submit to create a new tweet!</p>

<form action="/tweets/create" method="POST">

<label>Title:</label>
<input type="text" name="title" placeholder="Tweet XYZ" value="Tweet XYZ">

<label>User:</label>
<select name="user_name">
<option value="A1">User 1</option>
<option value="A2">User 2</option>
<option value="A3">User 3</option>
</select>

<button>Submit</button>
</form>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- web_app/templates/tweets.html -->

{% extends "bootstrap_layout.html" %}
{% set active_page = "tweets" %}

{% block content %}

<h2>Welcome to the Tweets Page</h2>

<p>{{ message }}</p>

{% if tweet %}
<ul>
{% for tweet in tweets %}
<li>{{ tweet.title }}</li>
{% endfor %}
</ul>

{% else %}
<p>tweet not found.</p>
{% endif %}

{% endblock %}
Binary file added twitoff_development.db
Binary file not shown.