From ab0801aed9ea45a50488390ec895df85e413c844 Mon Sep 17 00:00:00 2001
From: Karla Lopez
Date: Thu, 4 Jun 2015 19:33:18 -0700
Subject: [PATCH 1/9] Update and search
---
desserts.db | Bin 0 -> 12288 bytes
models.py | 49 +++++++++++++++++++++++++++
templates/details.html | 4 +--
templates/edit.html | 71 +++++++++++++++++++++++++++++++++++++++
templates/index.html | 44 ++++++++++++++++--------
views.py | 74 ++++++++++++++++++++++++++++++++++++++++-
6 files changed, 225 insertions(+), 17 deletions(-)
create mode 100644 desserts.db
create mode 100644 templates/edit.html
diff --git a/desserts.db b/desserts.db
new file mode 100644
index 0000000000000000000000000000000000000000..7c05c81ced712738d3c459f43a02236d58da9748
GIT binary patch
literal 12288
zcmeI%&r0J!90%~3v_TrsEUrb0h(AxJ;4T$kz_i3a7~9<#1&`BYb~}(7Nh5n)dfV67
zlMf<-FW@V9+ILuI#A*xfVK2fi-wBhM{4&Y!$0w&D$Dc!0O8Sx)Ls8N;d&f9uJ4B4J
zhVB*JC(Y86>f}O?d}{uuX@h;Zx99a;YvDI@l|G?C00Izz00bZa0SG_<0uX=z1m0d?
zV9hPp>wLUjihd@C^8C{DN+$~ZIH1^XhXI)gc}`8GbkL21y&$4)FQ)EE7;chNI~PMq
zr+(Dg_akqs)oPmHr|96wkIv{YI3rJ`O{Y0t;&aO@D}4O@F;yx@BUzMBgEf=zDfqWJ
zFA9}N+6{YtY)VK(mKREnCPlwqmf4$E+#BZpa_`(5_vqz#`;UQ)5P$##AOHafKmY;|
zfB*y_0D*r+;KwTGd$|&QkqRf(RexLKJQ?Iko{3W0j*cu&N2+GcY^3U5vG?now+r=E
zq*4{R=jlZsx28jsN=o!wZS(v>&1S2-Hpy$(Et_p{tDR@LS+{%Hzy4X}n~U6L>pJ}W
kUW?tD{lEU{j|Kq 50:
+ raise Exception("A bit too pricey!")
+
+ # Check for calories
+ if int(new_calories) > 1000:
+ raise Exception("No one should eat that")
+
+ #Check duplicates
+ if Dessert.query.filter_by(name=new_name).first():
+ raise Exception("Already in the database")
+
+
# This line maps to line 16 above (the Dessert.__init__ method)
dessert = Dessert(new_name, new_price, new_calories)
@@ -61,6 +74,42 @@ def create_dessert(new_name, new_price, new_calories):
db.session.rollback()
+def edit_dessert(dessert, new_name, new_price, new_calories):
+ # Edit a dessert with the provided input.
+
+
+ # Can you think of other ways to write this following check?
+ if new_name is None or new_price is None or new_calories is None:
+ raise Exception("Need name, price and calories!")
+
+ # They can also be empty strings if submitted from a form
+ if new_name == '' or new_price == '' or new_calories == '':
+ raise Exception("Need name, price and calories!")
+
+ # Check for price
+ if int(new_price) > 50:
+ raise Exception("A bit too pricey!")
+
+ # Check for calories
+ if int(new_calories) > 1000:
+ raise Exception("No one should eat that")
+
+ # This line maps to line 16 above (the Dessert.__init__ method)
+ dessert.name = new_name
+ dessert.price = new_price
+ dessert.calories = new_calories
+
+ # Save all pending changes to the database
+
+ try:
+ db.session.commit()
+ return dessert
+ except:
+ # If something went wrong, explicitly roll back the database
+ db.session.rollback()
+
+
+
def delete_dessert(id):
dessert = Dessert.query.get(id)
diff --git a/templates/details.html b/templates/details.html
index 23f0356..2a5d9aa 100644
--- a/templates/details.html
+++ b/templates/details.html
@@ -15,9 +15,9 @@ {{ dessert.name }}
Calories per dollar: {{ dessert.calories_per_dollar() }}
- Delete {{ dessert.name }}
+ Edit {{ dessert.name }} Delete {{ dessert.name }}
-
+
+
+
+
+
Edit Dessert
+
+ {% if dessert %}
+
+
+
+
+
+ {% endif %}
+
+ {% if error %}
+
+
+ An error occurred trying to edit your dessert:
+
+ {{ error }}
+
+
+ {% endif %}
+
+
+
+
+
+
+
+
+
+
+