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
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# a risk that we may run into circular dependencies, so we do it at the
# last minute here.

from views import *
# from views import *
from views2 import *

app.run(debug=True)
4 changes: 4 additions & 0 deletions backup.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
waffle,15.0,200
chocolate,15.0,200
ice cream,10.0,100
cake,30.0,400
27 changes: 27 additions & 0 deletions backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from models import *


def save_data():

with open('backup.csv', 'w') as f: # Open file 'backup.csv' for writing

for dessert in Dessert.query.all():
# Create a comma separated line
line = "{},{},{}\n".format(dessert.name, dessert.price,
dessert.calories)
# Write it to the file
f.write(line)

def load_data():

with open('backup.csv') as f:
for line in f:
name, price, calories = line.split(',')
d = Dessert(name, price, calories)
db.session.add(d)
db.session.commit()


if __name__=="__main__":
# save_data()
load_data()
Binary file added desserts.db
Binary file not shown.
63 changes: 60 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class Dessert(db.Model):
price = db.Column(db.Float)
calories = db.Column(db.Integer)

def __init__(self, name, price, calories):
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship("User", backref="desserts")

def __init__(self, name, price, calories, user_id):
self.name = name
self.price = price
self.calories = calories
self.user_id = user_id

def calories_per_dollar(self):
if self.calories:
Expand All @@ -32,7 +36,25 @@ def __init__(self, name):
self.name = name


def create_dessert(new_name, new_price, new_calories):
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100))
password = db.Column(db.String(100))
email = db.Column(db.String(250))
name = db.Column(db.String(100))
avatar = db.Column(db.String(250))

def __init__(self, username, password, email, name, avatar):
self.username = username
self.password = password
self.email = email
self.name = name
self.avatar = avatar


def create_dessert(new_name, new_price, new_calories, user_id):
# Dessert.query.filter_by

# Create a dessert with the provided input.

# We need every piece of input to be provided.
Expand All @@ -45,8 +67,17 @@ def create_dessert(new_name, new_price, new_calories):
if new_name == '' or new_price == '' or new_calories == '':
raise Exception("Need name, price and calories!")

# Check that name does not already exist

if Dessert.query.filter_by(name=new_name).first() is not None:
raise Exception("That name already exists!")

# Check that calories is between 0 and 2000
if 0 > new_calories > 2000:
raise Exception("Really that many calories?")

# This line maps to line 16 above (the Dessert.__init__ method)
dessert = Dessert(new_name, new_price, new_calories)
dessert = Dessert(new_name, new_price, new_calories, user_id)

# Actually add this dessert to the database
db.session.add(dessert)
Expand Down Expand Up @@ -82,6 +113,32 @@ def delete_dessert(id):
return "Dessert not found"


def update_dessert(id, new_name, new_price, new_calories):

dessert = Dessert.query.get(id)

if dessert:

dessert.name = new_name
dessert.price = new_price
dessert.calories = new_calories

try:
db.session.commit()
return "Dessert {} updated".format(new_name)
except:
db.session.rollback()
return "Something went wrong"
else:
return "Dessert not found"


def log_out(user_id):
if user_id:
user_id = None
return user_id


if __name__ == "__main__":

# Run this file directly to create the database tables.
Expand Down
Binary file added static/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion templates/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ <h3>Add Dessert</h3>

</div>

</body>
</body>
17 changes: 15 additions & 2 deletions templates/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@ <h4>{{ dessert.name }}</h4>

<p>Calories per dollar: {{ dessert.calories_per_dollar() }}</p>

<a href="/update/{{ dessert.id }}" class="btn btn-info">Update Item</a>

<a href="/delete/{{ dessert.id }}" class="btn btn-danger">Delete {{ dessert.name }}</a>
<a href="/delete/{{ dessert.id }}" class="btn btn-danger" id="delete-button">Delete {{ dessert.name }}</a>


</div>
<script type="text/javascript">

</body>
var delButton = document.getElementById('delete-button');

delButton.addEventListener('click', function(){
confirm("Are you sure you want to delete this item?")

// how can I pass the result of this into python...
})


</script>

</body>
10 changes: 10 additions & 0 deletions templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<title>Desserts</title>

<!-- Favicon! -->
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">

<!-- BEGIN BOOTSTRAP -->

<!-- Latest compiled and minified CSS -->
Expand All @@ -15,4 +18,11 @@

<!-- END BOOTSTRAP -->

<!-- Bit of CSS -->
<style>
.form-horizontal {
width: 50%;
margin: 50px auto;
}
</style>
</head>
14 changes: 12 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@

<h3>Dessert Menu</h3>

<form action="/desserts/{{ id }}" method="POST">
<div class="input-group">
<input type="text" class="form-control" name="search-name" placeholder="Search for name...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div>
</form>

{% if message %}
<div class="alert alert-info" role="alert">

{{ message }}

</div>
Expand All @@ -22,9 +31,10 @@ <h3>Dessert Menu</h3>
{% endfor %}
</ul>


<a href="/add" class="btn btn-success">Add Item</a>


</div>

</body>
</body>
23 changes: 23 additions & 0 deletions templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% include 'header.html' %}

<body>
<form action="/menu" method="POST" class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">User Name</label>
<div class="col-sm-8">
<input type="text" name="username" id="inputEmail3" placeholder="User name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Password</label>
<div class="col-sm-8">
<input type="password" name="password" id="inputPassword3" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" class="btn btn-default">Sign in</button>
</div>
</div>
</form>
</body>
41 changes: 41 additions & 0 deletions templates/menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% include 'header.html' %}

<body>

<div class="container">


<h3>Dessert Menu</h3>

<form action="/desserts/{{ dessert_id }}" method="POST">
<div class="input-group">
<input type="text" class="form-control" name="search-name" placeholder="Search for name...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div>
</form>

{% if message %}
<div class="alert alert-info" role="alert">

{{ message }}

</div>
{% endif %}


<ul>
{% for dessert in desserts %}
<li><a href="/desserts/{{ dessert.id }}">{{ dessert.name }}</a> - ${{ dessert.price }}, {{ dessert.calories }} calories</li>
{% endfor %}
</ul>


<a href="/add" class="btn btn-success">Add Item</a>
<a href="/login" class="btn btn-default">Log Out</a>


</div>

</body>
67 changes: 67 additions & 0 deletions templates/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% include 'header.html' %}

<body>

<div class="container">


<h3>Update Dessert</h3>

<!-- {% if dessert %}

<div class="alert alert-success" role="alert">

<p><strong>{{dessert.name}}</strong> successfully updated! Add another below.</p>
<p><a href="/">Back to Dessert List</a></p>

</div>

{% endif %}

{% if error %}
<div class="alert alert-danger" role="alert">

An error occurred trying to create your dessert:

{{ error }}

</div>
{% endif %} -->


<!-- The form action and method correspond to a Flask route -->
<form action="/update/{{ id }}" method="POST" class="form-inline">

<div class="form-group">

<label for="name_id">Name</label>
<input class="form-control" id="name_id" name="name_field" type="text" value="{{ dessert.name }}"/>

</div>

<div class="form-group">

<label for="price_id">Price</label>
<input class="form-control" id="price_id" name="price_field" type="text" size="4" value="{{ dessert.price }}" />

</div>

<div class="form-group">

<label for="cals_id">Calories</label>
<input class="form-control" id="cals_id" name="cals_field" type="text" size="4" value="{{ dessert.calories }}"/>

</div>



<!-- We need a submit button to submit a form! -->
<input type="submit" value="Update" class="btn btn-success"/>


</form>


</div>

</body>
Loading