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
Binary file added .DS_Store
Binary file not shown.
Binary file added Assignment/.DS_Store
Binary file not shown.
Binary file added Assignment/HW5_SQL/.DS_Store
Binary file not shown.
Binary file added Assignment/HW5_SQL/HW5_Starter_Code/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file added Assignment/HW5_SQL/HW5_Starter_Code/app.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 13 additions & 5 deletions Assignment/HW5_SQL/HW5_Starter_Code/app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
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
class CustomerForm(Form):
first_name = StringField('first_name', validators=[DataRequired()])
last_name = StringField('last_name', validators=[DataRequired()])
company = StringField('company', validators=[DataRequired()])
email = EmailField('email', validators=[DataRequired()])
phone = StringField('phone', validators=[DataRequired()])
street_address = StringField('street_address', validators=[DataRequired()])
city = StringField('city', validators=[DataRequired()])
state = StringField('state', validators=[DataRequired()])
country = StringField('country', validators=[DataRequired()])
zip_code = StringField('zip_code', validators=[DataRequired()])

class OrderForm(Form):
# Add order input form fields here
name_of_part = StringField('name_of_part', validators=[DataRequired()])
manufacturer_of_part = StringField('manufacturer_of_part', validators=[DataRequired()])
34 changes: 29 additions & 5 deletions Assignment/HW5_SQL/HW5_Starter_Code/app/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import sqlite3 as sql

def insert_data():
# SQL statement to insert into database goes here
def insert_data(first_name, last_name, company, email, phone, street_address, city, state, country, zip_code):
# SQL statement to insert into database goes here
with sql.connect("app.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO customer (first_name, last_name, company, email, phone) VALUES (?,?,?,?,?)", (first_name, last_name, company, email, phone))
con.commit()
new_cur = con.cursor()
new_cur.execute("INSERT INTO address (street_address, city, state, country, zip_code) VALUES (?,?,?,?,?)", (street_address, city, state, country, zip_code))
con.commit()

def insert_orders( name_of_part, manufacturer_of_part):
# SQL statement to insert into database
with sql.connect("app.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO orders ( name_of_part, manufacturer_of_part) VALUES (?,?)", (name_of_part, manufacturer_of_part))
con.commit()

def retrieve_customers():
# SQL statement to query database goes here
# SQL statement to query database goes here
with sql.connect("app.db") as con:
con.row_factory = sql.Row
cur = con.cursor()
result = cur.execute("select * from customer").fetchall()
print(result)
return result

def retrieve_orders():
# SQL statement to query database goes here

# SQL statement to query database goes here
with sql.connect("app.db") as con:
cur = con.cursor()
result = cur.execute("select * from orders").fetchall()
print(result)
return result

##You might have additional functions to access the database
34 changes: 32 additions & 2 deletions Assignment/HW5_SQL/HW5_Starter_Code/app/templates/customer.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,49 @@ <h2>Add Customer to Our Database</h2>
{{ form.hidden_tag() }}
<div class="divider"></div>
<div class="section">
<p>
First Name:<br>
{{ form.first_name(size=120) }}<br>
</p>
<p>
Last Name:<br>
{{ form.last_name(size=120) }}<br>
</p>
<p>
Company name:<br>
{{ form.company(size=120) }}<br>
</p>
<p>
Customer email:<br>
{{ form.email(size=120) }}<br>
<!-- Additional form fields for the customer should go here -->
</p>
<p>
Phone Number:<br>
{{ form.phone(size=120) }}<br>
</p>
</div>
<div class="divider"></div>
<div class="section">
<!-- Additional form fields for the Address should go here -->
<p>
Street Address:<br>
{{ form.street_address(size=120) }}<br>
</p>
<p>
City:<br>
{{ form.city(size=120) }}<br>
</p>
<p>
State:<br>
{{ form.state(size=120) }}<br>
</p>
<p>
Country:<br>
{{ form.country(size=120) }}<br>
</p>
<p>
Zip Code:<br>
{{ form.zip_code(size=120) }}<br>
</p>
</div>
<p><input type="submit" value="Create Customer"></p>
</form>
Expand Down
30 changes: 23 additions & 7 deletions Assignment/HW5_SQL/HW5_Starter_Code/app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,40 @@ <h3>These are all of our awesome customers:</h3>
<table class="striped">
<thead>
<tr>
<th data-field="first_name">First Name</th>
<th data-field="last_name">Last Name</th>
<th data-field="phone">Phone</th>
<th data-field="company">Company</th>
<th data-field="email">Email</th>
</tr>
</thead>
{% for customer in customers %}
{% for cu in customer %}
<tr>
<td>{{ customer['company'] }}</td>
<td>{{ customer['email'] }}</td>
<!-- Add more fields to showcase on this page
Below is a method of adding the 'add an order' button which will route you to create_order in views.py
<td><a href="{{ url_for('create_order', value=customer['customer_id']) }}">Add an Order</a></td> -->
<td>{{ cu['first_name'] }}</td>
<td>{{ cu['last_name'] }}</td>
<td>{{ cu['phone'] }}</td>
<td>{{ cu['company'] }}</td>
<td>{{ cu['email'] }}</td>
<td><a href="{{ url_for('create_order', value=cu['customer_id']) }}">Add an Order</a></td>
</tr>
{% endfor %}
</table>
</div>
<div class="section">
<h3>Orders from Customers:</h3>
<!-- Display all the current existing order information here -->
<table class="striped">
<thread>
<th data-field="name_of_part">Name of Part</th>
<th data-field="manufacturer_of_part">Manufacturer of Part</th>
</thread>
{% for or in orders %}
<tr>
<td>{{ or[1]}}</td>

