forked from FutureNathan/Habit-Calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendarDatabase.py
210 lines (184 loc) · 6.88 KB
/
calendarDatabase.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
import sqlite3
import time
import datetime
from calendar import monthrange
database_dir = '/var/Habit-Calendar/habit-calendar.db'
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
]
class Calendar:
def __init__(self):
self.conn = sqlite3.connect(database_dir, check_same_thread=False)
self.c = self.conn.cursor()
# function to create the calander table and call the reset table function to fill the table with empty values
def createCalander(self, name):
name = name.replace(" ", "_")
try:
q_string = "CREATE TABLE '{}' (count INTEGER, Jan INTEGER, Feb INTEGER, Mar INTEGER, Apr INTEGER, May INTEGER, Jun INTEGER, Jul INTEGER, Aug INTEGER, Sep INTEGER, Oct INTEGER, Nov INTEGER, Dec INTEGER )".format(name)
self.c.execute(q_string)
self.conn.commit()
self.resetTable(name)
self.addtoTaskOrder(name)
return True
except Exception as e:
print(e)
return False
# function to return the calander star data for a given calander
def getCalanderData(self, name):
q_string = "SELECT * FROM '{}'".format(name)
self.c.row_factory = None
self.c.execute(q_string)
data = self.c.fetchall()
if(data == []):
return
complete = []
for row in data:
rows = []
for col in range(1,13):
if(row[col] != None):
rows.append(row[col])
else:
rows.append(2)
complete.append(rows)
# print(complete)
return complete
# function to update a given calander by name, month and day
def updateCalander(self, name, month, day, value):
year = int(self.getSettings()[0][1])
q_string = "UPDATE '{}' SET {}={} WHERE count={}".format(name, month, value, day)
self.c.execute(q_string)
self.conn.commit()
def updateStreakDb(self, month, day):
tasks = self.getTaskOrder()
for i in tasks:
self.updateCalander(i, month, day, 9)
def getSingleEntry(self, name, month, day):
q_string = "SELECT {} FROM '{}' WHERE count = {}".format(month, name, day)
self.c.row_factory = None
self.c.execute(q_string)
data = self.c.fetchall()
if(data == []):
return
return data
# function to delete a calander table by its name
def deleteCalander(self, name):
q_string = "DROP TABLE '{}'".format(name)
self.c.execute(q_string)
self.conn.commit()
self.removeFromTaskOrder(name)
# Function that returns the names of all the calander tables in the database
def getTableNames(self):
q_string = "SELECT name FROM sqlite_master WHERE type='table';"
self.c.row_factory = None
self.c.execute(q_string)
data = self.c.fetchall()
if(data == []):
return
names = []
for name in data:
names.append(name[0])
names.remove("settings")
return names
# Function that returns the total number of calander tables in the database
def countTables(self):
q_string = "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name != 'android_metadata' AND name != 'sqlite_sequence';"
self.c.execute(q_string)
data = self.c.fetchall()
if(data == []):
return
data = int(data[0][0]) - 1
return data
# Function used by the create calander to reset a calander table with zero's
def resetTable(self, name):
year = int(self.getSettings()[0][1])
print(year)
for day in range(0,31):
row = {}
q_string = "INSERT INTO '{}' (count) VALUES({})".format(name, day+1)
self.c.execute(q_string)
self.conn.commit()
for month in range(0,12):
tot = monthrange(year, month+1)[1]
if(day < tot):
row[months[month]] = 0
# print("{} : {}".format(day+1, row))
data = self.dictToString(row)
q_string = "UPDATE '{}' SET {} WHERE count = {}".format(name, data, day+1)
self.c.execute(q_string)
self.conn.commit()
def getSettings(self):
q_string = "SELECT * FROM settings"
self.c.row_factory = None
self.c.execute(q_string)
data = self.c.fetchall()
if(data == []):
return
return data
def populateSettings(self, key, value):
q_string = "INSERT INTO settings ('key', 'value') VALUES('{}', '{}')".format(key, value)
self.c.execute(q_string)
self.conn.commit()
def updateSettings(self, key, value):
q_string = "UPDATE settings SET value = '{}' WHERE key = '{}'".format(value, key)
self.c.execute(q_string)
self.conn.commit()
def renameTaskDb(self, old_name, new_name):
q_string = "ALTER TABLE '{}' RENAME TO '{}'".format(old_name, new_name)
self.c.execute(q_string)
self.conn.commit()
def getTaskOrder(self):
data = self.getSettings()
try:
data = data[3][1]
data = data.split(' ')
if(len(data) == 1):
if(data[0] == ''):
return []
else:
return data
return data
except:
return []
def updateTaskOrder(self, order):
table_str = ""
for i in order:
if(table_str != ""):
table_str = table_str + " "+ i
else:
table_str = i
self.updateSettings("order", table_str)
def addtoTaskOrder(self, task):
old_order = self.getTaskOrder()
if(old_order != 0):
old_order.append(task)
else:
old_order = [task]
self.updateTaskOrder(old_order)
def removeFromTaskOrder(self, task):
old_order = self.getTaskOrder()
old_order.remove(task)
self.updateTaskOrder(old_order)
def getTodaysTask(self, month, day):
tables = self.getTableNames()
tasks_list = []
for table in tables:
data = self.getSingleEntry(table, month, day)[0][0]
tasks_list.append(data)
data = {
"total": len(tables),
"incomplete": tasks_list.count(0),
"complete": tasks_list.count(1) + tasks_list.count(9)
}
return data
# Function to convert dict keys and values to be embedded with the sql query used by the reset function
def dictToString(self, data):
q = ""
for i in data:
if(q != ""):
q = q + ","
q = q + i +"=" + str(data[i])
return q
def resetDB(self):
tables = self.getTableNames()
for table in tables:
self.deleteCalander(table)