mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-03-20 16:44:00 +01:00
feat: add categories and allow to finish a recurring transaction
This commit is contained in:
@@ -615,7 +615,7 @@ class RecurringTransactionForm(forms.ModelForm):
|
||||
end_date = cleaned_data.get("end_date")
|
||||
|
||||
if start_date and end_date and start_date > end_date:
|
||||
raise forms.ValidationError("End date should be after the start date.")
|
||||
raise forms.ValidationError(_("End date should be after the start date"))
|
||||
|
||||
return cleaned_data
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.2 on 2024-11-05 02:26
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('transactions', '0021_alter_transaction_account'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='recurringtransaction',
|
||||
old_name='paused',
|
||||
new_name='is_paused',
|
||||
),
|
||||
]
|
||||
@@ -328,7 +328,7 @@ class RecurringTransaction(models.Model):
|
||||
MONTH = "month", _("month(s)")
|
||||
YEAR = "year", _("year(s)")
|
||||
|
||||
paused = models.BooleanField(default=False, verbose_name=_("Paused"))
|
||||
is_paused = models.BooleanField(default=False, verbose_name=_("Paused"))
|
||||
account = models.ForeignKey(
|
||||
"accounts.Account", on_delete=models.CASCADE, verbose_name=_("Account")
|
||||
)
|
||||
@@ -444,8 +444,8 @@ class RecurringTransaction(models.Model):
|
||||
def generate_upcoming_transactions(cls):
|
||||
today = timezone.now().date()
|
||||
recurring_transactions = cls.objects.filter(
|
||||
models.Q(models.Q(end_date__isnull=True) | models.Q(end_date__gte=today))
|
||||
& models.Q(paused=False)
|
||||
Q(models.Q(end_date__isnull=True) | Q(end_date__gte=today))
|
||||
& Q(paused=False)
|
||||
)
|
||||
|
||||
for recurring_transaction in recurring_transactions:
|
||||
|
||||
@@ -127,6 +127,21 @@ urlpatterns = [
|
||||
views.recurring_transactions_list,
|
||||
name="recurring_transaction_list",
|
||||
),
|
||||
path(
|
||||
"recurring-trasanctions/list/active/",
|
||||
views.active_recurring_transactions_list,
|
||||
name="active_recurring_transaction_list",
|
||||
),
|
||||
path(
|
||||
"recurring-trasanctions/list/paused/",
|
||||
views.paused_recurring_transactions_list,
|
||||
name="paused_recurring_transaction_list",
|
||||
),
|
||||
path(
|
||||
"recurring-trasanctions/list/finished/",
|
||||
views.finished_recurring_transactions_list,
|
||||
name="finished_recurring_transaction_list",
|
||||
),
|
||||
path(
|
||||
"recurring-transactions/add/",
|
||||
views.recurring_transaction_add,
|
||||
@@ -152,4 +167,9 @@ urlpatterns = [
|
||||
views.recurring_transaction_toggle_pause,
|
||||
name="recurring_transaction_toggle_pause",
|
||||
),
|
||||
path(
|
||||
"recurring-transactions/<int:recurring_transaction_id>/finish/",
|
||||
views.recurring_transaction_finish,
|
||||
name="recurring_transaction_finish",
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
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.csrf import csrf_exempt
|
||||
from django.views.decorators.http import require_http_methods
|
||||
@@ -25,14 +29,60 @@ def recurring_transactions_index(request):
|
||||
@login_required
|
||||
@require_http_methods(["GET"])
|
||||
def recurring_transactions_list(request):
|
||||
recurring_transactions = RecurringTransaction.objects.all().order_by(
|
||||
"-start_date", "description", "id"
|
||||
)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"recurring_transactions/fragments/list.html",
|
||||
{"recurring_transactions": recurring_transactions},
|
||||
)
|
||||
|
||||
|
||||
@only_htmx
|
||||
@login_required
|
||||
@require_http_methods(["GET"])
|
||||
def active_recurring_transactions_list(request):
|
||||
today = timezone.localdate(timezone.now())
|
||||
recurring_transactions = RecurringTransaction.objects.filter(
|
||||
Q(end_date__gte=today) | Q(end_date__isnull=True),
|
||||
is_paused=False,
|
||||
).order_by("-start_date", "description", "id")
|
||||
|
||||
print(recurring_transactions)
|
||||
return render(
|
||||
request,
|
||||
"recurring_transactions/fragments/table.html",
|
||||
{"recurring_transactions": recurring_transactions, "status": "active"},
|
||||
)
|
||||
|
||||
|
||||
@only_htmx
|
||||
@login_required
|
||||
@require_http_methods(["GET"])
|
||||
def paused_recurring_transactions_list(request):
|
||||
today = timezone.localdate(timezone.now())
|
||||
recurring_transactions = RecurringTransaction.objects.filter(
|
||||
Q(end_date__gte=today) | Q(end_date__isnull=True),
|
||||
is_paused=True,
|
||||
).order_by("-start_date", "description", "id")
|
||||
|
||||
return render(
|
||||
request,
|
||||
"recurring_transactions/fragments/table.html",
|
||||
{"recurring_transactions": recurring_transactions, "status": "paused"},
|
||||
)
|
||||
|
||||
|
||||
@only_htmx
|
||||
@login_required
|
||||
@require_http_methods(["GET"])
|
||||
def finished_recurring_transactions_list(request):
|
||||
today = timezone.localdate(timezone.now())
|
||||
recurring_transactions = RecurringTransaction.objects.filter(
|
||||
Q(end_date__lte=today),
|
||||
).order_by("-start_date", "description", "id")
|
||||
|
||||
return render(
|
||||
request,
|
||||
"recurring_transactions/fragments/table.html",
|
||||
{"recurring_transactions": recurring_transactions, "status": "finished"},
|
||||
)
|
||||
|
||||
|
||||
@@ -110,33 +160,16 @@ def recurring_transaction_edit(request, recurring_transaction_id):
|
||||
)
|
||||
|
||||
|
||||
# @only_htmx
|
||||
# @login_required
|
||||
# @require_http_methods(["GET"])
|
||||
# def recurring_transaction_refresh(request, installment_plan_id):
|
||||
# installment_plan = get_object_or_404(InstallmentPlan, id=installment_plan_id)
|
||||
# installment_plan.update_transactions()
|
||||
#
|
||||
# messages.success(request, _("Installment Plan refreshed successfully"))
|
||||
#
|
||||
# return HttpResponse(
|
||||
# status=204,
|
||||
# headers={
|
||||
# "HX-Trigger": "updated, hide_offcanvas, toasts",
|
||||
# },
|
||||
# )
|
||||
|
||||
|
||||
@only_htmx
|
||||
@login_required
|
||||
@require_http_methods(["GET"])
|
||||
def recurring_transaction_toggle_pause(request, recurring_transaction_id):
|
||||
installment_plan = get_object_or_404(
|
||||
recurring_transaction = get_object_or_404(
|
||||
RecurringTransaction, id=recurring_transaction_id
|
||||
)
|
||||
current_paused = installment_plan.paused
|
||||
installment_plan.paused = not current_paused
|
||||
installment_plan.save(update_fields=["paused"])
|
||||
current_paused = recurring_transaction.is_paused
|
||||
recurring_transaction.is_paused = not current_paused
|
||||
recurring_transaction.save(update_fields=["is_paused"])
|
||||
|
||||
if current_paused:
|
||||
messages.success(request, _("Recurring transaction unpaused successfully"))
|
||||
@@ -152,6 +185,29 @@ def recurring_transaction_toggle_pause(request, recurring_transaction_id):
|
||||
)
|
||||
|
||||
|
||||
@only_htmx
|
||||
@login_required
|
||||
@require_http_methods(["GET"])
|
||||
def recurring_transaction_finish(request, recurring_transaction_id):
|
||||
recurring_transaction = get_object_or_404(
|
||||
RecurringTransaction, id=recurring_transaction_id
|
||||
)
|
||||
today = timezone.localdate(timezone.now()) - relativedelta(days=1)
|
||||
|
||||
recurring_transaction.end_date = today
|
||||
recurring_transaction.is_paused = True
|
||||
recurring_transaction.save(update_fields=["end_date", "is_paused"])
|
||||
|
||||
messages.success(request, _("Recurring transaction finished successfully"))
|
||||
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={
|
||||
"HX-Trigger": "updated, hide_offcanvas, toasts",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@only_htmx
|
||||
@login_required
|
||||
@csrf_exempt
|
||||
|
||||
@@ -14,82 +14,22 @@
|
||||
{% endspaceless %}
|
||||
</div>
|
||||
|
||||
<div class="border p-3 rounded-3 table-responsive">
|
||||
{% if recurring_transactions %}
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" class="col-auto"></th>
|
||||
<th scope="col" class="col">{% translate 'Name' %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for recurring_transaction in recurring_transactions %}
|
||||
<tr class="recurring_transaction">
|
||||
<td class="col-auto text-center">
|
||||
<div class="btn-group" role="group" aria-label="{% translate 'Actions' %}">
|
||||
<a class="btn btn-secondary btn-sm"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Edit" %}"
|
||||
hx-get="{% url 'recurring_transaction_edit' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#generic-offcanvas">
|
||||
<i class="fa-solid fa-pencil fa-fw"></i></a>
|
||||
<a class="btn btn-secondary btn-sm "
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Transactions" %}"
|
||||
hx-get="{% url 'recurring_transaction_transactions' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#persistent-generic-offcanvas-left">
|
||||
<i class="fa-solid fa-eye fa-fw"></i></a>
|
||||
{% if recurring_transaction.paused %}
|
||||
<a class="btn btn-secondary btn-sm text-info"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Unpause" %}"
|
||||
hx-get="{% url 'recurring_transaction_toggle_pause' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#generic-offcanvas"
|
||||
hx-trigger='confirmed'
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will start creating new transactions until you pause it" %}"
|
||||
data-confirm-text="{% translate "Yes, unpause it!" %}"
|
||||
_="install prompt_swal"><i class="fa-solid fa-play fa-fw"></i></a>
|
||||
{% else %}
|
||||
<a class="btn btn-secondary btn-sm text-info"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Pause" %}"
|
||||
hx-get="{% url 'recurring_transaction_toggle_pause' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#generic-offcanvas"
|
||||
hx-trigger='confirmed'
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will stop the creation of new transactions until you unpause it" %}"
|
||||
data-confirm-text="{% translate "Yes, pause it!" %}"
|
||||
_="install prompt_swal">
|
||||
<i class="fa-solid fa-pause fa-fw"></i></a>
|
||||
{% endif %}
|
||||
<a class="btn btn-secondary btn-sm text-danger"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Delete" %}"
|
||||
hx-delete="{% url 'recurring_transaction_delete' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-trigger='confirmed'
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will delete the recurrence and all transactions associated with it" %}"
|
||||
data-confirm-text="{% translate "Yes, delete it!" %}"
|
||||
_="install prompt_swal"><i class="fa-solid fa-trash fa-fw"></i></a>
|
||||
</td>
|
||||
<td class="col">{{ recurring_transaction.description }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<c-msg.empty title="{% translate "No recurring transactions" %}" :remove-padding="True"></c-msg.empty>
|
||||
{% endif %}
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<ul class="nav nav-pills card-header-pills" id="myTab" role="tablist">
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link active" data-bs-toggle="tab" type="button" role="tab" aria-selected="true" hx-get="{% url 'active_recurring_transaction_list' %}" hx-trigger="load, click" hx-target="#recurring-transactions-table">{% translate 'Active' %}</button>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link" hx-get="{% url 'paused_recurring_transaction_list' %}" hx-target="#recurring-transactions-table" data-bs-toggle="tab" type="button" role="tab" aria-selected="false">{% translate 'Paused' %}</button>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link" hx-get="{% url 'finished_recurring_transaction_list' %}" hx-target="#recurring-transactions-table" data-bs-toggle="tab" type="button" role="tab" aria-selected="false">{% translate 'Finished' %}</button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="recurring-transactions-table"></div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
109
app/templates/recurring_transactions/fragments/table.html
Normal file
109
app/templates/recurring_transactions/fragments/table.html
Normal file
@@ -0,0 +1,109 @@
|
||||
{% load currency_display %}
|
||||
{% load i18n %}
|
||||
{% if status == "active" %}
|
||||
<div class="card-body show-loading" hx-get="{% url 'active_recurring_transaction_list' %}" hx-trigger="updated from:window" hx-swap="outerHTML">
|
||||
{% elif status == 'paused' %}
|
||||
<div class="card-body show-loading" hx-get="{% url 'paused_recurring_transaction_list' %}" hx-trigger="updated from:window" hx-swap="outerHTML">
|
||||
{% elif status == 'finished' %}
|
||||
<div class="card-body show-loading" hx-get="{% url 'finished_recurring_transaction_list' %}" hx-trigger="updated from:window" hx-swap="outerHTML">
|
||||
{% endif %}
|
||||
{% if recurring_transactions %}
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" class="col-auto"></th>
|
||||
<th scope="col" class="col">{% translate 'Name' %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for recurring_transaction in recurring_transactions %}
|
||||
<tr class="recurring_transaction">
|
||||
<td class="col-auto text-center">
|
||||
<div class="btn-group" role="group" aria-label="{% translate 'Actions' %}">
|
||||
<a class="btn btn-secondary btn-sm"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Edit" %}"
|
||||
hx-get="{% url 'recurring_transaction_edit' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-swap="innerHTML"
|
||||
hx-target="#generic-offcanvas">
|
||||
<i class="fa-solid fa-pencil fa-fw"></i></a>
|
||||
<a class="btn btn-secondary btn-sm "
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Transactions" %}"
|
||||
hx-get="{% url 'recurring_transaction_transactions' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-swap="innerHTML"
|
||||
hx-target="#persistent-generic-offcanvas-left">
|
||||
<i class="fa-solid fa-eye fa-fw"></i></a>
|
||||
{% if status != 'finished' %}
|
||||
{% if recurring_transaction.is_paused %}
|
||||
<a class="btn btn-secondary btn-sm text-info"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Unpause" %}"
|
||||
hx-get="{% url 'recurring_transaction_toggle_pause' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#generic-offcanvas"
|
||||
hx-swap="innerHTML"
|
||||
hx-trigger='confirmed'
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will start creating new transactions until you pause it" %}"
|
||||
data-confirm-text="{% translate "Yes, unpause it!" %}"
|
||||
_="install prompt_swal"><i class="fa-solid fa-play fa-fw"></i></a>
|
||||
{% else %}
|
||||
<a class="btn btn-secondary btn-sm text-info"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Pause" %}"
|
||||
hx-get="{% url 'recurring_transaction_toggle_pause' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#generic-offcanvas"
|
||||
hx-trigger='confirmed'
|
||||
hx-swap="innerHTML"
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will stop the creation of new transactions until you unpause it" %}"
|
||||
data-confirm-text="{% translate "Yes, pause it!" %}"
|
||||
_="install prompt_swal">
|
||||
<i class="fa-solid fa-pause fa-fw"></i></a>
|
||||
{% endif %}
|
||||
<a class="btn btn-secondary btn-sm text-info"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Finish" %}"
|
||||
hx-get="{% url 'recurring_transaction_finish' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-target="#generic-offcanvas"
|
||||
hx-trigger='confirmed'
|
||||
hx-swap="innerHTML"
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will stop the creation of new transactions" %}"
|
||||
data-confirm-text="{% translate "Yes, finish it!" %}"
|
||||
_="install prompt_swal">
|
||||
<i class="fa-solid fa-flag-checkered fa-fw"></i></a>
|
||||
{% endif %}
|
||||
<a class="btn btn-secondary btn-sm text-danger"
|
||||
role="button"
|
||||
data-bs-toggle="tooltip"
|
||||
data-bs-title="{% translate "Delete" %}"
|
||||
hx-delete="{% url 'recurring_transaction_delete' recurring_transaction_id=recurring_transaction.id %}"
|
||||
hx-trigger='confirmed'
|
||||
hx-swap="innerHTML"
|
||||
data-bypass-on-ctrl="true"
|
||||
data-title="{% translate "Are you sure?" %}"
|
||||
data-text="{% translate "This will delete the recurrence and all transactions associated with it" %}"
|
||||
data-confirm-text="{% translate "Yes, delete it!" %}"
|
||||
_="install prompt_swal"><i class="fa-solid fa-trash fa-fw"></i></a>
|
||||
</div>
|
||||
</td>
|
||||
<td class="col">{{ recurring_transaction.description }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<c-msg.empty title="{% translate "No recurring transactions" %}" :remove-padding="True"></c-msg.empty>
|
||||
{% endif %}
|
||||
</div>
|
||||
@@ -4,5 +4,5 @@
|
||||
{% block title %}{% translate 'Recurring Transactions' %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div hx-get="{% url 'recurring_transaction_list' %}" hx-trigger="load, updated from:window" class="show-loading"></div>
|
||||
<div hx-get="{% url 'recurring_transaction_list' %}" hx-trigger="load" class="show-loading"></div>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user