From 6ac230c7d9707dfea1a5e780cb6505900e466989 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 28 Sep 2012 22:00:09 +0200 Subject: [PATCH] brmbar.Currency.rates(): Use _latest_ exchange rate, not earliest --- brmbar3/brmbar/Currency.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index 8acd260..725dcc5 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -45,14 +45,14 @@ class Currency: $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", [self.id, other.id]) + 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 - cur.execute("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s", [other.id, self.id]) + 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())