aboutsummaryrefslogtreecommitdiffstats
path: root/python/Dawn/dawn.py
blob: 6d28d6dcc5ed649b561bcba2ebdc31101dab439e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from flask import Flask, request, render_template
from models import db, ModelForm, BondDeal, Counterparties
from sqlalchemy.sql import text
from sqlalchemy import create_engine
import pandas as pd

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://dawn_user@debian/dawndb'
app.config['SECRET_KEY'] = 'papa'
app.config['UPLOAD_FOLDER'] = 'tickets'

def list_counterparties():
    return Counterparties.query.order_by('name').\
        with_entities(Counterparties.code, Counterparties.name)

def load_counterparties():
    counterparties = pd.read_csv("/home/share/CorpCDOs/data/counterparties.csv")
    counterparties[['city', 'state']] = counterparties.Location.str.split(", ", expand=True)
    counterparties.drop(['Location', 'Valuation Contact4', 'Valuation Contact4 Email'], axis=1, inplace=True)
    counterparties.rename(columns ={'CODE': 'code',
                                    'DTC Number': 'dtc_number',
                                    'Email1': 'sales_email',
                                    'FIRM': 'name',
                                    'Phone': 'sales_phone',
                                    'Sales Contact': 'sales_contact',
                                    'Valuation Contact1': 'valuation_contact1',
                                    'Valuation Contact1 Email': 'valuation_email1',
                                    'Valuation Contact2': 'valuation_contact2',
                                    'Valuation Contact2 Email': 'valuation_email2',
                                    'Valuation Contact3': 'valuation_contact3',
                                    'Valuation Contact3 Email': 'valuation_email3',
                                    'Valuation Note': 'notes'}, inplace=True)
    engine = create_engine('postgresql://dawn_user@debian/dawndb')
    counterparties.to_sql('counterparties', engine, if_exists='append', index=False)

def add_triggers():
    engine = create_engine('postgresql://dawn_user@debian/dawndb')
    engine.execute(text("""
    CREATE OR REPLACE FUNCTION mydealid()
    RETURNS TRIGGER AS $$
    BEGIN
       UPDATE bonds SET dealid = 'SC_' ||left(asset_class::text,3)||id WHERE bonds.id = NEW.id;
       RETURN NEW;
    END;
    $$ language plpgsql"""))
    engine.execute(text("""CREATE TRIGGER dealid
       AFTER INSERT ON bonds
       FOR EACH ROW
       EXECUTE PROCEDURE mydealid()"""))

class BondForm(ModelForm):
    class Meta:
        model = BondDeal
        include_foreign_keys = True
        exclude = ['dealid', 'lastupdate'] #we generate it with a trigger at the server level

class CounterpartyForm(ModelForm):
    class Meta:
        model = Counterparties

@app.route('/', methods=['GET', 'POST'])
def trade_entry():
    bond_form = BondForm()
    bond_form.counterparty.choices = list_counterparties()
    if bond_form.is_submitted():
        if bond_form.validate():
            bond = BondDeal()
            bond_form.populate_obj(bond)
            bond.ticket.save('test.pdf')
            bond.ticket='test.pdf'
            bond_form.get_session().add(bond)
            bond_form.get_session().commit()
            return "Success!"
        else:
            print(bond_form.errors)
    return render_template("trade_entry.html", form=bond_form)

@app.route('/trade/<int:tradeid>', methods=['GET', 'POST'])
def trade_update(tradeid):
    trade = BondDeal()
    bond_form = BondForm(obj = BondDeal.query.get(tradeid))
    bond_form.counterparty.choices = list_counterparties()
    if bond_form.is_submitted():
        if bond_form.validate():
            bond_form.populate_obj(trade)
            return redirect(url_for('list_trades'))
        else:
            return "something wrong!"
    return render_template("trade_entry.html", form=bond_form)

@app.route('/blotter/')
def list_trades():
    trade_list = BondDeal.query.order_by(BondDeal.trade_date)
    return render_template('blotter.html', trades=trade_list.all())

@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('/edit_cp/<cpcode>', methods=['GET', 'POST'])
def edit_counterparty(cpcode):
    cp_form = CounterpartyForm(obj = Counterparties.query.get(cpcode))
    if cp_form.is_submitted():
        if cp_form.validate():
            cp = Counterparties()
            cp_form.populate_obj(cp)
            return
    return render_template('edit_cp.html', form=cp_form)

if __name__=="__main__":
    db.init_app(app)
    db.drop_all(app=app)
    db.create_all(app=app)
    add_triggers()
    load_counterparties()
    app.run(debug=True)