forked from des-labs/des_ncsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
133 lines (123 loc) · 3.23 KB
/
db.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
import sqlite3
import uuid
import os
from global_vars import log
class DbInterface:
def __init__(self):
self.db = os.path.join('/db', 'db.sqlite3')
log.debug(self.db)
self.conn = None
self.cur = None
# Initialize the monitor SQLite database
self.open_db()
self.cur.execute('''
CREATE TABLE IF NOT EXISTS HELP_REQUESTS (
id INTEGER PRIMARY KEY,
time_created TEXT,
token TEXT,
email TEXT,
last_name TEXT,
first_name TEXT,
subject TEXT,
message TEXT,
topics TEXT,
received INTEGER
)'''
)
self.close_db()
def open_db(self):
self.conn = sqlite3.connect(self.db)
self.cur = self.conn.cursor()
def close_db(self):
self.conn.commit()
self.conn.close()
def generate_uuid(self):
return str(uuid.uuid4()).replace("-", "")
def add_new_request(self, form_data):
token = self.generate_uuid()
self.open_db()
self.cur.execute(f'''
INSERT INTO HELP_REQUESTS (
token,
time_created,
email,
last_name,
first_name,
subject,
message,
topics,
received
) VALUES(
:token,
datetime('now'),
:email,
:last_name,
:first_name,
:subject,
:message,
:topics,
0
)
''',
{
'token': token,
'email': form_data['email'],
'last_name': form_data['last_name'],
'first_name': form_data['first_name'],
'subject': form_data['subject'],
'message': form_data['message'],
'topics': form_data['topics'],
})
self.close_db()
return token
def get_request_data(self, token):
self.open_db()
self.cur.execute(f'''
SELECT
token,
email,
last_name,
first_name,
subject,
message,
topics,
received
FROM
HELP_REQUESTS
WHERE
token = :token
''',
{
'token': token,
})
results = self.cur.fetchall()
self.close_db()
return results
def delete_request(self, token):
self.open_db()
self.cur.execute(f'''
DELETE FROM
HELP_REQUESTS
WHERE
token = :token
''',
{
'token': token,
})
self.close_db()
return self.cur.rowcount == 1
def mark_received(self, token):
self.open_db()
self.cur.execute(f'''
UPDATE
HELP_REQUESTS
SET
received = 1
WHERE
token = :token
''',
{
'token': token,
})
self.close_db()
return self.cur.rowcount == 1