Skip to content

Commit

Permalink
Merge pull request #145 from agstrike/issue-133-exclude-account
Browse files Browse the repository at this point in the history
Exclude accounts with `showOnDashboard = False`
  • Loading branch information
simhnna authored Oct 31, 2022
2 parents 8aebe5c + 841fc15 commit 09f27ec
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 16 deletions.
21 changes: 17 additions & 4 deletions silverstrike/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,28 @@ def get_account_balance(request, account_id, dstart, dend):


@login_required
def get_balances(request, dstart, dend):
def get_balances(request, dstart, dend, include_non_dashboard_accounts=False):
return _get_balances(request, dstart, dend, False)


@login_required
def get_non_dashboard_balances(request, dstart, dend, include_non_dashboard_accounts=False):
return _get_balances(request, dstart, dend, True)


def _get_balances(request, dstart, dend, include_non_dashboard_accounts=False):
try:
dstart = datetime.datetime.strptime(dstart, '%Y-%m-%d').date()
dend = datetime.datetime.strptime(dend, '%Y-%m-%d').date()
except ValueError:
return HttpResponseBadRequest(_('Invalid date format, expected yyyy-mm-dd'))
balance = Split.objects.personal().exclude_transfers().filter(date__lt=dstart).aggregate(
models.Sum('amount'))['amount__sum'] or 0
splits = Split.objects.personal().exclude_transfers().date_range(dstart, dend).order_by('date')
if include_non_dashboard_accounts:
account_objects = Split.objects.personal()
else:
account_objects = Split.objects.personal_dashboard()
balance = account_objects.exclude_transfers().filter(date__lt=dstart).aggregate(
models.Sum('amount'))['amount__sum'] or 0
splits = account_objects.exclude_transfers().date_range(dstart, dend).order_by('date')
data_points = []
labels = []
days = (dend - dstart).days
Expand Down
4 changes: 4 additions & 0 deletions silverstrike/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def income(self):
def expense(self):
return self.filter(opposing_account__account_type=AccountType.FOREIGN, amount__lt=0)

def personal_dashboard(self):
return self.filter(account__account_type=AccountType.PERSONAL,
account__show_on_dashboard=True)

def date_range(self, dstart, dend):
return self.filter(date__gte=dstart, date__lte=dend)

Expand Down
59 changes: 56 additions & 3 deletions silverstrike/templates/silverstrike/charts.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ <h1>{% trans 'Charts' %}
<button id="all-time" class="btn btn-s btn-default range-chooser">{% trans 'All Time' %}</button>
<!--button id="custom" class="btn btn-s btn-default range-chooser">Custom Range</button-->
</div>
<div class="row text-right" style="margin-right: 18px;">
<span>
<label for="include_non_dashboard_accounts">{% trans 'Include Non-dashboard Accounts' %}</label>
<input type="checkbox" id="include_non_dashboard_accounts"></input>
</span>
</div>
</div>
</div>
<div class="row">
Expand Down Expand Up @@ -204,10 +210,46 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>
var six_months = 2;
var twelve_months = 3;
var all_time = 10;
var current_chart_range = three_months;
var accountChartData = new Array(4);
var balanceChartData = new Array(4);
var categoryChartData = new Array(4);

function get_balance_data(url_data, callback) {
url = "";
if ($('#include_non_dashboard_accounts')[0].checked) {
switch (url_data) {
case one_month:
url = "{% url 'api_non_dashboard_balance' first_day_of_month last_day_of_month %}"
break;
case three_months:
url = "{% url 'api_non_dashboard_balance' minus_3_months today %}"
break;
case six_months:
url = "{% url 'api_non_dashboard_balance' minus_6_months today %}"
break;
case twelve_months:
url = "{% url 'api_non_dashboard_balance' minus_12_months today %}"
break;
}
} else {
switch (url_data) {
case one_month:
url = "{% url 'api_balance' first_day_of_month last_day_of_month %}"
break;
case three_months:
url = "{% url 'api_balance' minus_3_months today %}"
break;
case six_months:
url = "{% url 'api_balance' minus_6_months today %}"
break;
case twelve_months:
url = "{% url 'api_balance' minus_12_months today %}"
break;
}
}
$.getJSON(url, {}, function(res, status) { callback(res, status) });
}

