mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-20 07:31:44 +01:00
Transfers are still two transactions, now linked to each other:
- shown as one item in lists, with each side's amount, account,
category and tags ("Combine transfers" user setting, on by default)
- date, reference date, paid and mute stay in sync between sides
- edit opens the transfer form; delete, restore, duplicate and bulk
actions act on both sides
- transfers made by the transfer form now trigger rules for each side
Recurring and quick transactions gain a Transfer type, with a "To
account" and optional "To amount", generating linked transfers. A
transfer can be converted into either.
Existing transfers are left as they are, since there is no reliable
way to pair them.
173 lines
4.9 KiB
Python
173 lines
4.9 KiB
Python
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.db import transaction as db_transaction
|
|
from django.forms import model_to_dict
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import render, get_object_or_404
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
from apps.common.decorators.htmx import only_htmx
|
|
from apps.transactions.forms import QuickTransactionForm
|
|
from apps.transactions.models import QuickTransaction, transaction_created
|
|
from apps.transactions.models import Transaction
|
|
|
|
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transactions_index(request):
|
|
return render(
|
|
request,
|
|
"quick_transactions/pages/index.html",
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transactions_list(request):
|
|
quick_transactions = QuickTransaction.objects.all().order_by("name")
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/list.html",
|
|
context={"quick_transactions": quick_transactions},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def quick_transaction_add(request):
|
|
if request.method == "POST":
|
|
form = QuickTransactionForm(request.POST)
|
|
if form.is_valid():
|
|
form.save()
|
|
messages.success(request, _("Item added successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
else:
|
|
form = QuickTransactionForm()
|
|
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/add.html",
|
|
{"form": form},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def quick_transaction_edit(request, quick_transaction_id):
|
|
quick_transaction = get_object_or_404(QuickTransaction, id=quick_transaction_id)
|
|
|
|
if request.method == "POST":
|
|
form = QuickTransactionForm(request.POST, instance=quick_transaction)
|
|
if form.is_valid():
|
|
form.save()
|
|
messages.success(request, _("Item updated successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
else:
|
|
form = QuickTransactionForm(instance=quick_transaction)
|
|
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/edit.html",
|
|
{"form": form, "quick_transaction": quick_transaction},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["DELETE"])
|
|
def quick_transaction_delete(request, quick_transaction_id):
|
|
quick_transaction = get_object_or_404(QuickTransaction, id=quick_transaction_id)
|
|
|
|
quick_transaction.delete()
|
|
|
|
messages.success(request, _("Item deleted successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transactions_create_menu(request):
|
|
quick_transactions = QuickTransaction.objects.all().order_by("name")
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/create_menu.html",
|
|
context={"quick_transactions": quick_transactions},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transaction_add_as_transaction(request, quick_transaction_id):
|
|
quick_transaction: QuickTransaction = get_object_or_404(
|
|
QuickTransaction, id=quick_transaction_id
|
|
)
|
|
today = timezone.localdate(timezone.now())
|
|
|
|
quick_transaction_data = model_to_dict(
|
|
quick_transaction,
|
|
exclude=[
|
|
"id",
|
|
"name",
|
|
"owner",
|
|
"account",
|
|
"category",
|
|
"tags",
|
|
"entities",
|
|
"internal_id",
|
|
"to_account",
|
|
"to_amount",
|
|
],
|
|
)
|
|
|
|
new_transaction = Transaction(**quick_transaction_data)
|
|
new_transaction.type = quick_transaction.transaction_type
|
|
new_transaction.account = quick_transaction.account
|
|
new_transaction.category = quick_transaction.category
|
|
|
|
new_transaction.date = today
|
|
new_transaction.reference_date = today.replace(day=1)
|
|
with db_transaction.atomic():
|
|
new_transaction.save()
|
|
new_transaction.tags.set(quick_transaction.tags.all())
|
|
new_transaction.entities.set(quick_transaction.entities.all())
|
|
to_transaction = quick_transaction.complete_transfer(new_transaction)
|
|
|
|
transaction_created.send(sender=new_transaction)
|
|
if to_transaction:
|
|
transaction_created.send(sender=to_transaction)
|
|
|
|
messages.success(request, _("Transaction added successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
|