-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
83 lines (68 loc) · 1.99 KB
/
model.py
File metadata and controls
83 lines (68 loc) · 1.99 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
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
import json
from datetime import datetime
GUESTBOOK_ENTRIES_FILE = "entries.json"
entries = []
next_id = 0
def init():
global entries
global next_id
try:
f = open(GUESTBOOK_ENTRIES_FILE)
entries = json.loads(f.read())
f.close()
id_list = []
for a in entries:
id_list.append(a['id'])
max_id = 0
for a in is_list:
if a > max_id:
max_id=a
next_id = max_id+1
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("%Y-%m-%d %H:%M:%S")
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 change_entry(text,id_):
global entries, GUESTBOOK_ENTRIES_FILE,next_id
now = datetime.now()
time_string = now.strftime("%Y-%m-%d %H:%M:%S")
for entry in entries:
if entry['id'] ==id_:
entry['text'] = text
entry['timestamp'] = time_string
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
for entry in entries:
###==
if entry['id'] == id_:
entries.remove(entry)
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.")