// initialize
$.getJSON("{% url 'api_accounts_balance' minus_3_months today %}", {}, function(res, status) {
Expand All @@ -225,6 +267,13 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>
});

// update charts
$('#include_non_dashboard_accounts').click(function() {
get_balance_data(current_chart_range, function(res, status) {
drawBalances(res);
balanceChartData[three_months] = res;
});
});

$('#all-time').click(function() {
if (accountChartData[twelve_months] == null) {
$.getJSON("{% url 'api_accounts_balance' all_time today %}", {}, function(res, status) {
Expand Down Expand Up @@ -254,6 +303,7 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>


$('#12month').click(function() {
current_chart_range = twelve_months;
if (accountChartData[twelve_months] == null) {
$.getJSON("{% url 'api_accounts_balance' minus_12_months today %}", {}, function(res, status) {
updateAccountChart(res);
Expand All @@ -263,7 +313,7 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>
updateAccountChart(accountChartData[twelve_months]);
}
if (balanceChartData[twelve_months] == null) {
$.getJSON("{% url 'api_balance' minus_12_months today %}", {}, function(res, status) {
get_balance_data(twelve_months, function(res, status) {
updateBalanceChart(res);
balanceChartData[twelve_months] = res;
});
Expand All @@ -282,6 +332,7 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>


$('#6month').click(function() {
current_chart_range = six_months;
if (accountChartData[six_months] == null) {
$.getJSON("{% url 'api_accounts_balance' minus_6_months today %}", {}, function(res, status) {
updateAccountChart(res);
Expand All @@ -292,7 +343,7 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>
}

if (balanceChartData[six_months] == null) {
$.getJSON("{% url 'api_balance' minus_6_months today %}", {}, function(res, status) {
get_balance_data(six_months, function(res, status) {
updateBalanceChart(res);
balanceChartData[six_months] = res;
});
Expand All @@ -311,13 +362,15 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>
});

$('#3month').click(function() {
current_chart_range = three_months;
updateAccountChart(accountChartData[three_months]);
updateBalanceChart(balanceChartData[three_months]);
updateCategoryChart(categoryChartData[three_months]);
});


$('#currentMonth').click(function() {
current_chart_range = one_month;
if (accountChartData[one_month] == null) {
$.getJSON("{% url 'api_accounts_balance' first_day_of_month last_day_of_month %}", {}, function(res, status) {
updateAccountChart(res);
Expand All @@ -328,7 +381,7 @@ <h3 class="box-title">{% trans 'Spending by category' %}</h3>
}

if (balanceChartData[one_month] == null) {
$.getJSON("{% url 'api_balance' first_day_of_month last_day_of_month %}", {}, function(res, status) {
get_balance_data(one_month, function(res, status) {
updateBalanceChart(res);
balanceChartData[one_month] = res;
});
Expand Down
29 changes: 25 additions & 4 deletions silverstrike/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import json
from datetime import date

from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse

from silverstrike.models import Account, AccountType
from silverstrike.models import Account, AccountType, Transaction
from silverstrike.tests import create_transaction


class ApiTests(TestCase):
def setUp(self):
User.objects.create_superuser(username='admin', email='email@example.com', password='pass')
self.client.login(username='admin', password='pass')
Account.objects.bulk_create(
[Account(name=t[1], account_type=t[0],
show_on_dashboard=True) for t in AccountType.choices])
self.personal = Account.objects.create(name="Personal", account_type=AccountType.PERSONAL,
show_on_dashboard=True)
self.foreign = Account.objects.create(name="Foreign", account_type=AccountType.FOREIGN,
show_on_dashboard=True)
self.system = Account.objects.create(name="System", account_type=AccountType.SYSTEM,
show_on_dashboard=True)
self.cash = Account.objects.create(name="Cash", account_type=AccountType.PERSONAL,
show_on_dashboard=False)
create_transaction('meh', self.foreign, self.personal, 1000,
Transaction.DEPOSIT, date(2022, 1, 2))
create_transaction('meh', self.foreign, self.cash, 1000,
Transaction.DEPOSIT, date(2022, 1, 3))

def test_get_accounts_return_value(self):
for t in AccountType.choices:
Expand All @@ -23,6 +34,16 @@ def test_get_accounts_return_value(self):
queryset = queryset.exclude(account_type=AccountType.SYSTEM)
self.assertEqual(data, list(queryset.values_list('name', flat=True)))

def test_get_balance_data_excludes_non_dashboard_accounts(self):
response = self.client.get(reverse('api_balance', args=['2022-01-02', '2022-01-02']))
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(['1000.00'], data.get('data'))

def test_get_non_dashboard_balance_return_value(self):
response = self.client.get(reverse('api_non_dashboard_balance', args=['2022-01-03', '2022-01-03']))
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(['2000.00'], data.get('data'))

def test_get_account_balance_invalid_date(self):
response = self.client.get(reverse('api_account_balance', args=['1', '2019-01-01', '20']))
self.assertEqual(response.status_code, 400)
Expand Down
11 changes: 10 additions & 1 deletion silverstrike/tests/views/test_IndexView.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def setUp(self):
User.objects.create_superuser(username='admin', email='email@example.com', password='pass')
self.client.login(username='admin', password='pass')
self.account = Account.objects.create(name='first account', show_on_dashboard=True)
self.personal = Account.objects.create(name='personal')
self.personal = Account.objects.create(name='personal', show_on_dashboard=True)
self.cash = Account.objects.create(name='cash', show_on_dashboard=False)
self.foreign = Account.objects.create(
name="foreign account", account_type=AccountType.FOREIGN)

Expand Down Expand Up @@ -44,6 +45,14 @@ def test_balance_does_not_count_future_transactions(self):
context = self.client.get(reverse('index')).context
self.assertEqual(context['balance'], 0)

def test_balance_does_not_count_non_dashboard_accounts(self):
create_transaction('meh', self.foreign, self.account, 1000,
Transaction.DEPOSIT, date(2015, 1, 1))
create_transaction('meh', self.cash, self.foreign, 500,
Transaction.DEPOSIT, date(2015, 1, 1))
context = self.client.get(reverse('index')).context
self.assertEqual(context['balance'], 1000)

def test_income(self):
pass

Expand Down
2 changes: 2 additions & 0 deletions silverstrike/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@

path('api/accounts/<account_type>/', api.get_accounts, name='api_accounts'),
path('api/balance/<dstart>/<dend>/', api.get_balances, name='api_balance'),
path('api/non_dashboard_balance/<dstart>/<dend>/',
api.get_non_dashboard_balances, name='api_non_dashboard_balance'),
path('api/account/<int:account_id>/balance/<dstart>/<dend>/',
api.get_account_balance, name='api_account_balance'),
path('api/accounts_balance/<dstart>/<dend>/',
Expand Down
6 changes: 3 additions & 3 deletions silverstrike/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def get_context_data(self, **kwargs):
dend = last_day_of_month(dstart)
context = super().get_context_data(**kwargs)
context['menu'] = 'home'
queryset = Split.objects.personal().past()
context['balance'] = round(queryset.aggregate(
models.Sum('amount'))['amount__sum'] or 0, 2)
queryset = Split.objects.personal_dashboard().past()
context['balance'] = queryset.aggregate(
models.Sum('amount'))['amount__sum'] or 0
queryset = queryset.date_range(dstart, dend)
context['income'] = abs(queryset.income().aggregate(
models.Sum('amount'))['amount__sum'] or 0)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ commands =
codecov

[flake8]
max-line-length = 100
max-line-length = 120
exclude = silverstrike/migrations,silverstrike/views/__init__.py

0 comments on commit 09f27ec

Please sign in to comment.