-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
157 lines (129 loc) · 4.32 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json, nanoid
from datetime import date
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
# Load the notes
# Check if the file isn"t there
with open("notes.json", "a") as notesDb:
pass
with open("notes.json") as notesDb:
notesTxt = notesDb.read()
if notesTxt == "":
with open("notes.json", "a") as notesDb:
notesDb.write("[]")
# now load the notes into python
with open("notes.json") as notesDb:
notes = json.loads(notesDb.read())
@app.route("/")
def index():
# render from the global notes variable
global notes
# search for notes
if request.args.get("keyword"):
# fetch keyword
keyword = request.args.get("keyword")
lowerCaseKW = keyword.lower()
# filter notes
filteredNotes = []
for note in notes:
name = note["name"].lower()
description = note["description"].lower()
if name.find(lowerCaseKW) != -1:
filteredNotes.append(note)
continue
if description.find(lowerCaseKW) != -1:
filteredNotes.append(note)
# render results
return render_template("search.html", notes=filteredNotes, keyword=keyword)
if request.full_path == "/?keyword=":
return redirect("/")
# render homepage
return render_template("index.html", notes=notes)
@app.route("/add-note")
def addNote():
# edit the global notes variable
global notes
# get the new note
args = request.args
newNote = {
"id": nanoid.generate(),
"name": args.get("name"),
"description": args.get("description"),
"date": date.today().__str__()
}
# add it to the list and dump that to the file
notes.append(newNote)
with open("notes.json", "w") as notesDb:
notesDb.write(json.dumps(notes))
# go back to the home page once we add the note
return redirect("/")
# route for viewing the datails
@app.route('/note-details')
def noteDetails():
global notes
if request.args.get("id"):
# fetch id
noteID = request.args.get("id")
# filter out the note which should be deleted
noteToShow = list(filter(lambda x: x["id"] == noteID, notes))[0]
# render with the details template
return render_template("details.html",note=noteToShow)
# route for editing a note
@app.route('/edit-note')
def editNote():
global notes
args = request.args
# for editing the note
if args.get("id") and args.get("name"):
# fetch info
edittedNote = {
"id": args.get("id"),
"name": args.get("name"),
"description": args.get("description"),
"date": args.get("date")
}
# updating the notes and dumping
updatedNotes = []
for note in notes:
if note["id"] == edittedNote["id"]:
updatedNotes.append(edittedNote)
else:
updatedNotes.append(note)
notes = updatedNotes
with open("notes.json", "w") as notesDb:
notesDb.write(json.dumps(notes))
# render home
return redirect("/")
if args.get("id"):
# fetch id
noteID = request.args.get("id")
# filter out the note which should be deleted
noteToShow = list(filter(lambda x: x["id"] == noteID, notes))[0]
# render with the details template
return render_template("edit.html",note=noteToShow)
# route for deleting a note
@app.route('/delete-note')
def deleteNote():
# edit the global notes variable
global notes
if request.args.get("id"):
# fetch id
noteID = request.args.get("id")
print(noteID)
# filter out the note which should be deleted
noteToDelete = list(filter(lambda x: x["id"] == noteID, notes))[0]
# remove the note and dump the list to json
notes.remove(noteToDelete)
with open("notes.json", "w") as notesDb:
notesDb.write(json.dumps(notes))
# go back to the home page
return redirect("/")
# delete all notes
@app.route('/delete-all-notes')
def deleteAllNotes():
# edit the global notes variable
global notes
notes = []
with open("notes.json", "w") as notesDb:
notesDb.write("[]")
return redirect("/")