-
Notifications
You must be signed in to change notification settings - Fork 0
/
diary.py
101 lines (75 loc) · 2.19 KB
/
diary.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
from collections import OrderedDict
import sys
import os
from datetime import datetime
from peewee import *
db = SqliteDatabase('diary.db')
class Entry(Model):
content = TextField()
timestamp = DateTimeField(default = datetime.now)
class Meta:
database = db
def initialize():
"""Create the database if the don't exist."""
db.connect()
db.create_tables([Entry], safe=True)
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def menu_loop():
"""Show the menu"""
choice = None
while choice != 'q':
clear()
print("Enter 'q to the quit'")
for k,v in menu.items():
print('{} {}'.format(k,v.__doc__))
choice = input('Action:').lower().strip()
if choice in menu:
clear()
menu[choice]()
def add_entry():
"""Add an Entry"""
print('Enter your entry. Press "ctrl+d" when finished!')
data = sys.stdin.read().strip()
if data:
if input('Save Entry [Y/N]').lower() != 'n':
Entry.create(content=data)
print("Saved Successfully")
def view_entry(search_query=None):
"""View Previous Entries"""
entries = Entry.select().order_by(Entry.timestamp.desc())
if search_query:
entries = entries.where(Entry.content.contains(search_query))
for entry in entries:
timestamp = entry.timestamp.strftime('%A %B %d, %H:%M%p')
time_str = "This Entry was made at:{}".format(timestamp)
clear()
print(time_str)
print('=' * len(time_str))
print(entry.content)
print('\n\n'+'='*len(time_str))
print('n) next entry')
print('d) delete entry')
print('q) return to the main menu')
next_action = input('Action: [N\q]').lower().strip()
if next_action == 'q':
break
elif next_action == 'd':
delete_entry(entry)
def search_entry():
"""Search entries for a string."""
view_entry(input('Search query: '))
pass
def delete_entry(entry):
"""Delete an Entry"""
if input('Are you sure? [y/n] ').lower() == 'y':
entry.delete_instance()
print('Entry deleted!')
menu = OrderedDict([
('a', add_entry),
('v', view_entry),
('s', search_entry)
])
if __name__ == '__main__':
initialize()
menu_loop()