Skip to content
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
78 changes: 76 additions & 2 deletions Unit-01/04-flask-crud/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
from flask import Flask
from flask import Flask, render_template, url_for, redirect, request
from flask_modus import Modus
from flask_sqlalchemy import SQLAlchemy

app=Flask(__name__)
modus=Modus(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://localhost/snacks-db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

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

def __repr__(self):
return "Name: {self.name}; Kind: {self.kind}"



@app.route("/snacks", methods=["GET", "POST"])
def index():
if request.method == "POST":
name = request.form.get('name')
kind = request.form.get('kind')
db.session.add(Snack(name, kind))
db.session.commit()
return redirect(url_for('index'))
return render_template("index.html", snacks=Snack.query.order_by(Snack.id).all())

@app.route("/snacks/new")
def new():
return render_template("new.html")

@app.route("/snacks/<int:id>", methods=["GET", "PATCH", "DELETE"])
def show(id):
try:
individual_snack = Snack.query.get(id)

if request.method == b"PATCH":
individual_snack.name = (request.form['name'])
individual_snack.kind = (request.form['kind'])
db.session.add(individual_snack)
db.session.commit()

elif request.method == b"DELETE":
db.session.delete(individual_snack)
db.session.commit()
return redirect(url_for('index'))

return render_template('show.html', snack = individual_snack)
except:
return redirect(url_for('not_found', id=id))

@app.route("/snacks/<int:id>/edit")
def edit(id):
try:
individual_snack = Snack.query.get(id)
return render_template('edit.html', snack=individual_snack)
except:
return redirect(url_for('not_found', id=id))



@app.route("/snacks/<int:id>/not-found")
def not_found(id):
return render_template('404.html')

if __name__ == "__main__":
app.run(debug=True)

snack_list = []
24 changes: 23 additions & 1 deletion Unit-01/04-flask-crud/snack.py
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# Add a class for a snack here!
# Add a class for a snack here!
class Snack():
id = 1
def __init__(self, name, kind):
self._name = name
self._kind = kind
self.id = Snack.id
Snack.id += 1
def __repr__(self):
return "Name: {self.name}; Kind: {self.kind}"
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value

@property
def kind(self):
return self._kind
@kind.setter
def kind(self, value):
self._kind = value
7 changes: 7 additions & 0 deletions Unit-01/04-flask-crud/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends"base.html" %}
{% block content %}
<div class="jumbotron d-flex flex-column align-items-center">
<h1 class="text-danger">Page not found</h1>
<p class="muted">This is not the snack you are looking for</p>
</div>
{% endblock %}
19 changes: 19 additions & 0 deletions Unit-01/04-flask-crud/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask CRUD</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

</head>
<body>
<nav class="navbar navbar-dark bg-dark">
<a class="navbar-brand text-warning">Ultimate Snack</a>
<a class="nav-link" href="/snacks">Home</a>
<a class="nav-link" href="/snacks/new">New Snack</a>
</nav>
{% block content %}
{% endblock %}
<script src="https://use.fontawesome.com/6b91cd47e2.js"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions Unit-01/04-flask-crud/templates/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends"base.html" %}
{% block content %}
<div class="jumbotron d-flex flex-column align-items-center">
<form action="{{url_for('show', id=snack.id)}}?_method=PATCH" method="POST">
<div>
<label for="name">Name</label>
<input type="text" name="name" value="{{snack.name}}" required>
</div>
<div>
<label for="kind">Kind</label>
<input type="text" name="kind" value="{{snack.kind}}" required>
</div>
<input type="submit" value="add changes" class="btn btn-light">
</form>
</div>

{% endblock %}
12 changes: 12 additions & 0 deletions Unit-01/04-flask-crud/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends"base.html" %}
{% block content %}
<div class= "d-flex flex-column align-items-center">
<h1>Snacks</h1>
<ul class="list-group">
{% for snack in snacks %}
<li class="list-group item d-flex justify-content-between"><span><a href = "{{url_for('show', id=snack.id)}}"<i class="fa fa-cog text-primary" aria-hidden="true"></i></a><strong class="px-2">{{snack.name}}</strong> <small class="px-2">{{snack.kind}}</small></span></li>
{% endfor %}
</ul>
</div>

