-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
40 lines (33 loc) · 968 Bytes
/
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
import json
from datetime import datetime
GUESTBOOK_ENTRIES_FILE = "entries.json"
entries = []
next_id = 0
def init(app):
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 entries, GUESTBOOK_ENTRIES_FILE, next_id
now = datetime.now()
time_string = now.strftime("%b %d, %Y %-I:%M %p")
entry = {"author": name, "text": text, "timestamp": time_string, "id":str(next_id)}
next_id = next_id + 1
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():
pass