-
Notifications
You must be signed in to change notification settings - Fork 0
/
entries.py
252 lines (225 loc) · 8.87 KB
/
entries.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import os
import sqlite3
import uuid
import json
import smugmug
import datetime
from tasks import Tasks, Task, TaskStages
from members import Members, Member
SCHEMA_VERSION = 9
class Entry():
def __init__(self, date, taskName, memberName, hours, accomplished, why,
learned, nextSteps, notes, diagram, photoLink, imgKey):
self.date = date
self.taskName = taskName
self.memberName = memberName
self.accomplished = accomplished
self.hours = hours
self.why = why
self.learned = learned
self.nextSteps = nextSteps
self.notes = notes
self.diagram = diagram
self.photoLink = photoLink
self.imgKey = imgKey
def __str__(self):
return f"Date: {self.date} Task: {self.taskName} Member: {self.memberName}"
def getPhotoLink(self, config):
return smugmug.get_medium_link(self.imgKey, config)
class Entries():
def __init__(self):
self.tasks = Tasks()
self.members = Members()
self.SCHEMA_VERSION = SCHEMA_VERSION
def createTable(self, dbConnection):
dbConnection.execute("""
CREATE TABLE Entries(
id INTEGER PRIMARY KEY,
date TEXT NOT NULL,
task_id INTEGER NOT NULL,
member_id INTEGER NOT NULL,
hours REAL,
accomplished TEXT,
why TEXT,
learned TEXT,
notes TEXT,
diagramDot TEXT,
next_steps TEXT,
photo_link TEXT,
imgKey TEXT)""")
self.tasks.createTable(dbConnection)
self.members.createTable(dbConnection)
dbConnection.execute('PRAGMA schema_version = ' + str(SCHEMA_VERSION))
def addEntry(self, dbConnection, date, taskId, memberId, hours,
accomplished, nextSteps, notes, diagram, photo,
smugmugConfig):
dbConnection.execute(
"Insert INTO Entries (date, task_id, member_id, hours,accomplished, next_steps, notes, diagram, imgKey) VALUES (?,?,?,?,?,?,?,?,?)",
(date, taskId, memberId, hours, accomplished, nextSteps, notes,
diagram, photo))
def migrate(self, dbConnection, dbSchemaVersion):
if dbSchemaVersion > SCHEMA_VERSION:
raise Exception("Unknown DB schema version" + str(dbSchemaVersion))
if dbSchemaVersion < 4:
dbConnection.execute("ALTER TABLE Entries ADD why TEXT")
if dbSchemaVersion < 5:
dbConnection.execute("ALTER TABLE Entries ADD notes TEXT")
if dbSchemaVersion < 6:
dbConnection.execute("ALTER TABLE Entries ADD diagramDot TEXT")
if dbSchemaVersion < 8:
dbConnection.execute("ALTER TABLE Entries ADD hours REAL ")
if dbSchemaVersion < 9:
dbConnection.execute(
"ALTER TABLE Entries RENAME COLUMN diagramDot TO diagram")
self.tasks.migrate(dbConnection, dbSchemaVersion)
self.members.migrate(dbConnection, dbSchemaVersion)
dbConnection.execute('PRAGMA schema_version = ' + str(SCHEMA_VERSION))
def getDateList(self, dbConnection):
dateList = []
for row in dbConnection.execute(
"SELECT DISTINCT date FROM Entries ORDER BY date DESC"):
dateList.append(row[0])
return dateList
def getPrevNext(self, dbConnection, dateString):
dateList = self.getDateList(dbConnection)
i = dateList.index(dateString)
if i == len(dateList) - 1:
prev = ""
else:
prev = dateList[i + 1]
if i == 0:
next = ""
else:
next = dateList[i - 1]
return (prev, next)
def updateSmugmugLink(self, dbConnection, smugmugConfig, entryID, imgKey):
photoLink = smugmug.get_medium_link(imgKey, smugmugConfig)
dbConnection.execute(
"""
UPDATE Entries
SET photo_link = ?
WHERE id = ?
""", (photoLink, entryID))
return photoLink
def getDateTasksDictionary(self, dateStr, dbConnection, smugmugConfig):
entryDict = {}
for row in dbConnection.execute(
"""
SELECT tasks.name, members.name, accomplished, why, learned, next_steps, notes, diagram, photo_link, imgkey, Entries.id, hours
FROM Entries
INNER JOIN tasks
ON Entries.task_id = Tasks.id
INNER JOIN members
ON Entries.member_id = Members.id
WHERE (date = ?)
""", (dateStr, )):
taskName = row[0]
memberName = row[1]
accomplished = row[2]
why = row[3]
learned = row[4]
next_steps = row[5]
notes = row[6]
diagram = row[7]
photoLink = row[8]
imgKey = row[9]
entriesId = row[10]
hours = row[11]
if not (photoLink):
if imgKey:
photoLink = self.updateSmugmugLink(dbConnection,
smugmugConfig,
entriesId, imgKey)
newEntry = Entry(dateStr, taskName, memberName, hours,
accomplished, why, learned, next_steps, notes,
diagram, photoLink, imgKey)
if not (row[0] in entryDict.keys()):
entryDict[row[0]] = [newEntry]
else:
entryDict[row[0]].append(newEntry)
return entryDict
def getEntriesWithHours(self, dbConnection):
entries = []
for row in dbConnection.execute(
"""
SELECT tasks.name, members.name, date, hours
FROM Entries
INNER JOIN tasks
ON Entries.task_id = Tasks.id
INNER JOIN members
ON Entries.member_id = Members.id
WHERE (hours > 0) ORDER BY date ASC
""", ()):
entries.append(
Entry(date=row[2],
taskName=row[0],
memberName=row[1],
hours=row[3],
accomplished="",
why="",
learned="",
nextSteps="",
notes="",
diagram="",
photoLink="",
imgKey=""))
return entries
def getDateDictionary(self, taskId, dbConnection, smugmugConfig):
entryDict = {}
taskName = ""
for row in dbConnection.execute(
"""
SELECT tasks.name, members.name, accomplished, photo_link, imgkey, Entries.id, date, why, notes, diagram
FROM Entries
INNER JOIN tasks
ON Entries.task_id = Tasks.id
INNER JOIN members
ON Entries.member_id = Members.id
WHERE (Tasks.id = ?) ORDER BY date ASC
""", (taskId, )):
taskName = row[0]
photoLink = row[3]
imgKey = row[4]
if not (photoLink):
if imgKey:
photoLink = self.updateSmugmugLink(dbConnection,
smugmugConfig, row[5],
imgKey)
newEntry = Entry(date=row[6],
taskName=taskName,
memberName=row[1],
hours=0,
accomplished=row[2],
learned="",
nextSteps="",
photoLink=photoLink,
imgKey=imgKey,
why=row[7],
notes=row[8],
diagram=row[9])
if not (row[6] in entryDict.keys()):
entryDict[row[6]] = [newEntry]
else:
entryDict[row[6]].append(newEntry)
return (entryDict, taskName)
if __name__ == "__main__":
DEFAULT_PATH = os.path.join(os.path.dirname(__file__),
'data/testDatabase.sqlite3')
# try:
# os.remove(DEFAULT_PATH)
# except IOError:
# pass #delete File, if it doesn't exist we don't care
"""
with sqlite3.connect(DEFAULT_PATH) as connection:
entries = Entries()
#entries.createTable(connection)
entries.addEntry(connection, datetime.datetime.now(), 2, 1, "Hi", "Saying Hi is fun", "Say Hi to more people", "")
entriesDict = entries.getDateTasksDictionary(datetime.datetime.now().strftime("%Y-%m-%d"), connection)
for k, v in entriesDict.items():
print(k + ":")
for entry in v:
print("-", entry)
"""
# if __name__ == "__main__":
# Entry = Minutes('data/9.8.2019.yaml')
# Entry.toHtml()