-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
259 lines (218 loc) · 9.66 KB
/
api.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
253
254
255
256
257
258
import os
import sqlite3
from datetime import datetime
class Database():
def __init__(self):
self.conn = sqlite3.connect('./licenses.db')
self.c = self.conn.cursor()
def create(self):
# creates and configues the database in the case it is empty or does not exist
if os.path.getsize('licenses.db') != 0:
return 'DB File already exists and has already been created.'
else:
self.c.execute('CREATE TABLE users (username text PRIMARY KEY, fName text, sName text, emailAddress text, password text, admin bool)')
self.c.execute('CREATE TABLE licenses (license text PRIMARY KEY, username text, boundToUser boolean, boundToDevice boolean, HWID string, devicename string, nextrenewal string, plan string, stripeSessionID string)')
self.c.execute('CREATE TABLE plans (name text PRIMARY KEY, interval integer, amount float)')
self.conn.commit()
return 'Created DB file'
def closeConnection(self):
# closes connection
# necessary to be called as it is important to refrain from concurrent database connections where possible
self.conn.close()
return
# --- non specific functions ---
def getCountofTable(self, table):
self.c.execute(f'''SELECT COUNT(*) FROM {table}''')
result = self.c.fetchone()
return result[0]
def getConditionalCountofTable(self, table, column, condition):
self.c.execute(f'''SELECT COUNT(*) FROM {table} WHERE {column} = {condition}''')
result = self.c.fetchone()
return result[0]
def getMostPopular(self,table, column):
self.c.execute(f'''SELECT {column}, COUNT({column}) AS value_occurance FROM {table} GROUP BY {column} ORDER BY value_occurance DESC LIMIT 1;''')
result = self.c.fetchone()
return result
# --- user related functions ---
def getAll(self, dbname):
self.c.execute(f'''SELECT * FROM {dbname}''')
result = self.c.fetchall()
return result
def searchUsersByUsername(self, user):
self.c.execute(f'''SELECT * FROM users WHERE username = ?''', (user,))
result = self.c.fetchone()
return result
def addToUsers(self, values):
if not self.searchUsersByUsername(tuple(values.split(','))[0]):
self.c.execute(f'''INSERT INTO users(username,fName,sName,emailAddress,password,admin)
VALUES(?, ?, ?, ?, ?, ?)''', tuple(values.split(',')))
self.conn.commit()
return
else:
return 'user already exists'
def updateUser(self, param, value, username):
self.c.execute(f'''UPDATE users SET {param} = "{value}" WHERE username = "{username}";''')
self.conn.commit()
return
def searchUsers(self, email, user):
self.c.execute(f'''SELECT username, emailAddress FROM users WHERE emailAddress = ? OR username = ?''',
(email, user))
result = self.c.fetchone()
return result
def deleteUser(self, user):
self.c.execute('''DELETE FROM users WHERE username = ?;''', (user,))
self.conn.commit()
return
# --- license related functions ---
def getLicenseInfo(self, license):
self.c.execute(f'''SELECT * FROM licenses WHERE license = ?''',
(license,))
result = self.c.fetchone()
return result
def checkIfLicenseExists(self, license):
self.c.execute(f'''SELECT * FROM licenses WHERE license = ?''',
(license,))
result = self.c.fetchone()
return bool(result)
def checkIfLicenseBound(self, license):
self.c.execute(f'''SELECT boundToUser FROM licenses WHERE license = ?''',
(license,))
result = self.c.fetchone()
if not result[0]:
return False
else:
return True
def checkIfUserHasLicense(self, username):
self.c.execute(f'''SELECT license FROM licenses WHERE username = ?''',
(username,))
result = self.c.fetchone()
if result:
return result[0]
else:
return False
def getUserbyLicense(self, license):
self.c.execute(f'''SELECT username FROM licenses WHERE license = ?''',
(license,))
result = self.c.fetchone()
if result:
return result[0]
else:
return None
def commitLicense(self, license, plan):
self.c.execute(
f'''INSERT INTO licenses(license,username,boundtoUser,boundtoDevice,HWID,devicename,nextrenewal,plan,stripeSessionID)
VALUES(?, NULL, FALSE, FALSE, NULL, NULL, NULL, ?, "placeholder")''',
(license, plan)) # needs to be updated to include plan, and validate that plane xists etc.
self.conn.commit()
return
def setLicenseToUnbound(self, license):
self.c.execute(
f'''UPDATE licenses SET username = NULL, boundtoUser = False, boundtoDevice = False, HWID = NULL, devicename = NULL WHERE license = ?;''',
(license,))
self.conn.commit()
return
def setLicenseToUnboundDEVICE(self, license):
self.c.execute(
f'''UPDATE licenses SET boundtoDevice = False, HWID = NULL, devicename = NULL WHERE license = ?;''',
(license,))
self.conn.commit()
return
def setLicenseHWIDandDevice(self, license, hwid, devicename):
self.c.execute(
f'''UPDATE licenses SET boundtoDevice = True, HWID = ?, devicename = ? WHERE license = ?;''',
(hwid, devicename, license,))
self.conn.commit()
return
def updateNextRenewal(self, license, date):
self.c.execute(f'''UPDATE licenses SET nextrenewal = ? WHERE license = ?;''',
(date, license))
self.conn.commit()
return
def updateLicenseKey(self, newlicense, oldlicense):
self.c.execute(f'''UPDATE licenses SET license = ? WHERE license = ?;''',
(newlicense, oldlicense))
self.conn.commit()
return
def getNextRenewal(self, license):
self.c.execute(f'''SELECT nextrenewal FROM licenses WHERE license = ?''', (license,))
result = self.c.fetchone()[0]
if not result or result == "NULL":
return None
else:
return datetime.strptime(result, '%Y-%m-%d %H:%M:%S.%f')
def getLicenseStripeSessionID(self, license):
self.c.execute(f'''SELECT stripeSessionID FROM licenses WHERE license = ?''', (license,))
result = self.c.fetchone()[0]
return result
def getAllLicenseWithRenewal(self):
self.c.execute(f'''SELECT license, nextrenewal FROM licenses WHERE nextrenewal != "None"''')
result = self.c.fetchall()
renewaldict = {}
for value in result:
renewaldict[value[0]] = datetime.strptime(value[1], '%Y-%m-%d %H:%M:%S.%f')
return renewaldict
def bindUsertoLicense(self, license, username):
# attemps to bind a license to a certain user given the license is valid.
if self.checkIfLicenseExists(license):
if not self.checkIfLicenseBound(license):
if not self.checkIfUserHasLicense(username):
self.c.execute(f'''UPDATE licenses SET boundtoUser = TRUE, username = ? WHERE license = ?;''',
(username, license))
print(f'bound {license} to {username}')
self.conn.commit()
return 'success'
else:
return f'User already has license {self.checkIfUserHasLicense(username)}'
else:
return f'That license is already bound to another user'
else:
return 'License doesnt exist'
def getPlanfromLicense(self, license):
self.c.execute(
'''SELECT plans.* FROM plans JOIN licenses ON licenses.plan = plans.name WHERE licenses.license = ?;''',
(license,))
result = self.c.fetchone()
resultdict = {"name": result[0],
"renewalinterval": result[1],
"renewalprice": result[2]}
return resultdict
def getLicensesfromPlan(self, plan):
self.c.execute(
'''SELECT license FROM licenses where plan =?;''',
(plan,))
result = self.c.fetchall()
return result
def findBoundLicensesOfGivenPlan(self, plan):
self.c.execute(
'''SELECT license FROM licenses where plan =? and boundToUser = 1;''',
(plan,))
result = self.c.fetchall()
return result
def deleteLicensesOfGivenPlan(self, plan):
self.c.execute('''DELETE FROM licenses WHERE plan = ?;''', (plan,))
self.conn.commit()
return
def deleteLicense(self, license):
self.c.execute('''DELETE FROM licenses WHERE license = ?;''', (license,))
self.conn.commit()
return
# --- plan related functions ---
def getPlanInfo(self, name):
self.c.execute(f'''SELECT * FROM plans WHERE name = "{name}";''')
result = self.c.fetchone()
if not result:
return None
else:
return result
def createPlan(self, name, interval, amount):
if not self.getPlanInfo(name):
self.c.execute(f'''INSERT INTO plans(name,interval,amount)
VALUES(?, ?, ?)''', (name, interval, amount))
self.conn.commit()
return
else:
return 'Plan already exists'
def deletePlan(self, plan):
self.c.execute('''DELETE FROM plans WHERE name = ?;''', (plan,))
self.conn.commit()
return