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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
__pycache__
Binary file added flask/orm-tutorial/models-tutorial/app.db
Binary file not shown.
8 changes: 8 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

from app import views, models
9 changes: 9 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask.ext.wtf import Form
from wtforms import StringField, IntegerField
from flask_wtf.html5 import EmailField
from wtforms.validators import DataRequired

class CustomerForm(Form):
company = StringField('company', validators=[DataRequired()])
email = EmailField('email', validators=[DataRequired()])
# Add additional Address fields here
15 changes: 15 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from app import db


class Customer(db.Model):
id = db.Column(db.Integer, primary_key=True)
company = db.Column(db.String(120), unique=False)
email = db.Column(db.String(120))
# You need to a relationship to Address table here
# see http://flask-sqlalchemy.pocoo.org/2.1/models/#one-to-many-relationships

def __repr__(self):
return '<Customer %r>' % self.email

# Your Address code should go here
# class Address(db.Model):
14 changes: 14 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
{% if title %}
<title>{{ title }} - ACME</title>
{% else %}
<title>Welcome to ACME Aircraft Parts</title>
{% endif %}
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css">
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
30 changes: 30 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/templates/customer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<div class= "section">
<a href="{{ url_for('display_customer') }}">Home</a>
</div>
<h2>Add Customer to Our Database</h2>
<form action="" method="post" name="login">
{{ form.hidden_tag() }}
<div class="divider"></div>
<div class="section">
<p>
Company name:<br>
{{ form.company(size=120) }}<br>
</p>
<p>
Customer email:<br>
{{ form.email(size=120) }}<br>
</p>
</div>
<div class="divider"></div>
<div class="section">
<!-- Additional form fields for the Address should go here -->
</div>
<p><input type="submit" value="Create Customer"></p>
</form>
</div>
</div>
{% endblock %}
31 changes: 31 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<h1>Welcome to ACME Aircraft Parts</h1>
<div class= "section">
<a href="{{ url_for('create_customer') }}">Add a Customer</a>
</div>
<div class="divider"></div>
<div class="section">
<h3>These are all of our awesome customers:</h3>
<table class="striped">
<thead>
<tr>
<th data-field="company">Company</th>
<th data-field="email">Email</th>
</tr>
</thead>
{% for customer in customers %}
<!-- {% for address in customer.addresses %}
{% endfor %} -->
<tr>
<td>{{ customer.company }}</td>
<td>{{ customer.email }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}
27 changes: 27 additions & 0 deletions flask/orm-tutorial/models-tutorial/app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask import render_template, redirect, request
from app import app, models, db
from .forms import CustomerForm


@app.route('/')
def index():
return redirect('/create_customer')

@app.route('/create_customer', methods=['GET', 'POST'])
def create_customer():
form = CustomerForm()
if form.validate_on_submit():
customer = models.Customer(
company = form.company.data,
email = form.email.data)
# you will need to add Address here
db.session.add(customer)
db.session.commit()
return redirect('/customers')
return render_template('customer.html', form=form)

@app.route('/customers')
def display_customer():
customers = models.Customer.query.all()
return render_template('home.html',
customers=customers)
8 changes: 8 additions & 0 deletions flask/orm-tutorial/models-tutorial/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')

WTF_CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'
21 changes: 21 additions & 0 deletions flask/orm-tutorial/models-tutorial/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Models Lab/Homework
The goal for this lab is to get you comfortable translating your database models into actual code. We will be using [ORMs](https://en.wikipedia.org/wiki/Object-relational_mapping) to help us. The particular ORM that we will be using is SQLAlchemy (the flask implementation is called [Flask-SQLAlchemy](http://flask-sqlalchemy.pocoo.org/2.1/)).

We'll walk through the example together in class which will give you an idea of how this all works together. Your goal for this lab and homework is to create the remaining models, forms, and routes to complete v2 of the exercise from last week.

This means that you must:
* Create the following tables and the appropriate relationships:
* customer
* address
* order
* Remember to run update_database.py whenever you make changes to any models.
* This will update the database with your changes. However, **it will delete any data in the database**.
* Create the forms, templates, routes, etc necessary to input this data into the database.
* Create a view to showcase the data into your database (see the current implementation of "home.html" for example)

The following documentation will help answer any questions you may have.

## Helpful Documentation
- [Flask-SQLAlchemy](http://flask-sqlalchemy.pocoo.org/2.1/)
- [Accessing SQLite3 Command Shell](https://www.sqlite.org/cli.html)
- [Flask-WTF](https://flask-wtf.readthedocs.org/en/latest/) (flask plugin for creating forms easily)
10 changes: 10 additions & 0 deletions flask/orm-tutorial/models-tutorial/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Flask==0.10.1
Flask-SQLAlchemy==2.1
Flask-WTF==0.12
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.12
Werkzeug==0.11.4
wheel==0.29.0
WTForms==2.1
2 changes: 2 additions & 0 deletions flask/orm-tutorial/models-tutorial/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from app import app
app.run(debug=True, host="0.0.0.0", port=8081)
3 changes: 3 additions & 0 deletions flask/orm-tutorial/models-tutorial/update_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from app import db
db.drop_all()
db.create_all()
27 changes: 27 additions & 0 deletions javascript/hw5-skeleton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Event hander for calling the SoundCloud API using the user's search query
function callAPI(query) {
$.get("https://api.soundcloud.com/tracks?client_id=b3179c0738764e846066975c2571aebb",
{'q': query,
'limit': '200'},
function(data) {
// PUT IN YOUR CODE HERE TO PROCESS THE SOUNDCLOUD API'S RESPONSE OBJECT
// HINT: CREATE A SEPARATE FUNCTION AND CALL IT HERE
},'json'
);
}

// 'Play' button event handler - play the track in the Stratus player
function changeTrack(url) {
// Remove any existing instances of the Stratus player
$('#stratus').remove();

// Create a new Stratus player using the clicked song's permalink URL
$.stratus({
key: "b3179c0738764e846066975c2571aebb",
auto_play: true,
align: "bottom",
links: url
});
}