-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
128 lines (99 loc) · 3.93 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from flask import Flask, request, redirect, render_template, url_for
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
############################################################
# SETUP
############################################################
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/plantsDatabase"
mongo = PyMongo(app)
############################################################
# ROUTES
############################################################
@app.route('/')
def plants_list():
"""Display the plants list page."""
# TODO: Replace the following line with a database call to retrieve *all*
# plants from the Mongo database's `plants` collection.
plants_data = ''
context = {
'plants': plants_data,
}
return render_template('plants_list.html', **context)
@app.route('/about')
def about():
"""Display the about page."""
return render_template('about.html')
@app.route('/create', methods=['GET', 'POST'])
def create():
"""Display the plant creation page & process data from the creation form."""
if request.method == 'POST':
# TODO: Get the new plant's name, variety, photo, & date planted, and
# store them in the object below.
new_plant = {
'name': '',
'variety': '',
'photo_url': '',
'date_planted': ''
}
# TODO: Make an `insert_one` database call to insert the object into the
# database's `plants` collection, and get its inserted id. Pass the
# inserted id into the redirect call below.
return redirect(url_for('detail', plant_id=''))
else:
return render_template('create.html')
@app.route('/plant/<plant_id>')
def detail(plant_id):
"""Display the plant detail page & process data from the harvest form."""
# TODO: Replace the following line with a database call to retrieve *one*
# plant from the database, whose id matches the id passed in via the URL.
plant_to_show = ''
# TODO: Use the `find` database operation to find all harvests for the
# plant's id.
# HINT: This query should be on the `harvests` collection, not the `plants`
# collection.
harvests = ''
context = {
'plant' : plant_to_show,
'harvests': harvests
}
return render_template('detail.html', **context)
@app.route('/harvest/<plant_id>', methods=['POST'])
def harvest(plant_id):
"""
Accepts a POST request with data for 1 harvest and inserts into database.
"""
# TODO: Create a new harvest object by passing in the form data from the
# detail page form.
new_harvest = {
'quantity': '', # e.g. '3 tomatoes'
'date': '',
'plant_id': plant_id
}
# TODO: Make an `insert_one` database call to insert the object into the
# `harvests` collection of the database.
return redirect(url_for('detail', plant_id=plant_id))
@app.route('/edit/<plant_id>', methods=['GET', 'POST'])
def edit(plant_id):
"""Shows the edit page and accepts a POST request with edited data."""
if request.method == 'POST':
# TODO: Make an `update_one` database call to update the plant with the
# given id. Make sure to put the updated fields in the `$set` object.
return redirect(url_for('detail', plant_id=plant_id))
else:
# TODO: Make a `find_one` database call to get the plant object with the
# passed-in _id.
plant_to_show = ''
context = {
'plant': plant_to_show
}
return render_template('edit.html', **context)
@app.route('/delete/<plant_id>', methods=['POST'])
def delete(plant_id):
# TODO: Make a `delete_one` database call to delete the plant with the given
# id.
# TODO: Also, make a `delete_many` database call to delete all harvests with
# the given plant id.
return redirect(url_for('plants_list'))
if __name__ == '__main__':
app.run(debug=True)