Skip to content
Snippets Groups Projects
Commit 3b85e2ba authored by Teemu Autto's avatar Teemu Autto
Browse files

command to update currency file.

parent 080d737a
No related branches found
No related tags found
No related merge requests found
......@@ -62,6 +62,9 @@ RUN pip --disable-pip-version-check install -v -e .
ARG CI_COMMIT_SHA
ENV CI_COMMIT_SHA=${CI_COMMIT_SHA}
## Download the currency exchange rates from European Central Bank
RUN flask update-currency-rates
## Save build date and time
RUN echo "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> /app/.env
......
......@@ -9,6 +9,8 @@ flask-babel
flask-mongoengine==1.0
CurrencyConverter
# Git hooks
pre-commit
......
......@@ -76,6 +76,9 @@ def create_app(config: Optional[Dict] = None) -> Flask:
from .auth import init_auth
init_auth(flask_app)
from .currency import init_currency
init_currency(flask_app)
from . import items
flask_app.register_blueprint(items.bp)
flask_app.add_url_rule('/', endpoint='index')
......
"""
Currency module.
This module contains the currency module, which is used to convert currencies.
Uses the ECB (European Central Bank) as the source of currency conversion rates.
To update the currency conversion rates, run the following command:
$ flask update-currency-rates
"""
from zipfile import ZipFile
import urllib.request
import click
from currency_converter import (
SINGLE_DAY_ECB_URL,
)
from flask import (
Flask,
current_app,
)
def init_currency(app: Flask):
"""
Initialize the currency module.
This function initializes the currency module, and registers the currency
converter as an extension.
:param app: The Flask application.
:return: None
"""
# Set default currency file path
app.config.setdefault('CURRENCY_FILE', app.instance_path + '/currency.csv')
app.cli.add_command(update_currency_rates)
@click.command()
def update_currency_rates():
"""
Update currency file from the European Central Bank.
This command is meant to be run from the command line, and is not meant to be
used in the application:
$ flask update-currency-rates
:return: None
"""
click.echo('Updating currency file from the European Central Bank...')
fd, _ = urllib.request.urlretrieve(SINGLE_DAY_ECB_URL)
with ZipFile(fd) as zf:
file_name = zf.namelist().pop()
with open(current_app.config['CURRENCY_FILE'], 'wb') as f:
f.write(zf.read(file_name))
click.echo('Done.')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment