-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
45 lines (37 loc) · 1.06 KB
/
model.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
import json
from datetime import datetime
GUESTBOOK_ENTRIES_FILE = "entries.json"
entries = []
next_id = 0
def init():
global entries
try:
f = open(GUESTBOOK_ENTRIES_FILE)
entries = json.loads(f.read())
f.close()
except:
print('Couldn\'t open', GUESTBOOK_ENTRIES_FILE)
entries = []
def get_entries():
global entries
return entries
def add_entry(name, text):
global next_id
global entries, GUESTBOOK_ENTRIES_FILE
now = datetime.now()
time_string = now.strftime("%b %d, %Y %-I:%M %p")
next_id = next_id + 1
entry = {"author": name, "text": text, "timestamp": time_string, "id": next_id}
entries.insert(0, entry) ## add to front of list
try:
f = open(GUESTBOOK_ENTRIES_FILE, "w")
dump_string = json.dumps(entries)
f.write(dump_string)
f.close()
except:
print("ERROR! Could not write entries to file.")
def delete_entry(id):
global entries, GUESTBOOK_ENTRIES_FILE
#print(entries, GUESTBOOK_ENTRIES_FILE)
# this doesn't work
pass