Repository navigation
Expand file tree
/
Copy pathdao.py
More file actions
342 lines (303 loc) · 10.8 KB
/
Copy pathdao.py
File metadata and controls
342 lines (303 loc) · 10.8 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding:utf-8 -*-
'''
data access object
'''
import sqlite3 as sqlite
from entity import *
import util
class MasterPwdDao(object):
'''
data access object for entity Master Password
default master password: password
encrypted MD5: 5f4dcc3b5aa765d61d8327deb882cf99
'''
def __init__(self, conn):
'''
constructor
'''
self.conn = conn
def getMasterPwd(self):
'''
get master password (md5 encrypted)
'''
sql = """ SELECT md5String FROM MASTERPASSWORD """
cur = self.conn.cursor()
cur.execute(sql)
mPwd = cur.fetchone()[0]
cur.close()
return mPwd
def updateMasterPwd(self, newMD5):
'''
update master password (md5 encrypted)
'''
sql = """ UPDATE MASTERPASSWORD SET md5String=? """
cur = self.conn.cursor()
cur.execute(sql, (newMD5, ))
cur.close()
class TagDao():
'''
data access object for tag
'''
def __init__(self, conn):
'''
constructor
'''
self.conn = conn
def insertTag(self, name):
tagID = self.getAvailableTagID()
sql = """ INSERT INTO TAG (id, name) VALUES (?, ?) """
cur = self.conn.cursor()
cur.execute(sql, (tagID, name))
cur.close()
def deleteTag(self, tagID):
sql = """ DELETE FROM TAG WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (tagID, ))
cur.close()
def updateTag(self, tagID, name):
sql = """ UPDATE TAG SET name=? WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (name, tagID))
cur.close()
def getAllTags(self):
tagList = []
sql = """ SELECT id, name FROM TAG """
cur = self.conn.cursor()
cur.execute(sql)
for row in cur.fetchall():
tag = Tag()
(tag.id, tag.name) = row
tagList.append(tag)
cur.close()
return tagList
def getTagsByPwdID(self, PwdID):
tagList = []
sql = """ SELECT id, name FROM TAG WHERE id in (
SELECT tagid FROM PWDTAGJOIN WHERE pwdid=?
) GROUP BY id, name """
cur = self.conn.cursor()
cur.execute(sql, (PwdID, ))
for row in cur.fetchall():
tag = Tag()
(tag.id, tag.name) = row
tagList.append(tag)
cur.close()
return tagList
def getTagByID(self, tagID):
sql = """ SELECT id, name FROM TAG WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (tagID, ))
row = cur.fetchone()
tag = Tag()
(tag.id, tag.name) = row
cur.close()
return tag
def getPwdCntByID(self, tagID):
res = 0
sql = """ SELECT COUNT(i.pwdid) FROM PWDTAGJOIN i, ACCOUNT a
WHERE i.tagid=? AND i.pwdid=a.id AND a.deleted<>1 """
cur = self.conn.cursor()
cur.execute(sql, (tagID, ))
res = cur.fetchone()[0]
cur.close()
return res
def removeTagFromAccount(self, tagID):
sql = """ DELETE FROM PWDTAGJOIN WHERE tagid=? """
cur = self.conn.cursor()
cur.execute(sql, (tagID, ))
cur.close()
def getAvailableTagID(self):
'''
get the next available tag id
'''
sql = """ SELECT MAX(id) FROM TAG """
cur = self.conn.cursor()
cur.execute(sql)
ID = cur.fetchone()[0]
cur.close()
return ID+1 if ID != None else 1
def isTagNameValid(self, name, ID=-1):
cur = self.conn.cursor()
if ID == -1:
sql = """ SELECT id FROM TAG WHERE name=? """
cur.execute(sql, (name, ))
else:
sql = """ SELECT id FROM TAG WHERE name=? AND id<>? """
cur.execute(sql, (name, ID))
res = cur.fetchone()
cur.close()
return True if res == None else False
class PwdDao():
'''
data access object for account (password items)
'''
def __init__(self, conn):
'''
constructor
'''
self.conn = conn
def getAllPwd(self):
'''
get all undeleted account (password items)
'''
pwdList = []
sql = """ SELECT id, title, description, username, password, secret, deleted, createdate, lastupdate
FROM ACCOUNT WHERE deleted<>1 """
cur = self.conn.cursor()
cur.execute(sql)
tagDao = TagDao(self.conn)
for row in cur.fetchall():
pwd = Passwd()
(pwd.id, pwd.title, pwd.description, pwd.username, pwd.pwd, pwd.secret, pwd.deleted, pwd.createDate, pwd.lastUpdate) = row
if pwd.id != 0:
pwd.tags = tagDao.getTagsByPwdID(pwd.id)
pwdList.append(pwd)
cur.close()
return pwdList
def getPwdByID(self, pwdID):
sql = """ SELECT id, title, description, username, password, secret, deleted, createdate, lastupdate
FROM ACCOUNT WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (pwdID, ))
tagDao = TagDao(self.conn)
pwd = Passwd()
(pwd.id, pwd.title, pwd.description, pwd.username, pwd.pwd, pwd.secret, pwd.deleted, pwd.createDate, pwd.lastUpdate) = cur.fetchone()
if pwd.id != 0:
pwd.tags = tagDao.getTagsByPwdID(pwd.id)
cur.close()
return pwd
def getAllPwdCnt(self):
res = 0
sql = """ SELECT COUNT(id) FROM ACCOUNT WHERE deleted<>1 """
cur = self.conn.cursor()
cur.execute(sql)
res = cur.fetchone()[0]
cur.close()
return res
def getPwdListFromTagID(self, tagID):
pwdList = []
sql = """ SELECT id, title, description, username, password, secret, deleted, createdate, lastupdate
FROM ACCOUNT WHERE id IN (
SELECT pwdid FROM PWDTAGJOIN WHERE tagid=? AND deleted<>1 ) """
cur = self.conn.cursor()
cur.execute(sql, (tagID, ))
tagDao = TagDao(self.conn)
for row in cur.fetchall():
pwd = Passwd()
(pwd.id, pwd.title, pwd.description, pwd.username, pwd.pwd, pwd.secret, pwd.deleted, pwd.createDate, pwd.lastUpdate) = row
if pwd.id != 0:
pwd.tags = tagDao.getTagsByPwdID(pwd.id)
pwdList.append(pwd)
cur.close()
return pwdList
def updateAccount(self, id, title, description, username, password, secret, lastupdate):
sql = """ UPDATE ACCOUNT SET title=?, description=?, username=?, password=?, secret=?, lastupdate=?
WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (title, description, username, password, secret, lastupdate, id))
cur.close()
return id
def insertAccount(self, id, title, description, username, password, secret, createdate):
sql = """ INSERT INTO ACCOUNT (id, title, description, username, password, secret, createdate, lastupdate)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) """
cur = self.conn.cursor()
cur.execute(sql, (id, title, description, username, password, secret, createdate, createdate))
cur.close()
return id
def recoverFromTrash(self, pwdID):
sql = """ UPDATE ACCOUNT SET deleted=0 WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (pwdID, ))
cur.close()
def moveToTrash(self, pwdID):
sql = """ UPDATE ACCOUNT SET deleted=1 WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (pwdID, ))
cur.close()
def emptyTrash(self):
sql = """ DELETE FROM ACCOUNT WHERE deleted=1 """
cur = self.conn.cursor()
cur.execute(sql)
cur.close()
def deleteAccount(self, pwdID):
sql = """ DELETE FROM ACCOUNT WHERE id=? """
cur = self.conn.cursor()
cur.execute(sql, (pwdID, ))
cur.close()
def setTagsOnAccount(self, accountID, tagIDs):
sql = """ INSERT INTO PWDTAGJOIN (pwdid, tagid) VALUES (?, ?) """
cur = self.conn.cursor()
for tagID in tagIDs:
cur.execute(sql, (accountID, tagID))
cur.close()
def updateAccountTags(self, accountID, tagIDs):
# 1st, remove all old tags
sql = """ DELETE FROM PWDTAGJOIN WHERE pwdid=? """
cur = self.conn.cursor()
cur.execute(sql, (accountID, ))
cur.close()
# 2nd, insert tags passed in
self.setTagsOnAccount(accountID, tagIDs)
def getAvailablePwdID(self):
'''
get the next available password id
'''
sql = """ SELECT MAX(id) FROM ACCOUNT """
cur = self.conn.cursor()
cur.execute(sql)
ID = cur.fetchone()[0]
cur.close()
return ID+1 if ID != None else 1
def getSearchResult(self, keyword):
'''
username is also encrypted, so it will not be in search.
'''
pwdList = []
sql = """ SELECT id, title, description, username, password FROM ACCOUNT WHERE (
title LIKE ? OR description LIKE ?) AND deleted<>1 """
keyword = '%' + keyword + '%'
cur = self.conn.cursor()
cur.execute(sql, (keyword, keyword))
tagDao = TagDao(self.conn)
for row in cur.fetchall():
pwd = Passwd()
(pwd.id, pwd.title, pwd.description, pwd.username, pwd.pwd) = row
if pwd.id != 0:
pwd.tags = tagDao.getTagsByPwdID(pwd.id)
pwdList.append(pwd)
cur.close()
return pwdList
def getPwdListInTrash(self):
pwdList = []
sql = """ SELECT id, title, description, username, password, deleted, createdate, lastupdate
FROM ACCOUNT WHERE deleted=1 """
cur = self.conn.cursor()
cur.execute(sql)
tagDao = TagDao(self.conn)
for row in cur.fetchall():
pwd = Passwd()
(pwd.id, pwd.title, pwd.description, pwd.username, pwd.pwd, pwd.deleted, pwd.createDate, pwd.lastUpdate) = row
if pwd.id != 0:
pwd.tags = tagDao.getTagsByPwdID(pwd.id)
pwdList.append(pwd)
cur.close()
return pwdList
def getPwdCntInTrash(self):
res = 0
sql = """ SELECT COUNT(id) FROM ACCOUNT WHERE deleted=1 """
cur = self.conn.cursor()
cur.execute(sql)
res = cur.fetchone()[0]
cur.close()
return res
def isTitleNameValid(self, name, ID=-1):
cur = self.conn.cursor()
if ID == -1:
sql = """ SELECT id FROM ACCOUNT WHERE title=? """
cur.execute(sql, (name, ))
else:
sql = """ SELECT id FROM ACCOUNT WHERE title=? AND id<>? """
cur.execute(sql, (name, ID))
res = cur.fetchone()
cur.close()
return True if res == None else False