-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
197 lines (139 loc) · 4.68 KB
/
database.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
import sqlite3
def connect_db():
return sqlite3.connect('db.db')
def add_user(name, private_key, public_key, allowed_ips, date, usage):
try:
conn = connect_db()
cursor = conn.cursor()
cursor.execute(''' INSERT INTO users (name, private_key, public_key, allowed_ips, date, usage, used, last_used, status, percent_push, days_left_push) VALUES (?, ?, ?, ?, ?, ?, 0, 0, 1, ?, ?)''', (name, private_key, public_key, allowed_ips, date, usage, "not sent", "not sent"))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Database error: {str(e)}")
return False
def remove_user(name):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM users WHERE name = ?', (name,))
conn.commit()
conn.close()
def change_date(name, date):
try:
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET date = ? WHERE name = ?', (date, name))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Error in change_date: {e}")
if 'conn' in locals() and conn:
conn.close()
return None
def change_usage(name, usage):
try:
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET usage = ? WHERE name = ?', (usage, name))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Error in change_usage: {e}")
if 'conn' in locals() and conn:
conn.close()
return None
def get_user_by_name(name):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE name = ?', (name,))
user = cursor.fetchone()
conn.close()
return user
def get_user_by_pubkey(public_key):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE public_key = ?', (public_key,))
user = cursor.fetchone()
conn.close()
return user
def update_used(public_key, used):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET used = ? WHERE public_key = ?', (used, public_key))
conn.commit()
conn.close()
def update_last_used(public_key, last_used):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET last_used = ? WHERE public_key = ?', (last_used, public_key))
conn.commit()
conn.close()
def check_allowed_ips(ip_address):
conn = connect_db()
cursor = conn.cursor()
query = "SELECT EXISTS(SELECT 1 FROM users WHERE allowed_ips = ? LIMIT 1)"
cursor.execute(query, (ip_address,))
result = cursor.fetchone()[0] == 1
conn.close()
return result
def get_all_users():
conn = connect_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
# Fetch all the results
users = cursor.fetchall()
conn.commit()
conn.close()
return users
def change_status(name, status):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET status = ? WHERE name = ?', (status, name))
conn.commit()
conn.close()
def set_percent_push(name):
try:
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET percent_push = ? WHERE name = ?', ("sent", name))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Error in change_date: {e}")
if 'conn' in locals() and conn:
conn.close()
return None
def set_days_push(name):
try:
conn = connect_db()
cursor = conn.cursor()
cursor.execute('UPDATE users SET days_left_push = ? WHERE name = ?', ("sent", name))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Error in change_date: {e}")
if 'conn' in locals() and conn:
conn.close()
return None
def reset_push(name):
try:
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
UPDATE users
SET days_left_push = ?, percent_push = ?
WHERE name = ?
''', ("not sent", "not sent", name))
conn.commit()
cursor.close()
conn.close()
return True
except Exception as e:
print(f"Error in reset_push: {e}")
if 'conn' in locals() and conn:
conn.close()
return False