Permalink
Browse files

wtf

  • Loading branch information...
1 parent bb4d2f8 commit f0c8188c23799f20584d4b7d4c4615be7e6f9792 @klange klange committed Sep 8, 2011
View
40 vending/Db.py
@@ -17,30 +17,45 @@ def __init__(self):
log(Log.Info, "db", "Using MySQL backend.")
self.user_database = None #: Server connection set to a database for user data
self.vend_database = None #: Server connection set to a database for vending data, like sodas
+ self.connected = False
def start(self):
"""
Connect to the MySQL databases.
"""
- log(Log.Info, "db-mysql", "Connecting to MySQL 'user' server (%s)." % getConfig("db_mysql_user_server"))
+ self.connect()
+ self.close()
+ log(Log.Notice, "db-mysql", "MySQL is ready.")
+ def connect(self):
+ if self.connected:
+ return
+ log(Log.Verbose, "db-mysql", "Connecting to MySQL 'user' server (%s)." % getConfig("db_mysql_user_server"))
self.user_database = MySQLdb.connect(getConfig("db_mysql_user_server"), getConfig("db_mysql_user_user"), getConfig("db_mysql_user_password"))
- log(Log.Info, "db-mysql", "Connecting to MySQL 'vend' server (%s)." % getConfig("db_mysql_vend_server"))
+ log(Log.Verbose, "db-mysql", "Connecting to MySQL 'vend' server (%s)." % getConfig("db_mysql_vend_server"))
self.vend_database = MySQLdb.connect(getConfig("db_mysql_vend_server"), getConfig("db_mysql_vend_user"), getConfig("db_mysql_vend_password"))
- log(Log.Info, "db-mysql", "Attaching to databases - user: %s, vend: %s." % (getConfig("db_mysql_user_db"), getConfig("db_mysql_vend_db")))
+ log(Log.Verbose, "db-mysql", "Attaching to databases - user: %s, vend: %s." % (getConfig("db_mysql_user_db"), getConfig("db_mysql_vend_db")))
self.user_database.select_db(getConfig("db_mysql_user_db"))
self.vend_database.select_db(getConfig("db_mysql_vend_db"))
- log(Log.Info, "db-mysql", "Disabling TRANSACTION mode for reads.")
+ log(Log.Verbose, "db-mysql", "Disabling TRANSACTION mode for reads.")
self.user_database.query("SET TRANSACTION ISOLATION LEVEL READ COMMITTED")
self.vend_database.query("SET TRANSACTION ISOLATION LEVEL READ COMMITTED")
- log(Log.Notice, "db-mysql", "MySQL is ready.")
+ self.connected = True
+ def close(self):
+ if not self.connected:
+ return
+ self.user_database.close()
+ self.vend_database.close()
+ self.connected = False
def authenticateUser(self, card_id):
"""
Get a user by their Card's ID number
"""
+ self.connect()
# Pull user from `user` database
try:
card_id = int(card_id)
except:
log(Log.Notice,"db-auth", "Card id is not an integer.")
+ self.close()
return "Failed to read card."
self.user_database.query("SELECT * FROM `%s` WHERE uin=%d" % (getConfig("db_mysql_user_table"), card_id))
t_result = self.user_database.store_result()
@@ -63,11 +78,13 @@ def authenticateUser(self, card_id):
Environment.user = VendingUser(user_sql['uid'],user_sql['uin'],user_dict)
Environment.state = State.Authenticated
Environment.tool.gui.updateUser()
+ self.close()
return None
def getItems(self):
"""
Update and return the list of available items in each tray.
"""
+ self.connect()
Environment.trays = []
self.vend_database.query("SELECT * FROM `%s`" % getConfig("db_mysql_vend_trays"))
t_result = self.vend_database.store_result()
@@ -77,22 +94,25 @@ def getItems(self):
s_result = self.vend_database.store_result()
soda = s_result.fetch_row(how=1)[0]
Environment.trays.append(VendingItem(soda['name'], i, tray['qty'], tray['price'], soda))
+ self.close()
return Environment.trays
def purchaseItem(self, item):
+ self.connect()
self.chargeUser(item.price, item.extra['sid'])
u = Environment.user
if u.isAdmin:
return True
self.user_database.query("UPDATE `%s` SET `calories`=%d, `caffeine`=%f, `spent`=%f, `sodas`=%d WHERE `uid`=%d" % \
- (getConfig("db_mysql_user_table_alt"), u.extra['calories'] + item.extra['calories'], u.extra['caffeine'] + item.extra['caffeine'], u.extra['spent'] + item.price, \
- u.extra['sodas'] + 1, u.uid))
+ (getConfig("db_mysql_user_table_alt"), u.extra['calories'] + item.extra['calories'], u.extra['caffeine'] + item.extra['caffeine'], u.extra['spent'] + item.price, u.extra['sodas'] + 1, u.uid))
self.user_database.commit()
self.vend_database.query("UPDATE `%s` SET `dispensed`=%d WHERE `sid`=%d" % (getConfig("db_mysql_vend_items"),item.extra['dispensed'], item.extra['sid']))
self.vend_database.commit()
+ self.close()
def chargeUser(self, amount, item_id):
"""
Charge the current user some amount of money.
"""
+ self.connect()
if Environment.user.isAdmin:
return True
log(Log.Info,"db-charge", "uid: %d, amount: %.2f" % (int(Environment.user.uid), amount))
@@ -104,25 +124,31 @@ def vend(self, tray):
"""
Update the databases' knowledge of the number of items in a tray.
"""
+ self.connect()
if Environment.user.isAdmin:
+ self.close()
return True
self.vend_database.query("select * from `%s` where `tid`=%d" % (getConfig("db_mysql_vend_trays"),tray))
dbtray = self.vend_database.store_result().fetch_row(how=1)[0]
self.vend_database.query("update `%s` set `qty`=%d where `tid`=%d" % (getConfig("db_mysql_vend_trays"), dbtray['qty'] - 1, tray))
self.vend_database.commit()
self.getItems()
+ self.close()
return True
# Written by schober1 -- may be f***ing broken!
def lastNPurchases(self, user, n):
"""
Retrieves the last n purchases for a user.
"""
+ self.connect()
try:
num_purchases = int(n)
except:
+ self.close()
log(Log.Notice,"db-auth", "n is not an integer.")
return None
self.user_database.query("SELECT * FROM `%s` WHERE `uid`=%d ORDER BY `tid` DESC LIMIT %d" % (getConfig("db_mysql_user_table_transactions"), user.uid, num_purchases))
purchases = self.user_database.store_result().fetch_row(how=1,maxrows=num_purchases)
+ self.close()
return purchases
View
BIN www/assets/drinks/71.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN www/assets/drinks/73.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN www/assets/drinks/cancel.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
7 www/gui/include/tray_list.html
@@ -13,6 +13,13 @@
t_class = "tray_normal"
drink._title = drink.title
#else
+ if drink.tray == 0:
+ t_class = "tray_normal"
+ drink.extra['sid'] = "cancel"
+ drink._title = "(Cancel)"
+ drink.extra['calories'] = "N/A"
+ drink.extra['caffeine'] = "N/A"
+ #if
?>
<tr class='<? print t_class ?>'>
<td rowspan='2' class='header'><? print drink.tray ?></td>

0 comments on commit f0c8188

Please sign in to comment.