-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
135 lines (100 loc) · 4.7 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
128
129
130
131
132
133
134
135
import os
from flask import Flask, render_template, redirect, request, session, url_for
from flask_pymongo import PyMongo, pymongo
from flask_paginate import Pagination, get_page_parameter
from bson.objectid import ObjectId
import re
app = Flask(__name__)
app.secret_key = "randomstring123"
app.config["MONGO_DBNAME"] = 'guitar_collection'
app.config["MONGO_URI"] = 'mongodb+srv://root:[email protected]/guitar_collection?retryWrites=true&w=majority'
mongo = PyMongo(app)
@app.route('/')
@app.route('/get_guitars')
def get_guitars():
return render_template("guitars.html",
guitars=mongo.db.guitars.find())
@app.route('/add_guitars')
def add_guitars():
return render_template('addguitars.html',
categories=mongo.db.categories.find())
@app.route('/insert_guitars', methods=['POST'])
def insert_guitars():
guitars = mongo.db.guitars
guitars.insert_one(request.form.to_dict())
return redirect(url_for('get_guitars'))
@app.route('/edit_guitars/<guitar_id>')
def edit_guitars(guitar_id):
the_guitar = mongo.db.guitars.find_one({"_id": ObjectId(guitar_id)})
all_categories = mongo.db.categories.find()
return render_template('editguitars.html', guitar=the_guitar,
categories=all_categories)
@app.route('/update_guitar/<guitar_id>', methods=["POST"])
def update_guitar(guitar_id):
guitars = mongo.db.guitars
guitars.update( {'_id': ObjectId(guitar_id)},
{
'guitar_name':request.form.get('guitar_name'),
'category_name':request.form.get('category_name'),
'guitar_description': request.form.get('guitar_description'),
'date_created': request.form.get('date_created'),
'image_url': request.form.get('image_url')
})
return redirect(url_for('get_guitars'))
@app.route('/delete_guitar/<guitar_id>')
def delete_guitar(guitar_id):
# access the tasks collection and call remove and we pass in the task_id as the parameter.
# Key value pair inside the curly braces.I use the object ID to format or parse the task ID in a way that's acceptable to Mongo.
# Once that's in place, I want to return or redirect.So I redirect to get tasks.
# Because once that function is complete, I want to see it disappear. I want visual evidence to see that that task is no longer on our list.
# So I redirect to the get_tasks function.
mongo.db.guitars.remove({'_id': ObjectId(guitar_id)})
return redirect(url_for('get_guitars'))
@app.route('/get_categories')
def get_categories():
return render_template('categories.html',
categories=mongo.db.categories.find())
@app.route('/delete_category/<category_id>')
def delete_category(category_id):
mongo.db.categories.remove({'_id': ObjectId(category_id)})
return redirect(url_for('get_categories'))
@app.route('/edit_category/<category_id>')
def edit_category(category_id):
return render_template('editcategory.html',
category=mongo.db.categories.find_one({'_id': ObjectId(category_id)}))
@app.route('/update_category/<category_id>', methods=['POST'])
def update_category(category_id):
mongo.db.categories.update(
{'_id': ObjectId(category_id)},
{'category_name': request.form.get('category_name')})
return redirect(url_for('get_categories'))
@app.route('/insert_category', methods=['POST'])
def insert_category():
# accessing mongoDB data base in preperstion of the insert. category_doc is creating a new BSON formatted doc.
category_doc = {'category_name': request.form.get('category_name')}
# adding the category doc into the catergory table.
mongo.db.categories.insert_one(category_doc)
# then return a redirect back to categories.
return redirect(url_for('get_categories'))
# the function that will direct and render the view that allows user to add a new category in the first place.
@app.route('/add_category')
def add_category():
return render_template('addcategory.html')
# Search Database for Guitars
@app.route('/find_guitars', methods=['GET', 'POST'])
def find_guitars():
if request.method=='POST':
# get the search term
search_term = request.form.get("search_term")
# create the index
mongo.db.guitars.create_index( [("$**", 'text')] )
# search with the search term that came through the form
cursor = mongo.db.guitars.find({ "$text": { "$search": search_term } })
guitars = [guitar for guitar in cursor]
# send guitars to page
return render_template('search.html', guitars=guitars, query=search_term)
return render_template('search.html')
if __name__ == '__main__':
app.run(host=os.environ.get('IP'),
port=int(os.environ.get('PORT')),
debug=True)