-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarketplace.py
250 lines (203 loc) · 8.79 KB
/
marketplace.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import db
import os
import sqlite3
from log import *
from settings import *
def init_db(filename):
try:
conn = sqlite3.connect(filename)
except Exception as e:
log_error(e)
return
queries = []
queries.append("CREATE TABLE {} (creator_id INTEGER, owner_id INTEGER, name TEXT, description TEXT, item_id INTEGER, creation_date TEXT)".format(ITEM_TABLE))
queries.append("CREATE TABLE {} (seller_id INTEGER, item_id INTEGER, price INTEGER, buyer_id INTEGER)".format(FOR_SALE_TABLE))
queries.append("CREATE TABLE {} (seller_id INTEGER, buyer_id INTEGER, price INTEGER, name TEXT, date TEXT)".format(TRADE_TABLE))
for q in queries:
conn.execute(q)
conn.commit()
conn.close()
class Marketplace():
"""
Interface with the database
"""
def __init__(self, db_name=DB_NAME_MARKETPLACE):
if not os.path.isfile(db_name):
init_db(db_name)
self.db = db.connect_db(db_name)
if self.db is not None:
log_info("DB {} succesfully loaded".format(db_name))
else:
log_error("Error opening marketplace DB")
def get_item_by_id(self, item_id):
query_fetch = "SELECT creator_id, owner_id, name, description, creation_date FROM {} WHERE item_id = ?".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_fetch, (item_id,))
return cur.fetchone()
def get_inventory(self, user_id):
query_inventory = "SELECT creator_id, name, description, item_id, creation_date FROM {} WHERE owner_id = ?".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_inventory, (user_id, ))
inventory = cur.fetchall()
if not inventory:
log_error("(get_inventory) No item for user_id {}".format(user_id))
return inventory
def create_item(self, user_id, name, description):
# Only one item each day per user
self.db.execute("BEGIN")
try:
today = "strftime('%Y-%m-%d', 'now', 'localtime')"
query_created_today = "SELECT * FROM {} WHERE creation_date = {} and creator_id = ?".format(ITEM_TABLE, today)
cur = self.db.cursor()
cur.execute(query_created_today, (user_id, ))
obj = cur.fetchone()
if obj is not None:
log_error("(create_item) {} is trying to create a second item today".format(user_id))
self.db.rollback()
return 0
# Compute new item_id
query_max_item_id = "SELECT MAX(item_id) FROM {}".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_max_item_id)
max_item_id = cur.fetchone()[0]
if max_item_id is None:
# First item
item_id = 0
else:
item_id = max_item_id + 1
query_create = "INSERT INTO {} (creator_id, owner_id, name, description, item_id, creation_date) VALUES (?, ?, ?, ?, ?, {})".format(ITEM_TABLE, today)
cur = self.db.cursor()
cur.execute(query_create, (user_id, user_id, name, description, item_id))
self.db.commit()
return item_id
except Exception as e:
self.db.rollback()
log_error("(send) Unknown exception in database transaction")
log_error(e)
return 0
def is_owner(self, user_id, item_id):
query_isowner = "SELECT * FROM {} WHERE owner_id = ? AND item_id = ?".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_isowner, (user_id, item_id))
obj = cur.fetchone()
if obj is None:
log_error("(is_owner) The user_id {} is not the owner of {}".format(user_id, item_id))
return False
return True
def delete_item(self, user_id, item_id):
if not self.is_owner(user_id, item_id):
return 1
if self.is_for_sale(item_id):
return 2
query_delete = "DELETE FROM {} WHERE owner_id = ? AND item_id = ?".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_delete, (user_id, item_id))
self.db.commit()
return 0
def is_for_sale(self, item_id):
query_is_for_sale = "SELECT * FROM {} WHERE item_id = ?".format(FOR_SALE_TABLE)
cur = self.db.cursor()
cur.execute(query_is_for_sale, (item_id, ))
obj = cur.fetchone()
if obj is None:
return False
return True
def sell(self, seller_id, item_id, price, buyer_id=None):
assert price >= 0
self.db.execute("BEGIN")
try:
# Check if the user has the item he's trying to sell
if not self.is_owner(seller_id, item_id):
self.db.rollback()
return 1
# Check that the item is not currently being sold
if self.is_for_sale(item_id):
self.db.rollback()
log_error("(sell) {} is already for sale".format(item_id))
return 2
query_sell = "INSERT INTO {} (seller_id, item_id, price, buyer_id) VALUES (?, ?, ?, ?)".format(FOR_SALE_TABLE)
cur = self.db.cursor()
cur.execute(query_sell, (seller_id, item_id, price, buyer_id))
self.db.commit()
return 0
except Exception as e:
self.db.rollback()
log_error("(send) Unknown exception in database transaction")
log_error(e)
return -1
def cancel_sale(self, seller_id, item_id):
if not self.is_owner(seller_id, item_id):
return 1
if not self.is_for_sale(item_id):
return 2
query_cancel = "DELETE FROM {} WHERE seller_id = ? AND item_id = ?".format(FOR_SALE_TABLE)
cur = self.db.cursor()
cur.execute(query_cancel, (seller_id, item_id))
self.db.commit()
return 0
def give(self, from_id, to_id, item_id):
self.db.execute("BEGIN")
try:
if not self.is_owner(from_id, item_id):
self.db.rollback()
return 1
if self.is_for_sale(item_id):
self.db.rollback()
return 2
query_transfer_ownership = "UPDATE {} SET owner_id = ? WHERE item_id = ?".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_transfer_ownership, (to_id, item_id))
self.db.commit()
return 0
except:
self.db.rollback()
return -1
def get_for_sale_items(self):
query_fetch = "SELECT {1}.name, {0}.item_id, {0}.price, {0}.seller_id, {0}.buyer_id FROM {0} JOIN {1} ON {0}.item_id = {1}.item_id ORDER BY {0}.price DESC".format(FOR_SALE_TABLE, ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_fetch)
return cur.fetchall()
def get_all_trades(self):
query_fetch = "SELECT seller_id, buyer_id, price, name, date FROM {}".format(TRADE_TABLE)
cur = self.db.cursor()
cur.execute(query_fetch)
trades = cur.fetchall()
return trades
def buy(self, buyer_id, item_id, bank, tax_free=False):
# Remove concurrency vulns ?
self.db.execute("BEGIN")
try:
# Check item is for sale, and for the right buyer
query_getsale = "SELECT buyer_id, seller_id, price FROM {} WHERE item_id = ?".format(FOR_SALE_TABLE)
cur = self.db.cursor()
cur.execute(query_getsale, (item_id, ))
data = cur.fetchone()
if data is None:
self.db.rollback()
return 6
if data[0] is not None and data[0] != buyer_id:
self.db.rollback()
return 7
item = self.get_item_by_id(item_id)
item_name = item[2]
# Send money
ret_val = bank.send(buyer_id, data[1], data[2], "Buy: {} ({})".format(item_name, item_id), tax_free)
if ret_val:
self.db.rollback()
return ret_val
self.cancel_sale(data[1], item_id)
# Change item owner
query_transfer_ownership = "UPDATE {} SET owner_id = ? WHERE item_id = ?".format(ITEM_TABLE)
cur = self.db.cursor()
cur.execute(query_transfer_ownership, (buyer_id, item_id))
item_name = self.get_item_by_id(item_id)[2]
query_add_trade = "INSERT INTO {} (seller_id, buyer_id, price, name, date) VALUES (?, ?, ?, ?, datetime('now', 'localtime'))".format(TRADE_TABLE)
cur = self.db.cursor()
cur.execute(query_add_trade, (data[1], buyer_id, data[2], item_name))
self.db.commit()
return 0
except Exception as e:
self.db.rollback()
log_error("(send) Unknown exception in database transaction")
log_error(e)
return -1