Skip to content

Commit

Permalink
export as JSON implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinna1 authored and iokiwi committed Oct 12, 2023
1 parent 238f66e commit 49c7e28
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ <h1>Recurring Expenses</h1>
Export
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="{% url 'recurring_expenses:export' %}?format=csv">CSV</a></li>
{% for format in export_formats %}
<li>
<a class="dropdown-item" href="{% url 'recurring_expenses:export' format%}">{{format.upper}}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/recurring_expenses/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
path("", views.IndexView.as_view(), name="index"),
path("new/", views.create_recurring_expense, name="new"),
path("import/", views.import_recurring_expenses, name="import"),
path("export/", views.export_recurring_expenses, name="export"),
path("export/<str:export_format>/", views.export_recurring_expenses, name="export"),
path("<uuid:expense_id>/edit", views.edit_recurring_expense, name="edit"),
path("<uuid:expense_id>/delete", views.delete_recurring_expense, name="delete"),
]
116 changes: 77 additions & 39 deletions app/recurring_expenses/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import csv
import json
import requests

from django.contrib.auth.decorators import login_required
Expand Down Expand Up @@ -123,53 +124,89 @@ def create_recurring_expense(request):
return render(request, "recurring_expenses/new.html", {"form": form})


@login_required
def export_recurring_expenses(request):
export_format = request.GET.get("format", "csv")
if "csv" in export_format.lower():
filename = "recurring_expenses.csv"
response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="' + filename + '"'},
)
writer = csv.writer(response)
_EXPORT_FORMATS = [
"csv",
"json",
]


def export_to_csv(expenses):
filename = "recurring_expenses.csv"
fx_rates = get_fxRate_nzd()
response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="' + filename + '"'},
)
writer = csv.writer(response)
writer.writerow(
[
"Active",
"Particulars",
"Amount",
"Currency",
"Amount NZD",
"Period (Months)",
]
)
for expense in expenses:
amount_nzd = (1 / fx_rates[expense.currency]) * float(expense.amount)
expense.amount_nzd = amount_nzd
writer.writerow(
[
"Active",
"Particulars",
"Amount",
"Currency",
"Amount NZD",
"Period (Months)",
expense.active,
expense.particulars,
format(expense.amount, ".2f"),
expense.currency,
format(expense.amount_nzd, ".2f"),
expense.period,
]
)
#
# Get expenses and amount in NZD
# Copied from class IndexView, maybe this should be a function?
# Exporting each expense to csv within this though so we don't
# have to run the for loop twice
#
fx_rates = get_fxRate_nzd()
expenses = RecurringExpense.objects.all()
for expense in expenses:
amount_nzd = (1 / fx_rates[expense.currency]) * float(expense.amount)
expense.amount_nzd = amount_nzd
writer.writerow(
[
expense.active,
expense.particulars,
format(expense.amount, ".2f"),
expense.currency,
format(expense.amount_nzd, ".2f"),
expense.period,
]
)
else:
response = HttpResponse(status=204)
return response


def export_to_json(expenses):
data = []
fx_rates = get_fxRate_nzd()
for expense in expenses:
amount_nzd = (1 / fx_rates[expense.currency]) * float(expense.amount)
expense.amount_nzd = amount_nzd
data.append({
"Active": expense.active,
"Particulars": expense.particulars,
"Amount": format(expense.amount, ".2f"),
"Currency": expense.currency,
"Amount NZD": format(expense.amount_nzd, ".2f"),
"Period (Months)": expense.period,
})

json_data = json.dumps(data, indent=4)

response = HttpResponse(
content_type="application/json",
headers={
"Content-Disposition": 'attachment; filename="recurring_expenses.json"'
},
)
response.write(json_data)
return response


@login_required
def export_recurring_expenses(request, export_format):
# Assuring that the url parameter is not case-sensitive.
export_format = export_format.lower()

if export_format not in _EXPORT_FORMATS:
return HttpResponse(status=204)

expenses = RecurringExpense.objects.all()
if export_format == "csv":
return export_to_csv(expenses)

elif export_format == "json":
return export_to_json(expenses)


class IndexView(LoginRequiredMixin, generic.TemplateView):
template_name = "recurring_expenses/index.html"

Expand All @@ -181,6 +218,7 @@ def get_context_data(self, **kwargs):
amount_nzd = (1 / fx_rates[expense.currency]) * float(expense.amount)
expense.amount_nzd = amount_nzd
context["recurring_expenses"] = expenses
context["export_formats"] = _EXPORT_FORMATS
return context


Expand Down

0 comments on commit 49c7e28

Please sign in to comment.