First commit or something.
commit
e99a600ee1
|
@ -0,0 +1,5 @@
|
|||
db_host = "localhost"
|
||||
db_prefix = "hf_"
|
||||
db_user = "hackfridge"
|
||||
db_password = "hackfridge"
|
||||
db_database = "hackfridge"
|
Binary file not shown.
|
@ -0,0 +1,63 @@
|
|||
import logic
|
||||
import sys
|
||||
|
||||
actions = {
|
||||
"initialize": (logic.initialize, [], True),
|
||||
"financing-add": (logic.add_financing, [("title", "string"), ("value", "int")], False),
|
||||
"info": (logic.print_info, [], False),
|
||||
"topup-add": (logic.add_topup, [("user_id", "string"), ("value", "string")], False),
|
||||
"purchase-add": (logic.add_purchase, [("user_id", "string"), ("product_id", "strings")], False),
|
||||
"product-add": (logic.add_product, [("name", "string"), ("code", "string"), ("value", "int"), ("cost", "int")], False),
|
||||
}
|
||||
|
||||
def start():
|
||||
db = logic.sql_connect()
|
||||
print "[i] Available actions: quit, %s" % (", ".join(actions))
|
||||
|
||||
while True:
|
||||
sys.stdout.write("> ")
|
||||
sys.stdout.flush()
|
||||
line = raw_input().strip()
|
||||
command = line.split()[0]
|
||||
|
||||
if command == "quit":
|
||||
break
|
||||
|
||||
if command not in actions:
|
||||
print "[e] No such action."
|
||||
continue
|
||||
|
||||
action = actions[command]
|
||||
method = action[0]
|
||||
arguments = action[1]
|
||||
unsafe = action[2]
|
||||
|
||||
if unsafe:
|
||||
print "[w] Warning! This command is UNSAFE. It will probably BREAK SHIT. Type an uppercase 'yes' to continue."
|
||||
sys.stdout.write("> ")
|
||||
sys.stdout.flush()
|
||||
result = raw_input()
|
||||
if result != "YES":
|
||||
print "[e] Make up your mind."
|
||||
continue
|
||||
|
||||
given_arguments = line.split()[1:]
|
||||
if len(given_arguments) != len(arguments):
|
||||
print "[e] Syntax: %s %s" % (command, " ".join([argument[0] for argument in arguments]))
|
||||
continue
|
||||
|
||||
parsed_arguments = []
|
||||
for i in range(len(arguments)):
|
||||
given = given_arguments[i]
|
||||
argument = arguments[i]
|
||||
|
||||
t = argument[1]
|
||||
if t == "int":
|
||||
given = int(given)
|
||||
|
||||
parsed_arguments.append(given)
|
||||
|
||||
method(db, *parsed_arguments)
|
||||
|
||||
if __name__ == "__main__":
|
||||
start()
|
|
@ -0,0 +1,130 @@
|
|||
import MySQLdb as sql
|
||||
|
||||
import config
|
||||
|
||||
def sql_connect():
|
||||
db = sql.connect(config.db_host, config.db_user, config.db_password, config.db_database)
|
||||
return db
|
||||
|
||||
def __sql_execute(db, query, *args, **kwargs):
|
||||
cursor = db.cursor(sql.cursors.DictCursor)
|
||||
r = cursor.execute(query, *args, **kwargs)
|
||||
if not query.startswith("SELECT"):
|
||||
cursor.close()
|
||||
return r
|
||||
|
||||
data = cursor.fetchall()
|
||||
cursor.close()
|
||||
return data
|
||||
|
||||
def initialize(db):
|
||||
__sql_execute(db, "DROP TABLE IF EXISTS %sproducts;" % config.db_prefix)
|
||||
__sql_execute(db, "DROP TABLE IF EXISTS %srestocks;" % config.db_prefix)
|
||||
__sql_execute(db, "DROP TABLE IF EXISTS %stopups;" % config.db_prefix)
|
||||
__sql_execute(db, "DROP TABLE IF EXISTS %sfinancing;" % config.db_prefix)
|
||||
__sql_execute(db, "DROP TABLE IF EXISTS %spurchases;" % config.db_prefix)
|
||||
|
||||
__sql_execute(db, """CREATE TABLE %sproducts (
|
||||
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(40) NOT NULL,
|
||||
code VARCHAR(100) NOT NULL,
|
||||
cost INT(11), NOT NULL,
|
||||
value INT(11) NOT NULL);""" % config.db_prefix)
|
||||
|
||||
__sql_execute(db, """CREATE TABLE %srestocks (
|
||||
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
product_id INT(11) NOT NULL,
|
||||
title VARCHAR(100) NOT NULL,
|
||||
datetime DATETIME NOT NULL,
|
||||
count INT(11) NOT NULL);""" % config.db_prefix)
|
||||
|
||||
__sql_execute(db, """CREATE TABLE %sfinancing (
|
||||
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
title VARCHAR(100) NOT NULL,
|
||||
datetime DATETIME NOT NULL,
|
||||
value INT(11) NOT NULL);""" % config.db_prefix)
|
||||
|
||||
__sql_execute(db, """CREATE TABLE %stopups (
|
||||
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
value INT(11) NOT NULL,
|
||||
datetime DATETIME NOT NULL,
|
||||
user_id INT(11) NOT NULL);""" % config.db_prefix)
|
||||
|
||||
__sql_execute(db, """CREATE TABLE %spurchases (
|
||||
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
||||
datetime DATETIME,
|
||||
product_id INT(11) NOT NULL,
|
||||
user_id INT(11) NOT NULL);""" % config.db_prefix)
|
||||
|
||||
def add_product(db, name, code, value, cost):
|
||||
__sql_execute(db, "INSERT INTO %sproducts (name, code, value, cost) VALUES (%%s, %%s, %%s);" % config.db_prefix, (name, code, value))
|
||||
|
||||
|
||||
def add_restock(db, product_id, title, count):
|
||||
__sql_execute(db, "INSERT INTO %srestocks (product_id, title, count, datetime) VALUES (%%s, %%s, %%s, NOW());" % config.db_prefix, (product_id, title, count))
|
||||
|
||||
def add_financing(db, title, value):
|
||||
__sql_execute(db, "INSERT INTO %sfinancing (title, value, datetime) VALUES (%%s, %%s, NOW());" % config.db_prefix, (title, value))
|
||||
|
||||
def add_topup(db, user_id, value):
|
||||
__sql_execute(db, "INSERT INTO %stopups (value, user_id, datetime) VALUES (%%s, %%s, NOW());" % config.db_prefix, (value, user_id))
|
||||
|
||||
def add_purchase(db, user_id, product_id):
|
||||
__sql_execute(db, "INSERT INTO %spurchases (product_id, user_id, datetime) VALUES (%%s, %%s, NOW());" % config.db_prefix, (product_id, user_id))
|
||||
|
||||
def print_info(db):
|
||||
f = get_financing_data(db)
|
||||
u = get_user_data(db)
|
||||
|
||||
print " FINANCING"
|
||||
print "-" * 76
|
||||
print "% 40s | % 20s | % 10s" % ("title", "date", "value")
|
||||
print "-" * 76
|
||||
for financing in f["financings"]:
|
||||
print "% 40s | % 20s | % 10.2f" % (financing["title"], financing["datetime"].strftime("%H:%M %m/%d/%y"), financing["value"]/100.0)
|
||||
print "-" * 76
|
||||
print " " * (63 - len("total")) + "total | " + "% 10.2f" % (f["financings_amount"]/100.0)
|
||||
print "-" * 76
|
||||
|
||||
|
||||
print "\n\n USERS"
|
||||
print "-" * 76
|
||||
print "% 63s | % 10s" % ("user id", "balance")
|
||||
print "-" * 76
|
||||
for user_id, balance in u["balance"].iteritems():
|
||||
print "% 63s | % 10.2f" % (user_id, balance/100.0)
|
||||
print "-" * 76
|
||||
|
||||
real_money = 0
|
||||
for topup in u["topups"]:
|
||||
real_money += topup["value"]
|
||||
|
||||
print "\n\n"
|
||||
print "Revenue (without financing): %.2f" % (real_money/100.0)
|
||||
|
||||
real_money += f["financings_amount"]
|
||||
print "Revenue (with financing): %.2f" % (real_money/100.0)
|
||||
|
||||
def get_financing_data(db):
|
||||
financings = __sql_execute(db, "SELECT title, datetime, value from %sfinancing;" % config.db_prefix)
|
||||
financings_amount = 0
|
||||
for financing in financings:
|
||||
financings_amount += financing["value"]
|
||||
|
||||
return {"financings": financings, "financings_amount": financings_amount}
|
||||
|
||||
|
||||
def get_user_data(db):
|
||||
topups = [{"user_id": topup["user_id"], "value": int(topup["SUM(value)"])} for topup in __sql_execute(db, "SELECT user_id, SUM(value) FROM %stopups GROUP BY user_id" % config.db_prefix)]
|
||||
users = dict([(topup["user_id"], topup["value"]) for topup in topups])
|
||||
|
||||
purchases = __sql_execute(db, "SELECT %spurchases.user_id, SUM(%sproducts.value) FROM %spurchases LEFT JOIN %sproducts on %spurchases.product_id = %sproducts.id GROUP BY %spurchases.user_id;" % (config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix, config.db_prefix))
|
||||
|
||||
for purchase in purchases:
|
||||
if purchase["user_id"] not in users:
|
||||
users[purchase["user_id"]] = 0
|
||||
|
||||
users[purchase["user_id"]] -= int(purchase['SUM(hf_products.value)'])
|
||||
|
||||
return {"balance":users, "topups": topups}
|
||||
|
Binary file not shown.
Reference in New Issue