All db operations now should go thru Database object

This commit is contained in:
Václav 'Ax' Hůla 2012-10-27 23:20:43 +02:00
parent ffa119e7f0
commit a4dbf3b77c
5 changed files with 128 additions and 100 deletions

View file

@ -20,13 +20,11 @@ class Currency:
def load(cls, db, id = None, name = None):
""" Constructor for existing currency """
if id is not None:
with closing(db.cursor()) as cur:
cur.execute("SELECT name FROM currencies WHERE id = %s", [id])
name = cur.fetchone()[0]
name = db.execute_and_fetch("SELECT name FROM currencies WHERE id = %s", [id])
name = name[0]
elif name is not None:
with closing(db.cursor()) as cur:
cur.execute("SELECT id FROM currencies WHERE name = %s", [name])
id = cur.fetchone()[0]
id = db.execute_and_fetch("SELECT id FROM currencies WHERE name = %s", [name])
id = id[0]
else:
raise NameError("Currency.load(): Specify either id or name")
return cls(db, name = name, id = id)
@ -34,52 +32,44 @@ class Currency:
@classmethod
def create(cls, db, name):
""" Constructor for new currency """
with closing(db.cursor()) as cur:
cur.execute("INSERT INTO currencies (name) VALUES (%s) RETURNING id", [name])
id = cur.fetchone()[0]
id = db.execute_and_fetch("INSERT INTO currencies (name) VALUES (%s) RETURNING id", [name])
id = id[0]
return cls(db, name = name, id = id)
def rates(self, other):
""" Return tuple ($buy, $sell) of rates of $self in relation to $other (brmbar.Currency):
$buy is the price of $self in means of $other when buying it (into brmbar)
$sell is the price of $self in means of $other when selling it (from brmbar) """
with closing(self.db.cursor()) as cur:
cur.execute("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [self.id, other.id])
res = cur.fetchone()
if res is None:
raise NameError("Currency.rate(): Unknown conversion " + other.name() + " to " + self.name())
buy_rate, buy_rate_dir = res
buy = buy_rate if buy_rate_dir == "target_to_source" else 1/buy_rate
res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [self.id, other.id])
if res is None:
raise NameError("Currency.rate(): Unknown conversion " + other.name() + " to " + self.name())
buy_rate, buy_rate_dir = res
buy = buy_rate if buy_rate_dir == "target_to_source" else 1/buy_rate
cur.execute("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [other.id, self.id])
res = cur.fetchone()
if res is None:
raise NameError("Currency.rate(): Unknown conversion " + self.name() + " to " + other.name())
sell_rate, sell_rate_dir = res
sell = sell_rate if sell_rate_dir == "source_to_target" else 1/sell_rate
res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [other.id, self.id])
if res is None:
raise NameError("Currency.rate(): Unknown conversion " + self.name() + " to " + other.name())
sell_rate, sell_rate_dir = res
sell = sell_rate if sell_rate_dir == "source_to_target" else 1/sell_rate
return (buy, sell)
return (buy, sell)
def convert(self, amount, target):
with closing(self.db.cursor()) as cur:
cur.execute("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [target.id, self.id])
res = cur.fetchone()
if res is None:
raise NameError("Currency.convert(): Unknown conversion " + self.name() + " to " + other.name())
rate, rate_dir = res
if rate_dir == "source_to_target":
resamount = amount * rate
else:
resamount = amount / rate
return resamount
res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [target.id, self.id])
if res is None:
raise NameError("Currency.convert(): Unknown conversion " + self.name() + " to " + target.name())
rate, rate_dir = res
if rate_dir == "source_to_target":
resamount = amount * rate
else:
resamount = amount / rate
return resamount
def str(self, amount):
return "{:.2f} {}".format(amount, self.name)
def update_sell_rate(self, target, rate):
with closing(self.db.cursor()) as cur:
cur.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [self.id, target.id, rate, "source_to_target"])
self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [self.id, target.id, rate, "source_to_target"])
def update_buy_rate(self, source, rate):
with closing(self.db.cursor()) as cur:
cur.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [source.id, self.id, rate, "target_to_source"])
self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [source.id, self.id, rate, "target_to_source"])