Skip to content

Commit

Permalink
Merge pull request #190 from edelgm6/debug-toolbar
Browse files Browse the repository at this point in the history
Debug toolbar
  • Loading branch information
edelgm6 authored Jan 24, 2024
2 parents 48eea6b + 45976aa commit 7e5e6d8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
4 changes: 3 additions & 1 deletion api/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class TaxChargeFactory:

@staticmethod
def create_bulk_tax_charges(date):
tax_charges = TaxCharge.objects.filter(date=date)
for value, _ in TaxCharge.Type.choices:
if not TaxCharge.objects.filter(date=date, type=value).exists():
existing_tax_charge = [charge for charge in tax_charges if charge.type == value]
if len(existing_tax_charge) == 0:
TaxCharge.objects.create(date=date, type=value, amount=0)
14 changes: 9 additions & 5 deletions api/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ def save(self):

class BaseJournalEntryItemFormset(BaseModelFormSet):

def __init__(self, *args, **kwargs):
open_accounts = Account.objects.filter(is_closed=False)
open_accounts_choices = [(account.name, account.name) for account in open_accounts]
kwargs['form_kwargs'] = {'open_accounts_choices': open_accounts_choices}
super(BaseJournalEntryItemFormset, self).__init__(*args, **kwargs)

def get_entry_total(self):
total = 0
for form in self.forms:
Expand Down Expand Up @@ -322,12 +328,10 @@ class Meta:
fields = ('account', 'amount')

def __init__(self, *args, **kwargs):
open_accounts_choices = kwargs.pop('open_accounts_choices', [])
super(JournalEntryItemForm, self).__init__(*args, **kwargs)
self.fields['amount'].localize = True
closed_accounts = Account.objects.filter(is_closed=False)
self.fields['account'].choices = [
(account.name, account.name) for account in closed_accounts
]
self.fields['account'].choices = open_accounts_choices

# Resolve the account name for the bound form
if self.instance.pk and self.instance.account:
Expand Down Expand Up @@ -408,7 +412,7 @@ def get_transactions(self):
accounts=data['account'],
date_from=data.get('date_from'),
date_to=data.get('date_to')
)
).select_related('account')
return queryset


Expand Down
18 changes: 9 additions & 9 deletions api/views/reconciliation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

class ReconciliationTableMixin:

def _get_current_balance(self, reconciliation):
balance_sheet = BalanceSheet(reconciliation.date)
balance = balance_sheet.get_balance(reconciliation.account)
def _get_current_balance(self, balance_sheet, account):
balance = balance_sheet.get_balance(account)
return balance

def get_reconciliation_html(self, reconciliations):
reconciliations = reconciliations.order_by('account')
def get_reconciliation_html(self, reconciliations, date):
reconciliations = reconciliations.select_related('account').order_by('account')

balance_sheet = BalanceSheet(date)
for reconciliation in reconciliations:
reconciliation.current_balance = self._get_current_balance(reconciliation)
reconciliation.current_balance = self._get_current_balance(balance_sheet, reconciliation.account)

ReconciliationFormset = modelformset_factory(Reconciliation, ReconciliationForm, extra=0)

Expand Down Expand Up @@ -59,7 +59,7 @@ def get(self, request, *args, **kwargs):
ReconciliationFactory.create_bulk_reconciliations(date=form.cleaned_data['date'])
reconciliations = form.get_reconciliations()

reconciliations_table = self.get_reconciliation_html(reconciliations)
reconciliations_table = self.get_reconciliation_html(reconciliations, date=form.cleaned_data['date'])
return HttpResponse(reconciliations_table)

# Loads full page
Expand All @@ -74,7 +74,7 @@ def get(self, request, *args, **kwargs):
ReconciliationFactory.create_bulk_reconciliations(date=initial_date)

