Skip to content

refactored completed SQLAlchemy pt I #114

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

Open
wants to merge 2 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
75 changes: 75 additions & 0 deletions Unit-01/06-sql-alchemy-1/app.py
Original file line number Diff line number Diff line change
@@ -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/<int:id>', 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/<int:id>/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)
13 changes: 13 additions & 0 deletions Unit-01/06-sql-alchemy-1/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Snacks CRUD App</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
15 changes: 15 additions & 0 deletions Unit-01/06-sql-alchemy-1/templates/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block content %}

<form action="{{url_for('show', id=snack.id)}}?_method=PATCH" method="POST">
<input type="text" name="name" value="{{snack.name}}">
<input type="text" name="kind" value="{{snack.kind}}">
<input type="submit" value="Edit yo snack!">
</form>

<form action="{{url_for('show', id=snack.id)}}?_method=DELETE" method="POST">
<input type="submit" value="DelEATe yo' snack">

</form>

{% endblock %}
16 changes: 16 additions & 0 deletions Unit-01/06-sql-alchemy-1/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block content %}

<h1>Snacks App</h1>

<a href="{{url_for('new')}}"> Add a new snack!</a>

<ul>
{% for snack in snacks %}
<li>
<a href="{{url_for('show', id=snack.id)}}">{{ snack.name }} {{snack.kind}}</a>
</li>
{% endfor %}
</ul>

{% endblock %}
15 changes: 15 additions & 0 deletions Unit-01/06-sql-alchemy-1/templates/new.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html' %}
{% block content %}

<h1>Add Yo' Snack!</h1>

<form action="{{url_for('index')}}" method="POST">
<label for="name">Snack Name:</label>
<input type="text" name="name">
<label for="name">Kind of Snack:</label>
<input type="text" name="kind">
<input type="submit" value="Add yo' snack">

</form>

{% endblock %}
13 changes: 13 additions & 0 deletions Unit-01/06-sql-alchemy-1/templates/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}
{% block content %}

<h1>More about this snack!</h1>
<p>
The name of the snack is {{snack.name}} - it's a tastey one!
</p>

<a href="{{url_for('index')}}">See all the snacks</a>
<br>
<a href="{{url_for('edit', id=snack.id)}}">Edit this snack</a>

{% endblock %}