Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 8e46644

Browse files
committed
Create audit db schema additions
1 parent de9a97f commit 8e46644

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

db/datastore.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import sqlite3 as lite
5+
import time
56
from api.utils import sanitize_html
67
from collections import Counter
78
from config import DATA_FOLDER
@@ -10,14 +11,14 @@
1011
from protos import objects
1112
from protos.objects import Listings, Followers, Following
1213
from os.path import join
13-
from db.migrations import migration1, migration2, migration3, migration4, migration5
14+
from db.migrations import migration1, migration2, migration3, migration4, migration5, migration6
1415

1516

1617
class Database(object):
1718

1819
__slots__ = ['PATH', 'filemap', 'profile', 'listings', 'keys', 'follow', 'messages',
1920
'notifications', 'broadcasts', 'vendors', 'moderators', 'purchases', 'sales',
20-
'cases', 'ratings', 'transactions', 'settings']
21+
'cases', 'ratings', 'transactions', 'settings', 'audit_shopping']
2122

2223
def __init__(self, testnet=False, filepath=None):
2324
object.__setattr__(self, 'PATH', self._database_path(testnet, filepath))
@@ -37,6 +38,7 @@ def __init__(self, testnet=False, filepath=None):
3738
object.__setattr__(self, 'ratings', Ratings(self.PATH))
3839
object.__setattr__(self, 'transactions', Transactions(self.PATH))
3940
object.__setattr__(self, 'settings', Settings(self.PATH))
41+
object.__setattr__(self, 'audit_shopping', ShoppingEvents(self.PATH))
4042

4143
self._initialize_datafolder_tree()
4244
self._initialize_database(self.PATH)
@@ -116,7 +118,7 @@ def _create_database(database_path):
116118
conn = lite.connect(database_path)
117119
cursor = conn.cursor()
118120

119-
cursor.execute('''PRAGMA user_version = 5''')
121+
cursor.execute('''PRAGMA user_version = 6''')
120122
cursor.execute('''CREATE TABLE hashmap(hash TEXT PRIMARY KEY, filepath TEXT)''')
121123

122124
cursor.execute('''CREATE TABLE profile(id INTEGER PRIMARY KEY, serializedUserInfo BLOB, tempHandle TEXT)''')
@@ -174,6 +176,16 @@ def _create_database(database_path):
174176
smtpNotifications INTEGER, smtpServer TEXT, smtpSender TEXT, smtpRecipient TEXT, smtpUsername TEXT,
175177
smtpPassword TEXT)''')
176178

179+
cursor.execute('''CREATE TABLE IF NOT EXISTS audit_shopping (
180+
audit_shopping_id integer PRIMARY KEY NOT NULL,
181+
shopper_guid text NOT NULL,
182+
contract_hash text NOT NULL,
183+
"timestamp" integer NOT NULL,
184+
action_id integer NOT NULL
185+
);''')
186+
cursor.execute('''CREATE INDEX IF NOT EXISTS shopper_guid_index ON audit_shopping (audit_shopping_id ASC);''')
187+
cursor.execute('''CREATE INDEX IF NOT EXISTS action_id_index ON audit_shopping (audit_shopping_id ASC);''')
188+
177189
conn.commit()
178190
conn.close()
179191

@@ -190,20 +202,27 @@ def _run_migrations(self):
190202
migration3.migrate(self.PATH)
191203
migration4.migrate(self.PATH)
192204
migration5.migrate(self.PATH)
205+
migration6.migrate(self.PATH)
193206
elif version == 1:
194207
migration2.migrate(self.PATH)
195208
migration3.migrate(self.PATH)
196209
migration4.migrate(self.PATH)
197210
migration5.migrate(self.PATH)
211+
migration6.migrate(self.PATH)
198212
elif version == 2:
199213
migration3.migrate(self.PATH)
200214
migration4.migrate(self.PATH)
201215
migration5.migrate(self.PATH)
216+
migration6.migrate(self.PATH)
202217
elif version == 3:
203218
migration4.migrate(self.PATH)
204219
migration5.migrate(self.PATH)
220+
migration6.migrate(self.PATH)
205221
elif version == 4:
206222
migration5.migrate(self.PATH)
223+
migration6.migrate(self.PATH)
224+
elif version == 5:
225+
migration6.migrate(self.PATH)
207226

208227

209228
class HashMap(object):
@@ -1333,3 +1352,39 @@ def get_credentials(self):
13331352
ret = cursor.fetchone()
13341353
conn.close()
13351354
return ret
1355+
1356+
class ShoppingEvents(object):
1357+
"""
1358+
Stores audit events for shoppers on your storefront
1359+
"""
1360+
1361+
def __init__(self, database_path):
1362+
self.PATH = database_path
1363+
1364+
def set(self, shopper_guid, action_id, contract_hash=None):
1365+
conn = Database.connect_database(self.PATH)
1366+
with conn:
1367+
cursor = conn.cursor()
1368+
timestamp = int(time.time())
1369+
if not contract_hash:
1370+
contract_hash = ''
1371+
cursor.execute('''INSERT INTO audit_shopping(shopper_guid, timestamp, contract_hash, action_id) VALUES
1372+
(?,?,?,?)''', (shopper_guid, timestamp, contract_hash, action_id))
1373+
conn.commit()
1374+
conn.close()
1375+
1376+
def get(self):
1377+
conn = Database.connect_database(self.PATH)
1378+
cursor = conn.cursor()
1379+
cursor.execute('''SELECT * FROM audit_shopping''')
1380+
ret = cursor.fetchall()
1381+
conn.close()
1382+
return ret
1383+
1384+
def get_events_by_id(self, event_id):
1385+
conn = Database.connect_database(self.PATH)
1386+
cursor = conn.cursor()
1387+
cursor.execute('''SELECT * FROM audit_shopping WHERE event_id=?''', event_id)
1388+
ret = cursor.fetchall()
1389+
conn.close()
1390+
return ret

db/migrations/migration6.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sqlite3
2+
3+
4+
def migrate(database_path):
5+
print "migrating to db version 6"
6+
conn = sqlite3.connect(database_path)
7+
conn.text_factory = str
8+
cursor = conn.cursor()
9+
10+
# create new table
11+
cursor.execute('''CREATE TABLE IF NOT EXISTS audit_shopping (
12+
audit_shopping_id integer PRIMARY KEY NOT NULL,
13+
shopper_guid text NOT NULL,
14+
contract_hash text,
15+
"timestamp" integer NOT NULL,
16+
action_id integer NOT NULL
17+
);''')
18+
cursor.execute('''CREATE INDEX IF NOT EXISTS shopper_guid_index ON audit_shopping (audit_shopping_id ASC);''')
19+
cursor.execute('''CREATE INDEX IF NOT EXISTS action_id_index ON audit_shopping (audit_shopping_id ASC);''')
20+
21+
# update version
22+
cursor.execute('''PRAGMA user_version = 6''')
23+
conn.commit()
24+
conn.close()

0 commit comments

Comments
 (0)