reconciliations = Reconciliation.objects.filter(date=initial_date)
reconciliation_table = self.get_reconciliation_html(reconciliations)
reconciliation_table = self.get_reconciliation_html(reconciliations, date=initial_date)
context = {
'reconciliation_table': reconciliation_table,
'filter_form': render_to_string(
Expand All @@ -101,6 +101,6 @@ def post(self, request):
if filter_form.is_valid():
reconciliations = filter_form.get_reconciliations()

reconciliation_table = self.get_reconciliation_html(reconciliations)
reconciliation_table = self.get_reconciliation_html(reconciliations, date=filter_form.cleaned_data['date'])

return HttpResponse(reconciliation_table)
34 changes: 23 additions & 11 deletions api/views/tax_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
from django.views import View
from django.shortcuts import get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin
from api.models import TaxCharge
from api.models import TaxCharge
from api.forms import TaxChargeFilterForm, TaxChargeForm
from api.statement import IncomeStatement
from api.factories import TaxChargeFactory
from api import utils


class TaxChargeMixIn:

def _add_tax_rate_and_charge(self, tax_charge, current_taxable_income=None):
last_day_of_month = tax_charge.date
first_day_of_month = date(last_day_of_month.year, last_day_of_month.month, 1)
taxable_income = IncomeStatement(tax_charge.date, first_day_of_month).get_taxable_income()
def _get_taxable_income(self, end_date):
first_day_of_month = date(end_date.year, end_date.month, 1)
taxable_income = IncomeStatement(end_date, first_day_of_month).get_taxable_income()
return taxable_income

def _add_tax_rate_and_charge(self, tax_charge, taxable_income, current_taxable_income=None):
tax_charge.taxable_income = taxable_income
tax_charge.tax_rate = None if taxable_income == 0 else tax_charge.amount / taxable_income
if current_taxable_income and tax_charge.tax_rate:
Expand Down Expand Up @@ -44,7 +47,7 @@ def get_tax_form_html(self, tax_charge=None, last_day_of_month=None):

current_taxable_income = income_statement.get_taxable_income()

for latest_tax_charge in [latest_federal_tax_charge,latest_state_tax_charge]:
for latest_tax_charge in [latest_federal_tax_charge, latest_state_tax_charge]:
if latest_tax_charge:
self._add_tax_rate_and_charge(latest_tax_charge, current_taxable_income)

Expand All @@ -60,11 +63,17 @@ def get_tax_form_html(self, tax_charge=None, last_day_of_month=None):
form_html = render_to_string(form_template, context)
return form_html

def get_tax_table_html(self, tax_charges):
def get_tax_table_html(self, tax_charges, end_date):

tax_charges = tax_charges.order_by('date','type')
tax_charges = tax_charges.select_related('transaction', 'transaction__account').order_by('date', 'type')
tax_dates = []
taxable_income = None
for tax_charge in tax_charges:
self._add_tax_rate_and_charge(tax_charge)
# Limit the number of IncomeStatement objects we need to make
if tax_charge.date not in tax_dates:
taxable_income = self._get_taxable_income(tax_charge.date)
self._add_tax_rate_and_charge(tax_charge=tax_charge, taxable_income=taxable_income)
tax_charge.transaction_string = str(tax_charge.transaction)

tax_charge_table_html = render_to_string(
'api/tables/tax-table.html',
Expand All @@ -73,6 +82,7 @@ def get_tax_table_html(self, tax_charges):

return tax_charge_table_html


class TaxChargeTableView(TaxChargeMixIn, LoginRequiredMixin, View):
login_url = '/login/'
redirect_field_name = 'next'
Expand All @@ -92,6 +102,7 @@ def get(self, request, *args, **kwargs):

return HttpResponse(html)


class TaxChargeFormView(TaxChargeMixIn, LoginRequiredMixin, View):
login_url = '/login/'
redirect_field_name = 'next'
Expand All @@ -106,6 +117,7 @@ def get(self, request, pk=None, *args, **kwargs):

return HttpResponse(form_html)


# Loads full page
class TaxesView(TaxChargeMixIn, LoginRequiredMixin, View):
login_url = '/login/'
Expand All @@ -117,10 +129,10 @@ def get(self, request, *args, **kwargs):
TaxChargeFactory.create_bulk_tax_charges(date=initial_end_date)

six_months_ago = utils.get_last_days_of_month_tuples()[5][0]
tax_charges = TaxCharge.objects.filter(date__gte=six_months_ago,date__lte=initial_end_date)
tax_charges = TaxCharge.objects.filter(date__gte=six_months_ago, date__lte=initial_end_date)

context = {
'tax_charge_table': self.get_tax_table_html(tax_charges),
'tax_charge_table': self.get_tax_table_html(tax_charges=tax_charges, end_date=initial_end_date),
'form': self.get_tax_form_html(last_day_of_month=utils.get_last_day_of_last_month()),
'filter_form': self.get_tax_filter_form_html()
}
Expand Down
2 changes: 1 addition & 1 deletion api/views/transaction_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_filter_form_html_and_objects(
transaction_types=transaction_type,
date_from=date_from,
date_to=date_to
)
).select_related('account')

context = {
'filter_form': form,
Expand Down
6 changes: 6 additions & 0 deletions ledger/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@
'django.contrib.humanize',
'api',
'import_export',
'debug_toolbar'
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]

INTERNAL_IPS = [
'127.0.0.1',
]

ROOT_URLCONF = 'ledger.urls'

TEMPLATES = [
Expand Down
2 changes: 1 addition & 1 deletion ledger/templates/api/tables/tax-table.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{% for tax_charge in tax_charges %}
<tr class="clickable-row" hx-get="{% url 'tax-form-bound' pk=tax_charge.pk %}" hx-target="#form-container">
<td>{{ tax_charge.type }}</td>
<td>{{ tax_charge.transaction }}</td>
<td>{{ tax_charge.transaction_string }}</td>
<td>{{ tax_charge.date }}</td>
<td>${{ tax_charge.amount|floatformat:2|intcomma }}</td>
<td>${{ tax_charge.taxable_income|floatformat:2|intcomma }}</td>
Expand Down
10 changes: 9 additions & 1 deletion ledger/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import debug_toolbar
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.contrib.auth.views import LoginView
from django.conf import settings
from api.views.frontend_views import TrendView, UploadTransactionsView, IndexView
from api.views.transaction_views import LinkTransactionsContentView, TransactionContentView, TransactionFormView, JournalEntryView, LinkTransactionsView, JournalEntryFormView, JournalEntryTableView, TransactionsView
from api.views.tax_views import TaxChargeTableView,TaxChargeFormView, TaxesView
Expand Down Expand Up @@ -50,3 +52,9 @@
path('amortization/amortization-form/<int:transaction_id>/', AmortizationFormView.as_view(), name='amortization-form'),
path('amortization/amortize-form/<int:amortization_id>/', AmortizeFormView.as_view(), name='amortize-form'),
]

if settings.DEBUG:

urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ dill==0.3.6
dj-database-url==1.2.0
Django==4.1.6
django-crispy-forms==2.0
django-debug-toolbar==4.2.0
django-import-export==3.3.5
djangorestframework==3.14.0
drf-extra-fields==3.4.1
et-xmlfile==1.1.0
factory-boy==3.3.0
Faker==22.2.0
flake8==7.0.0
gunicorn==20.1.0
isort==5.12.0
lazy-object-proxy==1.9.0
Expand All @@ -24,6 +26,8 @@ openpyxl==3.1.2
Pillow==9.4.0
platformdirs==3.0.0
psycopg2-binary==2.9.5
pycodestyle==2.11.1
pyflakes==3.2.0
pylint==2.16.1
pylint-django==2.5.3
pylint-plugin-utils==0.7
Expand Down

0 comments on commit 7e5e6d8

Please sign in to comment.