From 9b10013c5284d915b544fe30d33f549a620580e3 Mon Sep 17 00:00:00 2001 From: "Daniel W. Chen" Date: Sun, 27 Mar 2016 11:29:04 -0700 Subject: [PATCH] submitting hw6 --- flask/orm-tutorial/models-tutorial/app.db | Bin 9216 -> 6144 bytes .../orm-tutorial/models-tutorial/app/forms.py | 16 +++++- .../models-tutorial/app/models.py | 35 +++++++++++-- .../app/templates/customer.html | 33 +++++++++++- .../models-tutorial/app/templates/home.html | 36 +++++++++++++- .../models-tutorial/app/templates/order.html | 31 ++++++++++++ .../orm-tutorial/models-tutorial/app/views.py | 47 ++++++++++++++---- 7 files changed, 180 insertions(+), 18 deletions(-) create mode 100644 flask/orm-tutorial/models-tutorial/app/templates/order.html diff --git a/flask/orm-tutorial/models-tutorial/app.db b/flask/orm-tutorial/models-tutorial/app.db index 41839c5d7dc60a4b2c021d493facebb3bac8fa5a..b73bf06258cb9adc452ea3955c7cabf6dfd54785 100644 GIT binary patch literal 6144 zcmeHJ&1%~~5Z;yLxHj}qau6XnEV3O$w{vs&Rx>B) z4W6Km>tvZv&P+T!HBziIEyB9U#3$Iq#sH5H$FrbT_se0ep=@oz`6Lr4j2#jui{E-l zo&}bwlfA95;aOMUp9%4>5}Vx&-Ce5!U1})pHk{vN6>ui}DVM6?=wG^g71(~!guFT4 z@4#v3M|My&l5Yg!+pf24I!B|AVNm4Fo4eXY){sruP!t8u>qSE{k)%=~OX2>wTZ5Tz zMRrURD+?#il2Kk04XN!V5eew(mtR?>vss$z2bfKfLMR{~nKXgg6>n5@8B1HFouaJqXdr1INhNmd zz6xi)0#`l&5|@c1y>a5Y19oV{wo1KB9B7}T=Vw31()<4sCraEtY`HO~Cqd}jG2KKe z!Wh*kMF^FJWhbTze@~e7X~Ah@hA*S?SjmwKq~b9mPsmq1RzBg6_`5u2%u9#J1%8^g z7u5@TUv8*<^i0hZ*4FTu72C%iw;d8rZgcwRS)mFi<%j`fC79QZu6{?3)*mu7RnZXVGW?1&a! zCpmTZz+}xmL&l1irs>pVyUb)pgSBZIPbO*lD7Ta;tgqvdpU6l@Q?FhTl167J=>`5f zdvH3Xg8R1XO+~MZ5c%B2nL<8~&$3BA!-$8{lq-42$UnAz~LXF_nuhamOYGvq;JV|tct9OxT(~fs1))UksqRh0tA4-e?;KEqM}lz74+Wr zId|y39meh;vOVv#=Joxg_eebCRl38wkuJffey_uz{IWwj<}nc9eM(4w`@f5C8%|00;m9AOHk_01%i-K<)+J5|$CR UEKKh6U*78%T_`{R2+SLS-|?FD' % self.email + return '' % self.id # Your Address code should go here -# class Address(db.Model): +class Address(db.Model): + id = db.Column(db.Integer, primary_key=True) + street_address = db.Column(db.String(120), unique=False) + city = db.Column(db.String(20), unique=False) + state = db.Column(db.String(5), unique=False) + country = db.Column(db.String(60), unique=False) + zip_code = db.Column(db.String(20), unique=False) + customer_id = db.Column(db.Integer, db.ForeignKey('customer.id')) + + def __repr__(self): + return '
' % self.id + +class Order(db.Model): + id = db.Column(db.Integer, primary_key=True) + num_parts_ordered = db.Column(db.String(60), unique=False) + totalspent = db.Column(db.String(60), unique=False) + + def __repr__(self): + return '
' % self.id diff --git a/flask/orm-tutorial/models-tutorial/app/templates/customer.html b/flask/orm-tutorial/models-tutorial/app/templates/customer.html index d7b457a..7c2f119 100644 --- a/flask/orm-tutorial/models-tutorial/app/templates/customer.html +++ b/flask/orm-tutorial/models-tutorial/app/templates/customer.html @@ -10,6 +10,14 @@

Add Customer to Our Database

{{ form.hidden_tag() }}
+

+ First name:
+ {{ form.fname(size=60) }}
+

+

+ Last name:
+ {{ form.lname(size=60) }}
+

Company name:
{{ form.company(size=120) }}
@@ -18,10 +26,33 @@

