22
33import os
44import sqlite3 as lite
5+ import time
56from api .utils import sanitize_html
67from collections import Counter
78from config import DATA_FOLDER
1011from protos import objects
1112from protos .objects import Listings , Followers , Following
1213from 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
1617class 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
209228class 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
0 commit comments