From 6a67f6d6307b5530eb0518e4317b77a65555607f Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 13:23:19 -0400 Subject: [PATCH 01/17] Update financial inputs urls to route cash balance to the new file and not the old. --- blocnote/apps/financialInputs/urls.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/blocnote/apps/financialInputs/urls.py b/blocnote/apps/financialInputs/urls.py index f27b683..55e1e8d 100644 --- a/blocnote/apps/financialInputs/urls.py +++ b/blocnote/apps/financialInputs/urls.py @@ -1,8 +1,10 @@ +"""Define the routes for the financial inputs endpoint.""" from django.conf.urls import url from . import old_views -from .views import proforma_input -from .views import customer_preference +from .views import ( + proforma_input, customer_preference, cash_balance +) app_name = 'financial-inputs' urlpatterns = [ @@ -12,7 +14,7 @@ urlpatterns = [ url(r'^bills-overview/$', old_views.BillsOverviewView.as_view(), name='bills_overview'), url(r'^customer-preference/$', customer_preference.CustomerPreferenceView.as_view(), name='customer_preference'), url(r'^liabilities/$', old_views.LiabilitiesTable.as_view(), name='liabilities'), - url(r'^cash-balance/$', old_views.CashBalanceView.as_view(), name='cash_balance'), + url(r'^cash-balance/$', cash_balance.CashBalanceView.as_view(), name='cash_balance'), url(r'^income-statement/$', old_views.IncomeStatementTable.as_view(), name='income_statement'), url(r'^loan-options/$', old_views.LoanOptionsTable.as_view(), name='loan_options'), ] -- GitLab From 65e92ea225c0aa554fc61ce18c93bf0864f416ef Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 13:26:31 -0400 Subject: [PATCH 02/17] Create a new .py file for cash balance and move the code from old_views to this one. Change the get function to use the fields list to access attributes from the database object in a loop rather than each individual field. --- .../financialInputs/views/cash_balance.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 blocnote/apps/financialInputs/views/cash_balance.py diff --git a/blocnote/apps/financialInputs/views/cash_balance.py b/blocnote/apps/financialInputs/views/cash_balance.py new file mode 100644 index 0000000..4d550ca --- /dev/null +++ b/blocnote/apps/financialInputs/views/cash_balance.py @@ -0,0 +1,61 @@ +"""Views for the cash balance end-point.""" +import json +from django.views import View +from django.http import JsonResponse + +from blocnote.apps.financialInputs.models import CashBalance +class CashBalanceView(View): + """Display, store and use cash balance data.""" + + model = CashBalance + + def get(self, request, building_id): + """Handle HTTP GET request. + + Fetch cash balance data from database if present. If not present, return empty. + + Args: + request: HTTP GET request. + building_id: id of the building. + """ + cash_balance_objs = self.model.objects.filter(building_id=building_id) + fields = [ + 'statement_date', + 'is_from_balance_sheet', + 'balance_amount', + ] + instance = {} + if cash_balance_objs: + instance['result'] = [] + for obj in cash_balance_objs: + temp = {} + for field in fields: + temp[field] = obj.__dict__[field] + instance['result'].append(temp) + return JsonResponse({'instance': instance}) + return JsonResponse({}) + + def put(self, request, building_id): + """Handle HTTP PUT request. + + Retrieve the list of records from the request. Delete existing records and create new records. + + Args: + request: HTTP PUT request. + building_id: id of the building. + + Returns: + JsonResponse: An instance with OK. + """ + put = json.loads(request.body.decode()) + self.model.objects.filter( + building_id=building_id, + ).delete() + for record in put: + self.model.objects.create( + building_id=building_id, + statement_date=record['statement-date'], + is_from_balance_sheet=record['is-from-balance-sheet'], + balance_amount=record['balance'], + ) + return JsonResponse({}) -- GitLab From 3c3321d44d04831869df5880013cf9c41a586510 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 13:27:23 -0400 Subject: [PATCH 03/17] Remove cash balance code from old_views. --- blocnote/apps/financialInputs/old_views.py | 57 +--------------------- 1 file changed, 1 insertion(+), 56 deletions(-) diff --git a/blocnote/apps/financialInputs/old_views.py b/blocnote/apps/financialInputs/old_views.py index f3d9210..2f56874 100644 --- a/blocnote/apps/financialInputs/old_views.py +++ b/blocnote/apps/financialInputs/old_views.py @@ -1,10 +1,8 @@ import json -from datetime import date import datetime from django.shortcuts import render from django.http import JsonResponse -from django.db import connections from django.views import View from bpfin.back_end_call.back_end_inputs import monthly_bill, form_prior_income_table as prior_income_statement_table @@ -12,7 +10,7 @@ from bpfin.back_end_call.back_end_inputs import monthly_bill, form_prior_income_ from blocnote.lib.fetch_data import get_building_data from .models import ( - FinancingOverview, Bills, BillsOverview, EstimationAlgorithm, Liabilities, CashBalance, IncomeStatement, + FinancingOverview, Bills, BillsOverview, EstimationAlgorithm, Liabilities, IncomeStatement, LoanOptions, DefaultLoan, Lender, GrowthRate ) @@ -544,59 +542,6 @@ class LiabilitiesTable(View): return JsonResponse({}) -class CashBalanceView(View): - """Display, store and use cash balance data.""" - - model = CashBalance - - def get(self, request, building_id): - """Handle HTTP GET request. - - Fetch cash balance data from database if present. If not, return false status. - - Args: - request: HTTP GET request. - building_id: id of the building. - """ - objs = self.model.objects.filter(building_id=building_id) - instance = {} - if objs: - instance['result'] = [] - for obj in objs: - temp = {} - temp['date'] = obj.statement_date - temp['is_from_balance_sheet'] = obj.is_from_balance_sheet - temp['balance_amount'] = obj.balance_amount - instance['result'].append(temp) - return JsonResponse({'instance': instance}) - return JsonResponse({}) - - def put(self, request, building_id): - """Handle HTTP PUT request. - - Retrieve the list of records from the request. Delete existing records and create new records. - - Args: - request: HTTP PUT request. - building_id: id of the building. - - Returns: - JsonResponse: An instance with OK. - """ - put = json.loads(request.body.decode()) - self.model.objects.filter( - building_id=building_id, - ).delete() - for record in put: - self.model.objects.create( - building_id=building_id, - statement_date=record['statement-date'], - is_from_balance_sheet=record['is-from-balance-sheet'], - balance_amount=record['balance'], - ) - return JsonResponse({}) - - class IncomeStatementTable(View): """Income Statement table. -- GitLab From 23ded3d5673f9ef4a1ba7896f829d651888d6cd0 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 13:28:11 -0400 Subject: [PATCH 04/17] Change the date field in loadCashBalance to statement_date. --- .../apps/financialInputs/static/financialInputs/scripts/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 416d0b5..4dfc30f 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -564,7 +564,7 @@ function loadCashBalanceTable(instance) { if (instance) { for (var record=0; record < instance.result.length; record++) { balanceAmount = Number(instance.result[record].balance_amount); - date = instance.result[record].date; + date = instance.result[record].statement_date; isFromBalanceSheet = instance.result[record]['is_from_balance_sheet'] addCashBalanceRow(balanceAmount, date, isFromBalanceSheet); } -- GitLab From da6d8d24952d386e605ca11f4e78ec51583475b6 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 13:52:41 -0400 Subject: [PATCH 05/17] Update the field names in app.js for cash balance to reflect the same as in the database. --- .../financialInputs/static/financialInputs/scripts/app.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 4dfc30f..f22ba96 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -651,7 +651,7 @@ function submitCashBalanceForm(form) { var rowCount = table.rows.length; for (rowIndex = 1; rowIndex < rowCount; rowIndex++) { record = {}; - record['balance'] = table.rows[rowIndex].cells[1].children[0].value; + record['balance_amount'] = table.rows[rowIndex].cells[1].children[0].value; date = table.rows[rowIndex].cells[2].children[0].value; dateSplit = date.split('-'); dateDict = { @@ -665,13 +665,13 @@ function submitCashBalanceForm(form) { `; return false; } - record['statement-date'] = date; + record['statement_date'] = date; checkBox = table.rows[rowIndex].querySelectorAll('input[type="checkbox"]:checked'); if (checkBox.length > 0) { - record['is-from-balance-sheet'] = true; + record['is_from_balance_sheet'] = true; } else { - record['is-from-balance-sheet'] = false; + record['is_from_balance_sheet'] = false; } myResult.push(record); } -- GitLab From f4fed47e9780470445feaf2331636a0c7becc45a Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 13:53:36 -0400 Subject: [PATCH 06/17] Edit the cash balance put function to store each cash balance record as a dictionary instead of accessing each field seperately. --- blocnote/apps/financialInputs/views/cash_balance.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/blocnote/apps/financialInputs/views/cash_balance.py b/blocnote/apps/financialInputs/views/cash_balance.py index 4d550ca..db520c4 100644 --- a/blocnote/apps/financialInputs/views/cash_balance.py +++ b/blocnote/apps/financialInputs/views/cash_balance.py @@ -52,10 +52,6 @@ class CashBalanceView(View): building_id=building_id, ).delete() for record in put: - self.model.objects.create( - building_id=building_id, - statement_date=record['statement-date'], - is_from_balance_sheet=record['is-from-balance-sheet'], - balance_amount=record['balance'], - ) + record['building_id'] = building_id + self.model.objects.create(**record) return JsonResponse({}) -- GitLab From 00cd923f602e5580daa9f59a6d5874a4727532cd Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 14:46:08 -0400 Subject: [PATCH 07/17] Create a new js file for cash balance and move code from app.js to this file. Fix all indentation issues and change all var and undeclared variables to let or const. --- .../financialInputs/scripts/cashBalance.js | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js new file mode 100644 index 0000000..d871022 --- /dev/null +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js @@ -0,0 +1,148 @@ +/**Send HTTP GET request to obtain all cash balance records. */ +function getCashBalanceForm() { + request('cash-balance/', { + method: 'GET', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json' + }, + }).then(res => { + loadCashBalanceTable(res.payload.instance); + }); +} + +/**Load cash balance table with initial values. Takes response from GET request to construct the table. */ +function loadCashBalanceTable(instance) { + if (instance) { + for (let record=0; record < instance.result.length; record++) { + let balanceAmount = Number(instance.result[record].balance_amount); + let date = instance.result[record].statement_date; + let isFromBalanceSheet = instance.result[record]['is_from_balance_sheet']; + addCashBalanceRow(balanceAmount, date, isFromBalanceSheet); + } + } + else { + addCashBalanceRow('', '', false); + } +} + +/** + * Return check box to indicate if a record is from balance sheet or not. Takes a boolean as argument which + * is true if the record is from balance, else not. This function returns a checked checkbox if it is from balance + * sheet, else an unchecked checkbox. + */ +function getCashBalanceCheckBox(isFromBalanceSheet) { + let check = ''; + if (isFromBalanceSheet) { + check = 'checked'; + } + const text = ` + + `; + return text; +} + +/**Delete a given row in the cash balance table. Takes in the row index as argument. */ +function deleteCashBalanceRow(row) { + const result = confirm('ARE YOU SURE YOU WANT TO DELETE THIS ROW?'); + if (result) { + const table = document.querySelector('#cash-balance-table'); + const rowCount = table.rows.length; + if (rowCount === 2) { + alert('YOU MUST HAVE ATLEAST ONE CASH BALANCE ENTRY!'); + return false; + } + table.deleteRow(row); + for (let rowInd = 1; rowInd < table.rows.length; rowInd++) { + row = table.rows.item(rowInd).cells; + row.item(0).innerHTML = rowInd; + } + } + return false; +} + +/** + * Add a row to the cash balance table. Take balance amount, date and boolean that represents if a record is from + * balance sheet or not. + */ +function addCashBalanceRow(balance, date, isFromBalanceSheet) { + const table = document.querySelector('#cash-balance-table tbody'); + const rowCount = table.rows.length; + const row = table.insertRow(rowCount); + let cell = row.insertCell(0); + cell.innerHTML = `${rowCount + 1}`; + cell = row.insertCell(1); + cell.innerHTML = ` + + `; + cell = row.insertCell(2); + cell.innerHTML = ` + + `; + cell = row.insertCell(3); + cell.innerHTML = getCashBalanceCheckBox(isFromBalanceSheet); + cell = row.insertCell(4); + cell.innerHTML = ` + + `; + return false; +} + +// Watch cash balance to display/erase error/success messages. +const cashBalanceForm = document.querySelector('#cash-balance-form'); +cashBalanceForm.onchange = function() { + document.querySelector('#cash-balance-warning-message').innerHTML = ''; +}; + +/**Make PUT request with data from the cash balance table. */ +function submitCashBalanceForm(form) { + let myResult = []; + const table = document.querySelector('#cash-balance-table'); + const rowCount = table.rows.length; + for (let rowIndex = 1; rowIndex < rowCount; rowIndex++) { + let record = {}; + record['balance_amount'] = table.rows[rowIndex].cells[1].children[0].value; + const date = table.rows[rowIndex].cells[2].children[0].value; + const dateSplit = date.split('-'); + const dateDict = { + 'day': Number(dateSplit[2]), + 'month': Number(dateSplit[1]), + 'year': Number(dateSplit[0]), + }; + if (!validateDate(dateDict, todaysDate)) { + document.querySelector('#cash-balance-warning-message').innerHTML = ` + Statement date cannot be in the future! + `; + return false; + } + record['statement_date'] = date; + const checkBox = table.rows[rowIndex].querySelectorAll('input[type="checkbox"]:checked'); + if (checkBox.length > 0) { + record['is_from_balance_sheet'] = true; + } + else { + record['is_from_balance_sheet'] = false; + } + myResult.push(record); + } + request('cash-balance/', { + method: 'PUT', + credentials: 'same-origin', + body: JSON.stringify(myResult), + headers: new Headers({ + 'Content-Type': 'application/json', + 'X-CSRFToken': Cookies.get('csrftoken') + }) + }).then(res => { + if (!res.payload.err) { + document.querySelector('#cash-balance-warning-message').innerHTML = ` + Saved + `; + } + }); + return false; +} + +getCashBalanceForm(); -- GitLab From 775fa7915edb559f84537cd40ab9546e2429feb9 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 14:46:46 -0400 Subject: [PATCH 08/17] Remove cash balance code from app.js file. --- .../static/financialInputs/scripts/app.js | 149 ------------------ 1 file changed, 149 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index f22ba96..5cc9ab2 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -12,7 +12,6 @@ loadBillsOverview(); getIncomeStatementTable(); getLiabilitiesTable(); -getCashBalanceForm(); getLoanOptionsTable(); /** Validate that commissioning date is after the construction start date. */ @@ -545,154 +544,6 @@ function getLiabilitiesTable() { }); } -/**Send HTTP GET request to obtain all cash balance records. */ -function getCashBalanceForm() { - request(`cash-balance/`, { - method: 'GET', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json' - }, - }).then(res => { - loadCashBalanceTable(res.payload.instance); - }); -} - -/**Load cash balance table with initial values. Takes response from GET request to construct the table. */ -function loadCashBalanceTable(instance) { - table = document.querySelector('#cash-balance-table'); - if (instance) { - for (var record=0; record < instance.result.length; record++) { - balanceAmount = Number(instance.result[record].balance_amount); - date = instance.result[record].statement_date; - isFromBalanceSheet = instance.result[record]['is_from_balance_sheet'] - addCashBalanceRow(balanceAmount, date, isFromBalanceSheet); - } - } - else { - addCashBalanceRow('', '', false); - } -} - -/** - * Return check box to indicate if a record is from balance sheet or not. Takes a boolean as argument which - * is true if the record is from balance, else not. This function returns a checked checkbox if it is from balance - * sheet, else an unchecked checkbox. - */ -function getCashBalanceCheckBox(isFromBalanceSheet) { - var check = ""; - if (isFromBalanceSheet) { - check = "checked"; - } - var text = ` - - `; - return text; -} - -/**Delete a given row in the cash balance table. Takes in the row index as argument. */ -function deleteCashBalanceRow(row) { - var result = confirm('ARE YOU SURE YOU WANT TO DELETE THIS ROW?'); - if (result) { - table = document.querySelector('#cash-balance-table'); - var rowCount = table.rows.length; - if (rowCount === 2) { - alert('YOU MUST HAVE ATLEAST ONE CASH BALANCE ENTRY!'); - return false; - } - table.deleteRow(row); - for (rowInd = 1; rowInd < table.rows.length; rowInd++) { - row = table.rows.item(rowInd).cells; - row.item(0).innerHTML = rowInd; - } - } - return false; -} - -/** - * Add a row to the cash balance table. Take balance amount, date and boolean that represents if a record is from - * balance sheet or not. - */ -function addCashBalanceRow(balance, date, isFromBalanceSheet) { - table = document.querySelector('#cash-balance-table tbody'); - var rowCount = table.rows.length; - var row = table.insertRow(rowCount); - var cell = row.insertCell(0); - cell.innerHTML = `${rowCount + 1}`; - cell = row.insertCell(1); - cell.innerHTML = ` - - `; - cell = row.insertCell(2); - cell.innerHTML = ` - - `; - cell = row.insertCell(3); - cell.innerHTML = getCashBalanceCheckBox(isFromBalanceSheet); - cell = row.insertCell(4) - cell.innerHTML = ` - - `; - return false; -} - -// Watch cash balance to display/erase error/success messages. -var cashBalanceForm = document.querySelector('#cash-balance-form'); -cashBalanceForm.onchange = function() { - document.querySelector('#cash-balance-warning-message').innerHTML = ''; -} - -/**Make PUT request with data from the cash balance table. */ -function submitCashBalanceForm(form) { - var myResult = []; - table = document.querySelector('#cash-balance-table'); - var rowCount = table.rows.length; - for (rowIndex = 1; rowIndex < rowCount; rowIndex++) { - record = {}; - record['balance_amount'] = table.rows[rowIndex].cells[1].children[0].value; - date = table.rows[rowIndex].cells[2].children[0].value; - dateSplit = date.split('-'); - dateDict = { - 'day': Number(dateSplit[2]), - 'month': Number(dateSplit[1]), - 'year': Number(dateSplit[0]), - } - if (!validateDate(dateDict, todaysDate)) { - document.querySelector('#cash-balance-warning-message').innerHTML = ` - Statement date cannot be in the future! - `; - return false; - } - record['statement_date'] = date; - checkBox = table.rows[rowIndex].querySelectorAll('input[type="checkbox"]:checked'); - if (checkBox.length > 0) { - record['is_from_balance_sheet'] = true; - } - else { - record['is_from_balance_sheet'] = false; - } - myResult.push(record); - } - request('cash-balance/', { - method: 'PUT', - credentials: 'same-origin', - body: JSON.stringify(myResult), - headers: new Headers({ - 'Content-Type': 'application/json', - 'X-CSRFToken': Cookies.get('csrftoken') - }) - }).then(res => { - if (!res.payload.err) { - document.querySelector('#cash-balance-warning-message').innerHTML = ` - Saved - `; - } - }); - return false; -} - /** * Load income statement table. The column heading are displayed first along with the table body. The growth rate * drop down box is displayed as well. -- GitLab From 0e46602c236594b2ec9a436839ed0310894e9830 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 14:47:44 -0400 Subject: [PATCH 09/17] Add the cashBalance script to the index.html of the financialInputs. --- .../apps/financialInputs/templates/financialInputs/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/blocnote/apps/financialInputs/templates/financialInputs/index.html b/blocnote/apps/financialInputs/templates/financialInputs/index.html index b9b7c29..dfb6ab0 100644 --- a/blocnote/apps/financialInputs/templates/financialInputs/index.html +++ b/blocnote/apps/financialInputs/templates/financialInputs/index.html @@ -58,4 +58,5 @@ + {% endblock %} -- GitLab From b8d2ac4b2832cbe19eb46e16639bdab161cef993 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 15:31:17 -0400 Subject: [PATCH 10/17] Use js in-built date object. Remove unused variable from function. --- .../static/financialInputs/scripts/cashBalance.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js index d871022..bfbdca0 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js @@ -14,7 +14,7 @@ function getCashBalanceForm() { /**Load cash balance table with initial values. Takes response from GET request to construct the table. */ function loadCashBalanceTable(instance) { if (instance) { - for (let record=0; record < instance.result.length; record++) { + for (let record = 0; record < instance.result.length; record++) { let balanceAmount = Number(instance.result[record].balance_amount); let date = instance.result[record].statement_date; let isFromBalanceSheet = instance.result[record]['is_from_balance_sheet']; @@ -97,21 +97,17 @@ cashBalanceForm.onchange = function() { }; /**Make PUT request with data from the cash balance table. */ -function submitCashBalanceForm(form) { +function submitCashBalanceForm() { let myResult = []; const table = document.querySelector('#cash-balance-table'); const rowCount = table.rows.length; for (let rowIndex = 1; rowIndex < rowCount; rowIndex++) { let record = {}; record['balance_amount'] = table.rows[rowIndex].cells[1].children[0].value; + const todaysDate = new Date(); const date = table.rows[rowIndex].cells[2].children[0].value; - const dateSplit = date.split('-'); - const dateDict = { - 'day': Number(dateSplit[2]), - 'month': Number(dateSplit[1]), - 'year': Number(dateSplit[0]), - }; - if (!validateDate(dateDict, todaysDate)) { + let currentDate = new Date(date); + if (currentDate > todaysDate) { document.querySelector('#cash-balance-warning-message').innerHTML = ` Statement date cannot be in the future! `; -- GitLab From 6142b8b8a77bdc0b44c100743279477b515c2ff0 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 15:31:49 -0400 Subject: [PATCH 11/17] Remove form from cashBalance html since it is not being used for anything. --- .../financialInputs/templates/financialInputs/cashBalance.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html index e383f8b..2f2af77 100644 --- a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html +++ b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html @@ -1,7 +1,7 @@

Cash Balance

-
+
-- GitLab From df5c0a5e58293a0318bfeb1707e9d4a02c9d0b34 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 22 Jun 2017 15:42:24 -0400 Subject: [PATCH 12/17] Add blank lines to split import statements into groups. --- blocnote/apps/financialInputs/views/cash_balance.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/blocnote/apps/financialInputs/views/cash_balance.py b/blocnote/apps/financialInputs/views/cash_balance.py index db520c4..563cd06 100644 --- a/blocnote/apps/financialInputs/views/cash_balance.py +++ b/blocnote/apps/financialInputs/views/cash_balance.py @@ -1,9 +1,12 @@ """Views for the cash balance end-point.""" import json + from django.views import View from django.http import JsonResponse from blocnote.apps.financialInputs.models import CashBalance + + class CashBalanceView(View): """Display, store and use cash balance data.""" -- GitLab From 1bea0a4982b754b33431e901d2358afe19d8a789 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Mon, 26 Jun 2017 10:04:00 -0400 Subject: [PATCH 13/17] Change according to Conrad's comment for is_from_balance_sheet logic. --- .../static/financialInputs/scripts/cashBalance.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js index bfbdca0..a1786cd 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js @@ -115,12 +115,7 @@ function submitCashBalanceForm() { } record['statement_date'] = date; const checkBox = table.rows[rowIndex].querySelectorAll('input[type="checkbox"]:checked'); - if (checkBox.length > 0) { - record['is_from_balance_sheet'] = true; - } - else { - record['is_from_balance_sheet'] = false; - } + record['is_from_balance_sheet'] = (checkBox.length > 0) myResult.push(record); } request('cash-balance/', { -- GitLab From 514a43d0ae1a4ebcb814e84b8a7595d2f65ace20 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Mon, 26 Jun 2017 10:05:23 -0400 Subject: [PATCH 14/17] Fix indentation issue in views. --- blocnote/apps/financialInputs/views/cash_balance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocnote/apps/financialInputs/views/cash_balance.py b/blocnote/apps/financialInputs/views/cash_balance.py index 563cd06..9f12c0a 100644 --- a/blocnote/apps/financialInputs/views/cash_balance.py +++ b/blocnote/apps/financialInputs/views/cash_balance.py @@ -53,7 +53,7 @@ class CashBalanceView(View): put = json.loads(request.body.decode()) self.model.objects.filter( building_id=building_id, - ).delete() + ).delete() for record in put: record['building_id'] = building_id self.model.objects.create(**record) -- GitLab From a744065796d4e510ec8a37b147474677f9ecc934 Mon Sep 17 00:00:00 2001 From: Adarsh Date: Mon, 26 Jun 2017 13:36:17 -0400 Subject: [PATCH 15/17] Create new model form for cash balance with all fields. --- blocnote/apps/financialInputs/forms.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/blocnote/apps/financialInputs/forms.py b/blocnote/apps/financialInputs/forms.py index ce532fc..65c2e36 100644 --- a/blocnote/apps/financialInputs/forms.py +++ b/blocnote/apps/financialInputs/forms.py @@ -1,6 +1,6 @@ """Forms to render and validate for financial-inputs endpoint.""" from django.forms import ModelForm -from blocnote.apps.financialInputs.models import FinancingOverview, CustomerPreference +from blocnote.apps.financialInputs.models import FinancingOverview, CustomerPreference, CashBalance class ProformaInputsForm(ModelForm): @@ -33,3 +33,11 @@ class CustomerPreferenceForm(ModelForm): 'expected_payback', 'expected_net_noi_dscr', ] + + +class CashBalanceForm(ModelForm): + """Define the form to validate cash balance entries.""" + + class Meta: + model = CashBalance + fields = '__all__' -- GitLab From 149c95818c355f560d9513b249844664ef0197ef Mon Sep 17 00:00:00 2001 From: Adarsh Date: Mon, 26 Jun 2017 14:18:21 -0400 Subject: [PATCH 16/17] Use cash balance form to validate the values provided. If for any entry the values are not valid, store them in a dictionary an send this dictionary to the frontend for display. --- blocnote/apps/financialInputs/views/cash_balance.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/blocnote/apps/financialInputs/views/cash_balance.py b/blocnote/apps/financialInputs/views/cash_balance.py index 9f12c0a..cde2558 100644 --- a/blocnote/apps/financialInputs/views/cash_balance.py +++ b/blocnote/apps/financialInputs/views/cash_balance.py @@ -5,6 +5,7 @@ from django.views import View from django.http import JsonResponse from blocnote.apps.financialInputs.models import CashBalance +from blocnote.apps.financialInputs.forms import CashBalanceForm class CashBalanceView(View): @@ -54,7 +55,15 @@ class CashBalanceView(View): self.model.objects.filter( building_id=building_id, ).delete() + error_dict = {} for record in put: record['building_id'] = building_id - self.model.objects.create(**record) + form = CashBalanceForm(record) + if form.is_valid(): + self.model.objects.create(**record) + else: + for field, error in form.errors.items(): + error_dict[field] = error + if error_dict: + return JsonResponse(error_dict, status=400) return JsonResponse({}) -- GitLab From c9804780c6005d2a116c5d8f80d66f9d0cd370dd Mon Sep 17 00:00:00 2001 From: Adarsh Date: Mon, 26 Jun 2017 14:19:12 -0400 Subject: [PATCH 17/17] Update frontend to expect an error dictionary and display all the errors for the user. --- .../static/financialInputs/scripts/cashBalance.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js index a1786cd..524f227 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/cashBalance.js @@ -127,11 +127,22 @@ function submitCashBalanceForm() { 'X-CSRFToken': Cookies.get('csrftoken') }) }).then(res => { - if (!res.payload.err) { + if (!res.err) { document.querySelector('#cash-balance-warning-message').innerHTML = ` Saved `; } + else { + res.err.responseBody.then((error) => { + let errorMsg = ''; + Object.keys(error).forEach((key) => { + errorMsg += `${key}: ${error[key]}.\n`; + }); + document.querySelector('#cash-balance-warning-message').innerHTML = ` + ${errorMsg} + `; + }); + } }); return false; } -- GitLab