aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/Dawn/__init__.py15
-rw-r--r--python/Dawn/views.py (renamed from python/Dawn/dawn.py)64
2 files changed, 53 insertions, 26 deletions
diff --git a/python/Dawn/__init__.py b/python/Dawn/__init__.py
new file mode 100644
index 00000000..1029d764
--- /dev/null
+++ b/python/Dawn/__init__.py
@@ -0,0 +1,15 @@
+from flask import Flask
+import logging
+
+app = Flask(__name__)
+app.config.from_envvar('CONF')
+from Dawn.models import db
+db.init_app(app)
+if not app.debug:
+ fh = logging.FileHandler(filename=app.config['LOGFILE'])
+ fh.setLevel(logging.INFO)
+ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+ fh.setFormatter(formatter)
+ app.logger.addHandler(fh)
+
+import Dawn.views
diff --git a/python/Dawn/dawn.py b/python/Dawn/views.py
index c3a88dc8..9f0a0240 100644
--- a/python/Dawn/dawn.py
+++ b/python/Dawn/views.py
@@ -1,15 +1,16 @@
-from flask import Flask, request, render_template, redirect, url_for, send_from_directory, g
-from models import db, ModelForm, BondDeal, Counterparties
+from flask import (request, render_template, redirect,
+ url_for, send_from_directory, send_file, g)
+from .models import ModelForm, BondDeal, Counterparties
from sqlalchemy import create_engine
from sqlalchemy.exc import IntegrityError
import pandas as pd
import os
import datetime
import redis
-from utils import load_counterparties, load_trades, add_triggers, bump_rev, simple_serialize
-
-app = Flask(__name__)
-app.config.from_envvar('CONF')
+from .utils import load_counterparties, load_trades, add_triggers, bump_rev, simple_serialize
+from PyPDF2 import PdfFileMerger
+from io import BytesIO
+from Dawn import app
def cp_choices():
return Counterparties.query.order_by('name').\
@@ -56,7 +57,7 @@ def trade_manage(tradeid):
if tradeid:
trade.ticket = old_ticket_name
else:
- if tradeid:
+ if old_ticket_name:
trade.ticket = bump_rev(old_ticket_name)
else:
trade.ticket = "{0} {1}.pdf".format(str(trade.trade_date),
@@ -77,37 +78,48 @@ def trade_manage(tradeid):
@app.route('/blotter/')
def list_trades():
- trade_list = BondDeal.query.order_by(BondDeal.trade_date)
+ trade_list = BondDeal.query.order_by(BondDeal.trade_date, BondDeal.id)
return render_template('blotter.html', trades=trade_list.all())
-@app.route('/tickets/<path:filename>')
-def download_ticket(filename):
- return send_from_directory(app.config['TICKETS_FOLDER'],
- filename, as_attachment=True)
+@app.route('/tickets/<int:tradeid>')
+def download_ticket(tradeid):
+ trade = BondDeal.query.get(tradeid)
+ pdf = PdfFileMerger()
+ pdf.append(os.path.join(app.config['TICKETS_FOLDER'], trade.ticket))
+ pdf.append(os.path.join(app.config['CP_FOLDER'], trade.counterparty.instructions))
+ fh = BytesIO()
+ pdf.write(fh)
+ pdf.close()
+ fh.seek(0)
+ return send_file(fh, mimetype='application/pdf')
-@app.route('/counterparties/')
-def list_counterparties():
- cp_list = Counterparties.query.order_by(Counterparties.name)
- return render_template('counterparties.html', counterparties = cp_list.all())
+@app.route('/counterparties/<path:instr>', methods=['GET'])
+@app.route('/counterparties/', defaults={'instr': None}, methods=['GET'])
+def list_counterparties(instr):
+ if instr:
+ return send_from_directory(filename = instr,
+ directory = app.config['CP_FOLDER'],
+ mimetype='application/pdf')
+ else:
+ cp_list = Counterparties.query.order_by(Counterparties.name)
+ return render_template('counterparties.html', counterparties = cp_list.all())
@app.route('/edit_cp/<cpcode>', methods=['GET', 'POST'])
def edit_counterparty(cpcode):
cp = Counterparties.query.get(cpcode)
cp_form = CounterpartyForm(obj = cp)
+ old_instructions = cp.instructions
if cp_form.is_submitted():
if cp_form.validate():
cp_form.populate_obj(cp)
session = cp_form.get_session()
+ instructions = cp.instructions
+ if instructions.filename == '':
+ cp.instructions = old_instructions
+ else:
+ cp.instructions = cp.name + '.pdf'
+ instructions.save(os.path.join(app.config['CP_FOLDER'],
+ cp.instructions))
session.commit()
return redirect(url_for('list_counterparties'))
return render_template('edit_cp.html', form=cp_form, code=cpcode)
-
-if __name__=="__main__":
- db.init_app(app)
- #db.drop_all(app=app)
- #db.create_all(app=app)
- #engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
- #add_triggers(engine)
- #load_counterparties(engine)
- #load_trades(engine)
- app.run(debug=True)