-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from decimal import Decimal, ROUND_UP | ||
|
||
from mangopay.utils import Money | ||
import requests | ||
import xmltodict | ||
|
||
from liberapay.constants import D_CENT, D_ZERO | ||
from liberapay.website import website | ||
|
||
|
||
def _convert(self, c): | ||
if self.currency == c: | ||
return self | ||
amount = self.amount * website.currency_exchange_rates[(self.currency, c)] | ||
return Money(amount.quantize(D_CENT), c) | ||
|
||
|
||
Money.convert = _convert | ||
|
||
|
||
def fetch_currency_exchange_rates(db): | ||
currencies = set(db.one("SELECT array_to_json(enum_range(NULL::currency))")) | ||
r = requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml') | ||
rates = xmltodict.parse(r.text)['gesmes:Envelope']['Cube']['Cube']['Cube'] | ||
for fx in rates: | ||
currency = fx['@currency'] | ||
if currency not in currencies: | ||
continue | ||
db.run(""" | ||
INSERT INTO currency_exchange_rates | ||
(source_currency, target_currency, rate) | ||
VALUES ('EUR', %(target)s, %(rate)s) | ||
, (%(target)s, 'EUR', 1 / %(rate)s) | ||
ON CONFLICT (source_currency, target_currency) DO UPDATE | ||
SET rate = excluded.rate | ||
""", dict(target=currency, rate=Decimal(fx['@rate']))) | ||
|
||
|
||
def get_currency_exchange_rates(db): | ||
r = {(r[0], r[1]): r[2] for r in db.all("SELECT * FROM currency_exchange_rates")} | ||
if r: | ||
return r | ||
fetch_currency_exchange_rates(db) | ||
return get_currency_exchange_rates(db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ CACHE_STATIC=yes | |
CLEAN_ASSETS=yes | ||
OVERRIDE_QUERY_CACHE=yes | ||
ASPEN_CHANGES_RELOAD=no | ||
RUN_CRON_JOBS=no |