-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.py
70 lines (60 loc) · 1.73 KB
/
tokens.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
import sqlite3
import uuid
# token jest aktualnie wyslany na strone
def tokenIsBeingUsed(dbfile, token):
conn = sqlite3.connect(dbfile)
c = conn.cursor()
c.execute('UPDATE tokens SET isBeingUsed=? WHERE token=?', (1, str(token)))
conn.commit()
conn.close()
def isTokenBeingUsed(dbfile, token):
conn = sqlite3.connect(dbfile)
c = conn.cursor()
c.execute('SELECT isBeingUsed FROM tokens WHERE token=?', (str(token), ))
res = c.fetchall()
if len(res) > 0:
if res[0][0] == 0:
return False
return True
# token zostal zwrocony przez strone
def tokenIsUsed(dbfile, token):
conn = sqlite3.connect(dbfile)
c = conn.cursor()
c.execute('SELECT used FROM tokens WHERE token=?', (str(token), ))
res = c.fetchall()
if len(res) > 0:
print res
if res[0][0] == 0:
print 'token jest poprawny'
c.execute('UPDATE tokens SET used=? WHERE token=?', (1, str(token)))
conn.commit()
conn.close()
return True
else:
print '\ntoken nie jest poprawny'
return False
def createTokens(dbfile):
for i in range(15):
token = str(uuid.uuid4())
conn = sqlite3.connect(dbfile)
c = conn.cursor()
c.execute('INSERT INTO tokens (token) VALUES (?)', (str(token), ))
conn.commit()
conn.close()
def getTokenFromDatabase(dbfile):
conn = sqlite3.connect(dbfile)
c = conn.cursor()
c.execute('SELECT * FROM tokens')
allTokens = c.fetchall()
res = ''
for t in allTokens:
if isTokenBeingUsed(dbfile, t) is False:
res = t[0]
if res == '':
c.execute('UPDATE tokens SET used=0')
conn.commit()
for t in allTokens:
if t[1] == 0:
res = t[0]
conn.close()
return str(res)