-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (73 loc) · 2.76 KB
/
app.py
File metadata and controls
83 lines (73 loc) · 2.76 KB
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
from flask import Flask,redirect,render_template,url_for,request
from datetime import datetime
from flask_pymongo import PyMongo
from bson import ObjectId
from config import username,password
# import os
#instance of the imported flask
app = Flask(__name__,template_folder="templates",static_folder="static")
#database connection
app.config['MONGO_URI'] = 'mongodb://localhost:27017/flaskdb'
# app.config['MONGO_URI'] = 'mongodb+srv://{0}:<{1}>@cluster0-iaim4.gcp.mongodb.net/test?retryWrites=true&w=majority'.format(username,password)
taskdb = PyMongo(app)
#showing Views and functions applied
@app.route('/' ,methods=['POST','GET'])
def index():
# checking the method
if request.method == 'POST':
# declaring error
error = None
# targeting the contents
task_todo = request.form['content']
date_create = datetime.utcnow()
# Assigning to database
add_task = {'task': task_todo,
'date_created': date_create
}
# Inserting to the database
taskdb.db.users.insert_one(add_task)
return redirect('/')
else:
# to display on the tables after getting the method to be 'GET'
tasks = taskdb.db.users.find()
return render_template('index.html', tasks = tasks)
@app.route('/delete/<id>')
def delete(id):
# targeting the data with the id
task_to_delete = taskdb.db.users.find_one({"_id":ObjectId(id)})
try:
# deleting it in the database
taskdb.db.users.delete_one(task_to_delete)
return redirect('/')
except:
return 'There is an Error '
@app.route('/update/<id>', methods= ['GET','POST'])
def update(id):
# targeting the data with the id
task_to_update = taskdb.db.users.find_one({'_id': ObjectId(id)})
if request.method == 'POST':
task_to_up = request.form['content']
try:
# updating from the database
taskdb.db.users.update_one({"_id":ObjectId(id)}, { '$set' :{'task': task_to_up}})
return redirect('/')
except:
return "There was an error updating your request"
else:
return render_template('update.html' ,task = task_to_update)
@app.route('/search' )
def searchresult():
key = request.values.get('search')
refer = request.values.get('refer')
if (refer == '_id'):
error = "No result found"
todo_l = taskdb.db.users.find_one({refer:ObjectId(key)})
return render_template('searchresult.html' , error = error)
else:
todo_l = taskdb.db.users.find_one({refer:key})
return render_template('searchresult.html' ,task = todo_l)
@app.errorhandler(404)
def page_not_found(error):
return "This page could not be found! Enter a valid one!",404
if __name__ == "__main__":
app.run(debug=True)