aboutsummaryrefslogtreecommitdiffstats
path: root/python/Dawn/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/Dawn/views.py')
-rw-r--r--python/Dawn/views.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/python/Dawn/views.py b/python/Dawn/views.py
index 1149b078..897cef58 100644
--- a/python/Dawn/views.py
+++ b/python/Dawn/views.py
@@ -302,7 +302,7 @@ def split_direction(g, direction):
def gen_cashflow_deals(form, session, wire_id=None):
- to_date = lambda s: datetime.datetime.strptime(s, "%Y-%m-%d")
+ to_date = datetime.date.fromisoformat
d = {
"action": form.get("action"),
"trade_date": form.get("trade_date", None, to_date),
@@ -382,12 +382,27 @@ def wire_manage(wire_id):
@app.route("/trades/<kind>/<dealid>/terminate", methods=["GET", "POST"])
def terminate(dealid, kind):
- termination = Termination(dealid=dealid, termination_date=datetime.date.today())
- form = TerminationForm(obj=termination)
+ termination = Termination()
+ form = TerminationForm(dealid=dealid)
form.termination_cp.choices = form.termination_cp.choices + list(cp_choices(kind))
if form.validate_on_submit():
form.populate_obj(termination)
session = form.get_session()
+ if not termination.partial_termination or termination.termination_cp is None:
+ rec = db.session.execute(
+ "SELECT cp_code, notional, coalesce(terminated_amount, 0.) "
+ "FROM swaptions "
+ "LEFT JOIN "
+ "(SELECT dealid, sum(termination_amount) AS terminated_amount "
+ " FROM terminations group by dealid) term USING (dealid) "
+ "WHERE dealid = :dealid",
+ {"dealid": dealid},
+ )
+ cp_code, notional, terminated_amount = next(rec)
+ termination.termination_amount = notional - terminated_amount
+ if termination.termination_cp is None:
+ termination.termination_cp = cp_code
+ session.add(termination)
try:
session.commit()
except IntegrityError as e: