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 , migration6
14+ from db .migrations import migration1 , migration2 , migration3 , migration4 , migration5 , migration6 , migration7
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 )
@@ -174,6 +176,17 @@ 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
187+ (audit_shopping_id ASC);''' )
188+ cursor .execute ('''CREATE INDEX IF NOT EXISTS action_id_index ON audit_shopping (audit_shopping_id ASC);''' )
189+
177190 conn .commit ()
178191 conn .close ()
179192
@@ -190,23 +203,30 @@ def _run_migrations(self):
190203 migration3 .migrate (self .PATH )
191204 migration4 .migrate (self .PATH )
192205 migration5 .migrate (self .PATH )
206+ migration6 .migrate (self .PATH )
193207 elif version == 1 :
194208 migration2 .migrate (self .PATH )
195209 migration3 .migrate (self .PATH )
196210 migration4 .migrate (self .PATH )
197211 migration5 .migrate (self .PATH )
212+ migration6 .migrate (self .PATH )
198213 elif version == 2 :
199214 migration3 .migrate (self .PATH )
200215 migration4 .migrate (self .PATH )
201216 migration5 .migrate (self .PATH )
217+ migration6 .migrate (self .PATH )
202218 elif version == 3 :
203219 migration4 .migrate (self .PATH )
204220 migration5 .migrate (self .PATH )
221+ migration6 .migrate (self .PATH )
205222 elif version == 4 :
206223 migration5 .migrate (self .PATH )
207224 migration6 .migrate (self .PATH )
208225 elif version == 5 :
209226 migration6 .migrate (self .PATH )
227+ migration7 .migrate (self .PATH )
228+ elif version == 6 :
229+ migration7 .migrate (self .PATH )
210230
211231
212232class HashMap (object ):
@@ -1360,3 +1380,39 @@ def get_credentials(self):
13601380 ret = cursor .fetchone ()
13611381 conn .close ()
13621382 return ret
1383+
1384+ class ShoppingEvents (object ):
1385+ """
1386+ Stores audit events for shoppers on your storefront
1387+ """
1388+
1389+ def __init__ (self , database_path ):
1390+ self .PATH = database_path
1391+
1392+ def set (self , shopper_guid , action_id , contract_hash = None ):
1393+ conn = Database .connect_database (self .PATH )
1394+ with conn :
1395+ cursor = conn .cursor ()
1396+ timestamp = int (time .time ())
1397+ if not contract_hash :
1398+ contract_hash = ''
1399+ cursor .execute ('''INSERT INTO audit_shopping(shopper_guid, timestamp, contract_hash, action_id) VALUES
1400+ (?,?,?,?)''' , (shopper_guid , timestamp , contract_hash , action_id ))
1401+ conn .commit ()
1402+ conn .close ()
1403+
1404+ def get (self ):
1405+ conn = Database .connect_database (self .PATH )
1406+ cursor = conn .cursor ()
1407+ cursor .execute ('''SELECT * FROM audit_shopping''' )
1408+ ret = cursor .fetchall ()
1409+ conn .close ()
1410+ return ret
1411+
1412+ def get_events_by_id (self , event_id ):
1413+ conn = Database .connect_database (self .PATH )
1414+ cursor = conn .cursor ()
1415+ cursor .execute ('''SELECT * FROM audit_shopping WHERE event_id=?''' , event_id )
1416+ ret = cursor .fetchall ()
1417+ conn .close ()
1418+ return ret
0 commit comments