{% endblock %}
17 changes: 17 additions & 0 deletions Unit-01/04-flask-crud/templates/new.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends"base.html" %}
{% block content %}
<div class="jumbotron d-flex flex-column align-items-center">
<form action="{{url_for('index')}}" method="POST">
<div>
<label for="name">Name</label>
<input type="text" name="name" required>
</div>
<div>
<label for="kind">Kind</label>
<input type="text" name="kind" required>
</div>
<input type="submit" value="Create Snack" class="btn btn-light">
</form>
</div>

{% endblock %}
18 changes: 18 additions & 0 deletions Unit-01/04-flask-crud/templates/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends"base.html" %}
{% block content %}
<div class="d-flex flex-column align-items-center">
<div class="card m-5" style="width: 20rem">
<div class="card-content p-3">
<h3 class="card-title text-uppercase">{{snack.name}}</h1>
<h5 class="card-subtitle mb-2 text-muted">snack</h5>
<p>name: <span class="font-italic">{{snack.name}}</span></p>
<p>kind: <span class="font-italic">{{snack.kind}}</span></p>
<div class="d-flex">
<button class="btn btn-light"><a href="{{url_for('edit', id=snack.id)}}" class="card-link"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a></button>
<form action="{{url_for('show', id=snack.id)}}?_method=DELETE" method="POST"><button class="btn btn-light ml-3" type="submit"><i class="fa fa-trash-o text-primary" aria-hidden="true"></i></button></form>
</div>
</div>
</div>
</div>

{% endblock %}
79 changes: 79 additions & 0 deletions Unit-01/06-sql-alchemy-1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from flask import Flask, render_template, url_for, redirect, request
from flask_modus import Modus
from flask_sqlalchemy import SQLAlchemy

app=Flask(__name__)
modus=Modus(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://localhost/snacks-db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Snack(db.Model):
__tablename__="snacks"

id= db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
kind = db.Column(db.Text)
rating = db.Column(db.Text)

def __init__(self, name, kind, rating):
self.name = name
self.kind = kind
self.rating = rating

def __repr__(self):
return "Name: {self.name}; Kind: {self.kind}; Rating: {self.rating}"



@app.route("/snacks", methods=["GET", "POST"])
def index():
if request.method == "POST":
name = request.form.get('name')
kind = request.form.get('kind')
db.session.add(Snack(name, kind))
db.session.commit()
return redirect(url_for('index'))
return render_template("index.html", snacks=Snack.query.order_by(Snack.id).all())

@app.route("/snacks/new")
def new():
return render_template("new.html")

@app.route("/snacks/<int:id>", methods=["GET", "PATCH", "DELETE"])
def show(id):
try:
individual_snack = Snack.query.get(id)

if request.method == b"PATCH":
individual_snack.name = (request.form['name'])
individual_snack.kind = (request.form['kind'])
db.session.add(individual_snack)
db.session.commit()

elif request.method == b"DELETE":
db.session.delete(individual_snack)
db.session.commit()
return redirect(url_for('index'))

return render_template('show.html', snack = individual_snack)
except:
return redirect(url_for('not_found', id=id))

@app.route("/snacks/<int:id>/edit")
def edit(id):
try:
individual_snack = Snack.query.get(id)
return render_template('edit.html', snack=individual_snack)
except:
return redirect(url_for('not_found', id=id))



@app.route("/snacks/<int:id>/not-found")
def not_found(id):
return render_template('404.html')

if __name__ == "__main__":
app.run(debug=True)

14 changes: 14 additions & 0 deletions Unit-01/06-sql-alchemy-1/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from app import app,db

from flask_script import Manager

from flask_migrate import Migrate, MigrateCommand

migrate = Migrate(app, db)

manager = Manager(app)

manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
manager.run()
1 change: 1 addition & 0 deletions Unit-01/06-sql-alchemy-1/migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
45 changes: 45 additions & 0 deletions Unit-01/06-sql-alchemy-1/migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -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
Loading