-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
106 lines (76 loc) · 3.39 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
import os
from flask import Flask, render_template, redirect, request, url_for
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
app = Flask(__name__)
app.config["MONGO_DBNAME"] = 'task_manager'
app.config["MONGO_URI"] = 'mongodb+srv://root:[email protected]/task_manager?retryWrites=true&w=majority'
#About the app.config mongo uri: It's far better to use an environment variable, which is better in production, so it wont be visible for everyone when pushing it to github.
mongo = PyMongo(app)
#--------------------------Tasks---------------------------------
@app.route('/')
@app.route('/get_tasks')
def get_tasks():
return render_template('tasks.html', tasks=mongo.db.tasks.find())
@app.route('/add_task')
def add_task():
return render_template('addtask.html', categories=mongo.db.categories.find())
@app.route('/insert_task', methods=['POST'])
def insert_task():
tasks = mongo.db.tasks
tasks.insert_one(request.form.to_dict())
return redirect(url_for('get_tasks'))
@app.route('/edit_task/<task_id>')
def edit_task(task_id):
the_task = mongo.db.tasks.find_one({"_id": ObjectId(task_id)})
all_categories = mongo.db.categories.find()
return render_template('edittask.html', task=the_task,
categories=all_categories)
@app.route('/update_task/<task_id>', methods=["POST"])
def update_task(task_id):
tasks = mongo.db.tasks
tasks.update( {'_id': ObjectId(task_id)},
{
'task_name':request.form.get('task_name'),
'category_name':request.form.get('category_name'),
'task_description': request.form.get('task_description'),
'due_date': request.form.get('due_date'),
'is_urgent':request.form.get('is_urgent')
})
return redirect(url_for('get_tasks'))
@app.route('/delete_task/<task_id>')
def delete_task(task_id):
mongo.db.tasks.remove({'_id': ObjectId(task_id)})
return redirect(url_for('get_tasks'))
#--------------------------Categories----------------------------
@app.route('/get_categories')
def get_categories():
return render_template('categories.html',
categories=mongo.db.categories.find())
@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('/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('/insert_category', methods=['POST'])
def insert_category():
category_doc = {'category_name': request.form.get('category_name')}
mongo.db.categories.insert_one(category_doc)
return redirect(url_for('get_categories'))
@app.route('/add_category')
def add_category():
return render_template('addcategory.html')
if __name__ == '__main__':
app.run(host=os.environ.get('IP'),
port=int(os.environ.get('PORT')),
debug=True)