From 104289418ba52ba9feaefc30d268048189f1118d Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 21 Jun 2016 07:51:36 -0600 Subject: [PATCH] Add support for creating and listing OFXAccounts This makes it so that we can add new accounts and show the accounts that we already have. We don't do anything with them yet, but that's okay it was interesting figuring out how to get them set up at all. I'm currently storing the passwords as unencrypted, which I intend to change, but it's going to take some time to research exactly how to encrypt them so that the data is not retrievable by a bad actor with access to the database. --- templates/accounts.html | 39 +++++++++++++++++++++++++-- templates/login.html | 3 ++- vanth/pages/accounts.py | 24 ++++++++++++++--- vanth/platform/ofxaccount.py | 52 ++++++++++++++++++++++++++++++++++++ vanth/platform/ofxsource.py | 11 ++------ 5 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 vanth/platform/ofxaccount.py diff --git a/templates/accounts.html b/templates/accounts.html index 5f2cf66..6e64ee2 100644 --- a/templates/accounts.html +++ b/templates/accounts.html @@ -1,7 +1,42 @@ {% extends 'layout.html' %} {% block main_content %}

Accounts

-{% if not accounts %} -

You don't have any accounts yet. Let's create some

+{% if accounts %} + + + {% for account in accounts %} + + + + + + {% endfor %} +
NameTypeInstitution
{{ account.name }}{{ account.type }}{{ account.institution }}
+{% else %} +

You don't have any accounts yet. Let's create some

{% endif %} +

Add new account

+
+
+ + + + + + + + + + + + + {% for source in sources %} + + {% endfor %} + +
+
{% endblock %} diff --git a/templates/login.html b/templates/login.html index 03f949b..cf662c3 100644 --- a/templates/login.html +++ b/templates/login.html @@ -8,6 +8,7 @@ - + + {% endblock %} diff --git a/vanth/pages/accounts.py b/vanth/pages/accounts.py index 1571d25..1d0f67e 100644 --- a/vanth/pages/accounts.py +++ b/vanth/pages/accounts.py @@ -1,8 +1,24 @@ import flask +import vanth.platform.ofxaccount +import vanth.platform.ofxsource + blueprint = flask.Blueprint('accounts', __name__) -@blueprint.route('/accounts/') -def accounts(): - my_accounts = [] - return flask.render_template('accounts.html', accounts=my_accounts) +@blueprint.route('/accounts/', methods=['GET']) +def get_accounts(): + my_accounts = vanth.platform.ofxaccount.get(flask.session['user_id']) + sources = vanth.platform.ofxsource.get() + return flask.render_template('accounts.html', accounts=my_accounts, sources=sources) + +@blueprint.route('/account/', methods=['POST']) +def post_account(): + account_type = flask.request.form.get('account_type') + institution = flask.request.form.get('institution') + name = flask.request.form.get('name') + password = flask.request.form.get('password') + userid = flask.request.form.get('userid') + + + vanth.platform.ofxaccount.create(flask.session['user_id'], name, account_type, institution, password, userid) + return flask.redirect('/accounts/') diff --git a/vanth/platform/ofxaccount.py b/vanth/platform/ofxaccount.py new file mode 100644 index 0000000..2b1bf95 --- /dev/null +++ b/vanth/platform/ofxaccount.py @@ -0,0 +1,52 @@ +import uuid + +import chryso.connection +import sqlalchemy + +import vanth.platform.ofxsource +import vanth.tables + + +def get(user_id): + engine = chryso.connection.get() + query = sqlalchemy.select([ + vanth.tables.OFXSource.c.name.label('institution'), + vanth.tables.OFXAccount.c.name, + vanth.tables.OFXAccount.c.source, + vanth.tables.OFXAccount.c.type, + vanth.tables.OFXAccount.c.user_id, + vanth.tables.OFXAccount.c.uuid, + ]).where( + vanth.tables.OFXAccount.c.source == vanth.tables.OFXSource.c.uuid + ).where( + vanth.tables.OFXAccount.c.owner == user_id + ) + results = engine.execute(query) + return [{ + 'institution' : result[vanth.tables.OFXSource.c.name.label('institution')], + 'name' : result[vanth.tables.OFXAccount.c.name], + 'source' : result[vanth.tables.OFXAccount.c.source], + 'type' : result[vanth.tables.OFXAccount.c.type], + 'user_id' : result[vanth.tables.OFXAccount.c.user_id], + 'uuid' : result[vanth.tables.OFXAccount.c.uuid], + } for result in results] + +def create(user_id, name, account_type, institution, password, account_user): + engine = chryso.connection.get() + + source_name = sqlalchemy.select([ + vanth.tables.OFXSource.c.uuid + ]).where(vanth.tables.OFXSource.c.name == institution) + + _uuid = uuid.uuid4() + statement = vanth.tables.OFXAccount.insert().values( # pylint: disable=no-value-for-parameter + uuid = _uuid, + name = name, + user_id = account_user, + password = password, + type = account_type, + source = source_name, + owner = user_id, + ) + engine.execute(statement) + return _uuid diff --git a/vanth/platform/ofxsource.py b/vanth/platform/ofxsource.py index eca0b7d..764745f 100644 --- a/vanth/platform/ofxsource.py +++ b/vanth/platform/ofxsource.py @@ -1,20 +1,13 @@ import logging import chryso.connection -import sepiida.routing import vanth.tables LOGGER = logging.getLogger(__name__) -def by_filter(filters): +def get(): engine = chryso.connection.get() - LOGGER.debug("Getting ofxsources by filter %s", filters) query = vanth.tables.OFXSource.select() results = engine.execute(query).fetchall() - return [{ - 'name' : result[vanth.tables.OFXSource.c.name], - 'fid' : result[vanth.tables.OFXSource.c.fid], - 'bankid' : result[vanth.tables.OFXSource.c.bankid], - 'uri' : sepiida.routing.uri('ofxsource', result[vanth.tables.OFXSource.c.uuid]), - } for result in results] + return [dict(result) for result in results]