<td>{{ or[2]}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions Assignment/HW5_SQL/HW5_Starter_Code/app/templates/orders.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% 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 to Cart</h2>
<form action="" method="post" name="login">
{{ form.hidden_tag() }}
<div class="divider"></div>
<div class="section">
<p>
Name of Part:<br>
{{ form.name_of_part(size=120) }}<br>
</p>
<p>
Manufacturer of Part:<br>
{{ form.manufacturer_of_part(size=120) }}<br>
</p>
</div>
<p><input type="submit" value="Create Order"></p>
</form>
</div>
</div>
{% endblock %}
33 changes: 26 additions & 7 deletions Assignment/HW5_SQL/HW5_Starter_Code/app/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import render_template, redirect, request
from app import app, models, db
from .forms import CustomerForm
from .forms import CustomerForm, OrderForm
# Access the models file to use SQL functions
from .models import *


@app.route('/')
Expand All @@ -12,20 +13,38 @@ def index():
def create_customer():
form = CustomerForm()
if form.validate_on_submit():
# Get data from the form
# Send data from form to Database
return redirect('/customers')
first_name = form.first_name.data
last_name = form.last_name.data
company = form.company.data
email = form.email.data
phone = form.phone.data
street_address = form.street_address.data
city = form.city.data
state = form.state.data
country = form.country.data
zip_code = form.zip_code.data
insert_data(first_name, last_name, company, email, phone, street_address, city, state, country, zip_code),
return redirect('/customer')
return render_template('customer.html', form=form)

@app.route('/customers')
@app.route('/customer')
def display_customer():
# Retreive data from database to display
cu = retrieve_customers()
orders = models.retrieve_orders()
print(cu)
return render_template('home.html',
customers=customers)
customer=cu, orders=orders)

@app.route('/create_order/<value>', methods=['GET', 'POST'])
def create_order(value):
# Get data from the form
# Send data from form to Database
return redirect('/customers')
return render_template('order.html', form=orderForm)
form = OrderForm()
if form.validate_on_submit():
name_of_part = form.name_of_part.data
manufacturer_of_part = form.manufacturer_of_part.data
insert_orders(name_of_part, manufacturer_of_part),
return redirect('/customer')
return render_template('orders.html', form=form)
38 changes: 38 additions & 0 deletions Assignment/HW5_SQL/HW5_Starter_Code/schema.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
-- Insert code to create Database Schema
-- This will create your .db database file for use
drop table if exists customer;
drop table if exists address;
drop table if exists orders;
drop table if exists customer_order;

create table customer (
customer_id integer primary key,
first_name text not null,
last_name text not null,
company text not null,
email text not null,
phone text not null
);

create table address (
id integer primary key,
street_address varchar(200) not null,
city text not null,
state text not null,
country text not null,
zip_code varchar(11) not null,
customer_id integer,
foreign key(customer_id) references customer(customer_id)
);

create table orders (
order_id integer primary key,
name_of_part text not null,
manufacturer_of_part text not null
);

create table customer_order (
id integer primary key,
order_id integer,
customer_id integer,
foreign key(order_id) references orders(order_id),
foreign key(customer_id) references customer(customer_id)
);
Binary file added Labs/.DS_Store
Binary file not shown.
Binary file added Labs/Lab-SQLite.zip
Binary file not shown.
Binary file added Labs/Lab-SQLite/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions Labs/Lab-SQLite/Untitled.rtf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{\rtf1\ansi\ansicpg1252\cocoartf1504
{\fonttbl\f0\fnil\fcharset0 Menlo-Regular;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;\red255\green255\blue255;}
{\*\expandedcolortbl;\csgray\c100000;\csgray\c0;\csgray\c100000;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0

\f0\fs22 \cf2 \cb3 \CocoaLigature0 sqlite3 app.db < schema.sql}
Binary file added Labs/Lab-SQLite/app.db
Binary file not shown.
8 changes: 8 additions & 0 deletions Labs/Lab-SQLite/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
Binary file added Labs/Lab-SQLite/app/__init__.pyc
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions Labs/Lab-SQLite/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()])

Binary file added Labs/Lab-SQLite/app/forms.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions Labs/Lab-SQLite/app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# squlite3 app.db < schema.sql creates an app.db that contians table

import sqlite3 as sql

def insert_customer(company,email):
# SQL statement to insert into database goes here
with sql.connect("app.db") as con:
cur = con.cursor()
# Python recognizes the variables we want to enter as '?'
cur.execute("INSERT INTO customers (company,email) VALUES (?,?)", (company,email))
con.commit()

# con = sql.connect("app.db")
# blah blah
# con.commit()
# con.close()

def retrieve_customers():
# SQL statement to query database goes here
with sql.connect("app.db") as con:
con.row_factory = sql.Row
cur = con.cursor()
result = cur.execute("select * from customers").fetchall()
return result
Binary file added Labs/Lab-SQLite/app/models.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions Labs/Lab-SQLite/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>
31 changes: 31 additions & 0 deletions Labs/Lab-SQLite/app/templates/customer.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">
<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">
<!--add input fields-->
<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 %}
Loading