-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
55 lines (48 loc) · 1.43 KB
/
model.py
File metadata and controls
55 lines (48 loc) · 1.43 KB
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
import json
from datetime import datetime
GUESTBOOK_ENTRIES_FILE = "entries.json"
entries = []
next_id = 0
def init(app):
global entries, next_id
try:
f = open(GUESTBOOK_ENTRIES_FILE)
entries = json.loads(f.read())
f.close()
max_id = 0
for e in entries:
if 'id' in e and int(e['id']) >= max_id:
max_id = int(e['id']) + 1
next_id = max_id
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 %H:%M %p")
entry = {"author": name, "text": text, "timestamp": time_string, "id": str(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(idnum):
global entries, GUESTBOOK_ENTRIES_FILE
for e in entries:
if e['id'] == idnum:
entries.remove(e)
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.")