Add Customer to Our Database

Customer email:
{{ form.email(size=120) }}

+

+ Customer phone:
+ {{ form.phone(size=20) }}
+

- +

+ Street Address:
+ {{ form.street_address(size=120) }}
+

+

+ City:
+ {{ form.city(size=20) }}
+

+

+ State:
+ {{ form.state(size=5) }}
+

+

+ Country:
+ {{ form.country(size=60) }}
+

+

+ Zip Code:
+ {{ form.zip_code(size=20) }}
+

diff --git a/flask/orm-tutorial/models-tutorial/app/templates/home.html b/flask/orm-tutorial/models-tutorial/app/templates/home.html index 6b447ec..31c3b4c 100644 --- a/flask/orm-tutorial/models-tutorial/app/templates/home.html +++ b/flask/orm-tutorial/models-tutorial/app/templates/home.html @@ -5,6 +5,7 @@

Welcome to ACME Aircraft Parts

@@ -12,16 +13,47 @@

These are all of our awesome customers:

+ + + + + {% for customer in customers %} - + {% for address in customer.addresses.all() %} + + + + + + {% endfor %} + + {% endfor %} +
First NameLast Name Company EmailPhoneCityZip Code
{{ customer.fname }}{{ customer.lname }} {{ customer.company }} {{ customer.email }}{{ customer.phone }}{{ address.city }}{{ address.zip_code }}
+
+
+

Orders from Customers:

+ + + + + + + + + {% for order in orders %} + + + + {% for order in order.customer.all() %} + + {% endfor %} {% endfor %}
Number of PartsTotal SpentCustomer ID
{{ order.num_parts_ordered }}{{ order.totalspent }}{{ order.id }}
diff --git a/flask/orm-tutorial/models-tutorial/app/templates/order.html b/flask/orm-tutorial/models-tutorial/app/templates/order.html new file mode 100644 index 0000000..f78ecb9 --- /dev/null +++ b/flask/orm-tutorial/models-tutorial/app/templates/order.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} +{% block content %} +
+
+
+ Home +
+

Add Order to Our Database

+
+ {{ form.hidden_tag() }} +
+
+

+ Number of Parts:
+ {{ form.num_parts_ordered(size=60) }}
+

+

+ Money Spent:
+ {{ form.totalspent(size=60) }}
+

+

+ Customer IDs (seperate by ","):
+ {{ form.customer_id(size=60) }}
+

+
+
+

+
+
+
+{% endblock %} diff --git a/flask/orm-tutorial/models-tutorial/app/views.py b/flask/orm-tutorial/models-tutorial/app/views.py index e49db4f..d673006 100644 --- a/flask/orm-tutorial/models-tutorial/app/views.py +++ b/flask/orm-tutorial/models-tutorial/app/views.py @@ -1,6 +1,6 @@ from flask import render_template, redirect, request from app import app, models, db -from .forms import CustomerForm +from .forms import CustomerForm, OrderForm @app.route('/') @@ -9,19 +9,48 @@ def index(): @app.route('/create_customer', methods=['GET', 'POST']) def create_customer(): - form = CustomerForm() - if form.validate_on_submit(): + customerForm = CustomerForm() + if customerForm.validate_on_submit(): customer = models.Customer( - company = form.company.data, - email = form.email.data) - # you will need to add Address here + fname = customerForm.fname.data, + lname = customerForm.lname.data, + company = customerForm.company.data, + email = customerForm.email.data, + phone = customerForm.phone.data) + address = models.Address( + street_address = customerForm.street_address.data, + city = customerForm.city.data, + state = customerForm.state.data, + country = customerForm.country.data, + zip_code = customerForm.zip_code.data, + customer = customer) db.session.add(customer) + db.session.add(address) db.session.commit() return redirect('/customers') - return render_template('customer.html', form=form) + return render_template('customer.html', form=customerForm) @app.route('/customers') def display_customer(): customers = models.Customer.query.all() - return render_template('home.html', - customers=customers) + orders = models.Order.query.all() + #orderCustomer = models.orders.query.all() + return render_template('home.html', customers=customers, orders=orders) + +@app.route('/create_order', methods=['GET', 'POST']) +def create_order(): + orderForm = OrderForm() + if orderForm.validate_on_submit(): + order = models.Order( + num_parts_ordered = orderForm.num_parts_ordered.data, + totalspent = orderForm.totalspent.data) + + customerids = orderForm.customer_id.data.split(',') + + for customer_id in customerids: + customer = models.Customer.query.filter_by(id=customer_id).first() + customer.orders.append(order) + db.session.add(order) + db.session.commit() + return redirect('/customers') + return render_template('order.html', form=orderForm)