-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
155 lines (115 loc) · 4.78 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
from flask import Flask, render_template, request, redirect, url_for
from flaskext.markdown import Markdown
import pickle
from os import path as os_path, mkdir as os_mkdir, remove as os_remove
from datetime import datetime
import sys, getopt
app = Flask("Champagne")
Markdown(app)
noteList = []
noteDir = "notes"
# create note directory if it doesn't exist
if not os_path.exists(noteDir):
os_mkdir(noteDir)
noteListFileName = os_path.join(noteDir, "notes.pkl")
# check for existing file with note metadata
if os_path.exists(noteListFileName):
with open(noteListFileName, 'rb') as notesFile:
noteList = pickle.load(notesFile)
@app.route("/")
def home():
return render_template("home.html", notes=noteList)
@app.route("/addNote")
def addNote():
return render_template("noteForm.html", headerLabel="New Note", submitAction="createNote", cancelUrl=url_for('home'))
@app.route("/createNote", methods=["post"])
def createNote():
# get next note id
if len(noteList):
idList = [ int(i['id']) for i in noteList ]
noteId = str(max(idList)+1)
else:
noteId = "1"
noteFileName = os_path.join(noteDir, noteId+".pkl")
lastModifiedDate = datetime.now()
lastModifiedDate = lastModifiedDate.strftime("%d-%b-%Y %H:%M:%S")
noteTitle = request.form['noteTitle']
noteMessage = request.form['noteMessage']
note = {'id': noteId, 'title': noteTitle, 'lastModifiedDate': lastModifiedDate, 'message': noteMessage}
# save the note
with open(noteFileName, 'wb') as noteFile:
pickle.dump(note, noteFile)
# add metadata to list of notes for display on home page
noteList.append({'id': noteId, 'title': noteTitle, 'lastModifiedDate': lastModifiedDate})
# save changes to the file containing the list of note metadata
with open(noteListFileName, 'wb') as notesFile:
pickle.dump(noteList, notesFile)
return redirect(url_for('viewNote', noteId=noteId))
@app.route("/viewNote/<int:noteId>")
def viewNote(noteId):
noteId = str(noteId)
noteFileName = os_path.join(noteDir, noteId+".pkl")
with open(noteFileName, 'rb') as noteFile:
note = pickle.load(noteFile)
return render_template("viewNote.html", note=note, submitAction="/saveNote")
@app.route("/editNote/<int:noteId>")
def editNote(noteId):
noteId = str(noteId)
noteFileName = os_path.join(noteDir, noteId+".pkl")
with open(noteFileName, 'rb') as noteFile:
note = pickle.load(noteFile)
cancelUrl = url_for('viewNote', noteId=noteId)
return render_template("noteForm.html", headerLabel="Edit Note", note=note, submitAction="/saveNote", cancelUrl=cancelUrl)
@app.route("/saveNote", methods=["post"])
def saveNote():
lastModifiedDate = datetime.now()
lastModifiedDate = lastModifiedDate.strftime("%d-%b-%Y %H:%M:%S")
noteId = str(int(request.form['noteId']))
noteTitle = request.form['noteTitle']
noteMessage = request.form['noteMessage']
noteFileName = os_path.join(noteDir, noteId+".pkl")
note = {'id': noteId, 'title': noteTitle, 'lastModifiedDate': lastModifiedDate, 'message': noteMessage}
# save the note
with open(noteFileName, 'wb') as noteFile:
pickle.dump(note, noteFile)
# remove the old version of the note from the list of note metadata and add the new version
global noteList
newNoteList = [ i for i in noteList if not (i['id'] == noteId) ]
noteList = newNoteList
# add metadata to list of notes for display on home page
noteList.append({'id': noteId, 'title': noteTitle, 'lastModifiedDate': lastModifiedDate})
# save changes to the file containing the list of note metadata
with open(noteListFileName, 'wb') as notesFile:
pickle.dump(noteList, notesFile)
return redirect(url_for('viewNote', noteId=noteId))
@app.route("/deleteNote/<int:noteId>")
def deleteNote(noteId):
# delete the note file
noteFileName = os_path.join(noteDir, noteId+".pkl")
os_remove(noteFileName)
# remove the note from the list of note metadata
global noteList
newNoteList = [ i for i in noteList if not (i['id'] == noteId) ]
noteList = newNoteList
# save changes to the file containing the list of note metadata
with open(noteListFileName, 'wb') as notesFile:
pickle.dump(noteList, notesFile)
return redirect("/")
if __name__ == "__main__":
debug = False
try:
opts, args = getopt.getopt(sys.argv[1:], "h:p:", ["debug"])
except getopt.GetoptError:
print('usage: main.py [-h 0.0.0.0] [-p 5000] [--debug]')
sys.exit(2)
port = "5000"
host = "0.0.0.0"
print(opts)
for opt, arg in opts:
if opt == '-p':
port = arg
elif opt == '-h':
host = arg
elif opt == "--debug":
debug = True
app.run(host=host, port=port, debug=debug)