From 96952dec2bf34e2a8e573b9de98fdeed14a0da43 Mon Sep 17 00:00:00 2001 From: Tianrui-Luo <77422312+Tianrui-Luo@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:15:15 -0500 Subject: [PATCH 01/12] Create edit.py and add the base edit invoice function --- backend/api/invoices/edit.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 backend/api/invoices/edit.py diff --git a/backend/api/invoices/edit.py b/backend/api/invoices/edit.py new file mode 100644 index 00000000..fa9d9b10 --- /dev/null +++ b/backend/api/invoices/edit.py @@ -0,0 +1,30 @@ +from django.contrib import messages +from django.http import HttpRequest, JsonResponse, QueryDict +from django.shortcuts import render +from django.views.decorators.http import require_http_methods + +from backend.models import Invoice + + +@require_http_methods(["EDIT"]) +def edit_invoice(request: HttpRequest): + edit_items = QueryDict(request.body) + print("edit items", edit_items) + + invoice = edit_items.get("invoice") + + try: + invoice = Invoice.objects.get(id=invoice) + except: + return JsonResponse({"message": "Invoice not found"}, status=404) + + if not invoice or invoice.user != request.user: + return JsonResponse({"message": "Invoice not found"}, status=404) + + invoice.edit() + + if request.htmx: + messages.success(request, "Invoice edited") + return render(request, "partials/base/toasts.html") + + return JsonResponse({"message": "Invoice successfully edited"}, status=200) From ee17863a57875c5da781c3a592f84127c374c6ca Mon Sep 17 00:00:00 2001 From: Nuovaxu Date: Mon, 11 Dec 2023 22:53:39 -0500 Subject: [PATCH 02/12] feat: add require_method of EDIT, allow users to update contributes --- backend/api/invoices/edit.py | 41 +++++++++++++++++++++++++++--------- backend/api/invoices/urls.py | 7 +++++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/backend/api/invoices/edit.py b/backend/api/invoices/edit.py index fa9d9b10..70a06fe1 100644 --- a/backend/api/invoices/edit.py +++ b/backend/api/invoices/edit.py @@ -4,24 +4,45 @@ from django.views.decorators.http import require_http_methods from backend.models import Invoice +from datetime import datetime -@require_http_methods(["EDIT"]) +@require_http_methods(["POST"]) def edit_invoice(request: HttpRequest): - edit_items = QueryDict(request.body) - print("edit items", edit_items) - - invoice = edit_items.get("invoice") try: - invoice = Invoice.objects.get(id=invoice) + invoice = Invoice.objects.get(id=request.POST.get('invoice_id')) except: return JsonResponse({"message": "Invoice not found"}, status=404) - if not invoice or invoice.user != request.user: - return JsonResponse({"message": "Invoice not found"}, status=404) - - invoice.edit() + attributes_to_updates = { + "date_due": datetime.strptime(request.POST.get('date_due'), '%Y-%m-%d').date(), + "date_issued": request.POST.get('date_issued'), + "client_name": request.POST.get('to_name'), + "client_company": request.POST.get('to_company'), + "client_address": request.POST.get('to_address'), + "client_city": request.POST.get('to_city'), + "client_county": request.POST.get('to_county'), + "client_country": request.POST.get('to_country'), + "self_name": request.POST.get('from_name'), + "self_company": request.POST.get('from_company'), + "self_address": request.POST.get('from_address'), + "self_city": request.POST.get('from_city'), + "self_county": request.POST.get('from_county'), + "self_country": request.POST.get('from_country'), + "notes": request.POST.get('notes'), + "invoice_number": request.POST.get('invoice_number'), + "vat_number": request.POST.get('vat_number'), + "reference": request.POST.get('reference'), + "sort_code": request.POST.get('sort_code'), + "account_number": request.POST.get('account_number'), + "account_holder_name": request.POST.get('account_holder_name') + } + + for column_name, new_value in attributes_to_updates.items(): + setattr(invoice, column_name, new_value) + + invoice.save() if request.htmx: messages.success(request, "Invoice edited") diff --git a/backend/api/invoices/urls.py b/backend/api/invoices/urls.py index c9193130..c7b9035e 100644 --- a/backend/api/invoices/urls.py +++ b/backend/api/invoices/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from . import fetch, delete +from . import fetch, delete, edit from .create import set_destination from .create.services import add, remove @@ -30,6 +30,11 @@ delete.delete_invoice, name="delete", ), + path( + "edit/", + edit.edit_invoice, + name="edit", + ), path("fetch/", fetch.fetch_all_invoices, name="fetch"), ] From f7012e1d147192ea89f0f6aaa6a3a233b7619e3a Mon Sep 17 00:00:00 2001 From: Tianrui-Luo <77422312+Tianrui-Luo@users.noreply.github.com> Date: Mon, 11 Dec 2023 23:49:02 -0500 Subject: [PATCH 03/12] Update urls.py to correctly navigate to the invoice edit page --- backend/urls.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/urls.py b/backend/urls.py index 334869d8..557ff14f 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -3,6 +3,7 @@ from django.contrib import admin from django.urls import re_path as url, path, include from django.views.static import serve +from .api.invoices import edit from backend.views.core import ( other, @@ -115,9 +116,14 @@ invoices.create.create_invoice_page, name="invoices dashboard create", ), + #path( + # "dashboard/invoices/", + # invoices.dashboard.invoices_dashboard_id, + # name="invoices dashboard edit", + #), path( - "dashboard/invoices/", - invoices.dashboard.invoices_dashboard_id, + "dashboard/invoices/edit/", + edit.edit_invoice, name="invoices dashboard edit", ), # path('dashboard/invoices//edit', invoices.dashboard.invoices_dash~board_id, name='invoices dashboard'), From 3ed3a898071d05d112bdbbc3a1c60732f42817e4 Mon Sep 17 00:00:00 2001 From: Tianrui-Luo <77422312+Tianrui-Luo@users.noreply.github.com> Date: Mon, 11 Dec 2023 23:51:22 -0500 Subject: [PATCH 04/12] Update _fetch_body.html to activate the edit function from frontend webpage and redirect to edit html --- .../pages/invoices/dashboard/_fetch_body.html | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/frontend/templates/pages/invoices/dashboard/_fetch_body.html b/frontend/templates/pages/invoices/dashboard/_fetch_body.html index 44717ddb..1d42ce73 100644 --- a/frontend/templates/pages/invoices/dashboard/_fetch_body.html +++ b/frontend/templates/pages/invoices/dashboard/_fetch_body.html @@ -58,24 +58,23 @@ Manage Access -
-
  • - - - Edit - -
  • -
  • -
  • +
  • + -
  • -
    + > + + Delete + + From bb9e564b39eb2318b4b8743cda6de1e44eb6f381 Mon Sep 17 00:00:00 2001 From: HarryHuCodes <48864969+HarryHuCodes@users.noreply.github.com> Date: Tue, 12 Dec 2023 03:42:37 -0500 Subject: [PATCH 05/12] Added temporary html pages for editing invoice --- .../templates/pages/invoices/edit/edit.html | 67 +++++++++++++++++++ .../invoices/edit/edit_from_destination.html | 23 +++++++ .../invoices/edit/edit_to_destination.html | 24 +++++++ 3 files changed, 114 insertions(+) create mode 100644 frontend/templates/pages/invoices/edit/edit.html create mode 100644 frontend/templates/pages/invoices/edit/edit_from_destination.html create mode 100644 frontend/templates/pages/invoices/edit/edit_to_destination.html diff --git a/frontend/templates/pages/invoices/edit/edit.html b/frontend/templates/pages/invoices/edit/edit.html new file mode 100644 index 00000000..ef6e3399 --- /dev/null +++ b/frontend/templates/pages/invoices/edit/edit.html @@ -0,0 +1,67 @@ +{% extends 'base/base.html' %} +{% load static %} +{% load markdownify %} +{% block content %} + {% include 'components/modal.html' with modals=modal_data %} +
    + {% csrf_token %} +
    STEP 1 - DESTINATIONS
    +
    +
    +

    From

    + +
    +
    + {% include 'pages/invoices/create/edit_from_destination.html' %} + {% include 'pages/invoices/create/edit_to_destination.html' %} +
    +
    +
    STEP 2 - DATES
    +
    +
    +
    +
    +
    + + + +
    +
    +
    +
    +
    +
    + + + +
    +
    +
    +
    +
    + + + +
    + +
    + + +
    + + +{% endblock content %} \ No newline at end of file diff --git a/frontend/templates/pages/invoices/edit/edit_from_destination.html b/frontend/templates/pages/invoices/edit/edit_from_destination.html new file mode 100644 index 00000000..edb35bba --- /dev/null +++ b/frontend/templates/pages/invoices/edit/edit_from_destination.html @@ -0,0 +1,23 @@ +{% if not swapping %} + +{% endif %} \ No newline at end of file diff --git a/frontend/templates/pages/invoices/edit/edit_to_destination.html b/frontend/templates/pages/invoices/edit/edit_to_destination.html new file mode 100644 index 00000000..92904747 --- /dev/null +++ b/frontend/templates/pages/invoices/edit/edit_to_destination.html @@ -0,0 +1,24 @@ +{% if not swapping %} +

    To

    + +{% endif %} \ No newline at end of file From 0086f06091e2f4d0c8657c0b1e199152745a7b08 Mon Sep 17 00:00:00 2001 From: HarryHuCodes <48864969+HarryHuCodes@users.noreply.github.com> Date: Tue, 12 Dec 2023 04:03:06 -0500 Subject: [PATCH 06/12] Configured backend.views.core.invoices.edit and wrote the view function used in urlpatterns path for redirect to edit.html --- backend/urls.py | 3 +- backend/views/core/invoices/create.py | 7 +++ backend/views/core/invoices/edit.py | 67 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 backend/views/core/invoices/edit.py diff --git a/backend/urls.py b/backend/urls.py index 557ff14f..792a52ad 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -123,7 +123,8 @@ #), path( "dashboard/invoices/edit/", - edit.edit_invoice, + invoices.create.edit_invoice_page, + #invoices.edit.invoice_edit_page_get, name="invoices dashboard edit", ), # path('dashboard/invoices//edit', invoices.dashboard.invoices_dash~board_id, name='invoices dashboard'), diff --git a/backend/views/core/invoices/create.py b/backend/views/core/invoices/create.py index bdc20580..5f7aa62e 100644 --- a/backend/views/core/invoices/create.py +++ b/backend/views/core/invoices/create.py @@ -64,3 +64,10 @@ def create_invoice_page(request: HttpRequest): if request.method == "POST": return invoice_page_post(request) return invoice_page_get(request) + + +@require_http_methods(["GET", "POST"]) +def edit_invoice_page(request: HttpRequest): + if request.method == "POST": + return invoice_page_post(request) + return invoice_page_get(request) \ No newline at end of file diff --git a/backend/views/core/invoices/edit.py b/backend/views/core/invoices/edit.py new file mode 100644 index 00000000..802b94d1 --- /dev/null +++ b/backend/views/core/invoices/edit.py @@ -0,0 +1,67 @@ +from django.contrib import messages +from django.http import HttpRequest, JsonResponse, QueryDict +from django.shortcuts import render +from django.views.decorators.http import require_http_methods + +from backend.models import Invoice +from datetime import datetime + + +def invoice_edit_page_get(request, id): + context = {"type": "edit"} + return render(request, "pages/invoices/edit/edit.html", context) + + +@require_http_methods(["EDIT"]) +def edit_invoice(request: HttpRequest): + + edit_items = QueryDict(request.body) + invoice = edit_items.get("invoice") + + try: + invoice = Invoice.objects.get(id=request.POST.get('invoice_id')) + except: + return JsonResponse({"message": "Invoice not found"}, status=404) + + attributes_to_updates = { + "date_due": datetime.strptime(request.POST.get('date_due'), '%Y-%m-%d').date(), + "date_issued": request.POST.get('date_issued'), + "client_name": request.POST.get('to_name'), + "client_company": request.POST.get('to_company'), + "client_address": request.POST.get('to_address'), + "client_city": request.POST.get('to_city'), + "client_county": request.POST.get('to_county'), + "client_country": request.POST.get('to_country'), + "self_name": request.POST.get('from_name'), + "self_company": request.POST.get('from_company'), + "self_address": request.POST.get('from_address'), + "self_city": request.POST.get('from_city'), + "self_county": request.POST.get('from_county'), + "self_country": request.POST.get('from_country'), + "notes": request.POST.get('notes'), + "invoice_number": request.POST.get('invoice_number'), + "vat_number": request.POST.get('vat_number'), + "reference": request.POST.get('reference'), + "sort_code": request.POST.get('sort_code'), + "account_number": request.POST.get('account_number'), + "account_holder_name": request.POST.get('account_holder_name') + } + + for column_name, new_value in attributes_to_updates.items(): + setattr(invoice, column_name, new_value) + + invoice.save() + + if request.htmx: + messages.success(request, "Invoice edited") + return render(request, "partials/base/toasts.html") + + return JsonResponse({"message": "Invoice successfully edited"}, status=200) + + + +@require_http_methods(["GET", "POST"]) +def edit_invoice_page(request: HttpRequest, id): + if request.method == "POST": + return edit_invoice(request) + return invoice_edit_page_get(request, id) \ No newline at end of file From 10cd4c901eb4d8fbd4e92e289abc0bf4dd8aa755 Mon Sep 17 00:00:00 2001 From: HarryHuCodes <48864969+HarryHuCodes@users.noreply.github.com> Date: Tue, 12 Dec 2023 12:21:58 -0500 Subject: [PATCH 07/12] Fixed argument id error when loading page invoices/edit/ and updated redirect paths in edit.html --- backend/urls.py | 4 ++-- frontend/templates/pages/invoices/edit/edit.html | 4 ++-- .../templates/pages/invoices/edit/edit_from_destination.html | 4 ++++ .../templates/pages/invoices/edit/edit_to_destination.html | 4 ++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/backend/urls.py b/backend/urls.py index 792a52ad..a9f610c2 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -3,7 +3,7 @@ from django.contrib import admin from django.urls import re_path as url, path, include from django.views.static import serve -from .api.invoices import edit +from .views.core.invoices import edit from backend.views.core import ( other, @@ -123,7 +123,7 @@ #), path( "dashboard/invoices/edit/", - invoices.create.edit_invoice_page, + invoices.edit.edit_invoice_page, #invoices.edit.invoice_edit_page_get, name="invoices dashboard edit", ), diff --git a/frontend/templates/pages/invoices/edit/edit.html b/frontend/templates/pages/invoices/edit/edit.html index ef6e3399..44565e6c 100644 --- a/frontend/templates/pages/invoices/edit/edit.html +++ b/frontend/templates/pages/invoices/edit/edit.html @@ -12,8 +12,8 @@

    From

    - {% include 'pages/invoices/create/edit_from_destination.html' %} - {% include 'pages/invoices/create/edit_to_destination.html' %} + {% include 'pages/invoices/edit/edit_from_destination.html' %} + {% include 'pages/invoices/edit/edit_to_destination.html' %}
    STEP 2 - DATES
    diff --git a/frontend/templates/pages/invoices/edit/edit_from_destination.html b/frontend/templates/pages/invoices/edit/edit_from_destination.html index edb35bba..83588273 100644 --- a/frontend/templates/pages/invoices/edit/edit_from_destination.html +++ b/frontend/templates/pages/invoices/edit/edit_from_destination.html @@ -4,6 +4,8 @@ hx-trigger="click once" hx-swap="beforeend" hx-target="#modal_container" hx-get="{% url "api:base:modal retrieve" modal_name="invoices_from_destination" %}"> {% endif %} + +

    {{ name | default:"John Smith" }}

    {{ company | default:"Google" }}

    @@ -12,6 +14,8 @@

    {{ county | default:"Oxfordshire" }}

    {{ country | default:"England" }}

    + + diff --git a/frontend/templates/pages/invoices/edit/edit_to_destination.html b/frontend/templates/pages/invoices/edit/edit_to_destination.html index 92904747..6463941c 100644 --- a/frontend/templates/pages/invoices/edit/edit_to_destination.html +++ b/frontend/templates/pages/invoices/edit/edit_to_destination.html @@ -5,6 +5,8 @@

    To

    hx-trigger="click once" hx-swap="beforeend" hx-target="#modal_container" hx-get="{% url "api:base:modal retrieve" modal_name="invoices_to_destination" %}"> {% endif %} + +

    {{ name | default:"John Smith" }}

    {{ company | default:"Google" }}

    @@ -13,6 +15,8 @@

    To

    {{ county | default:"Oxfordshire" }}

    {{ country | default:"England" }}

    + + From 3cc9a2a74a1a3dc725a37c952121ef08ef7551bb Mon Sep 17 00:00:00 2001 From: Tianrui-Luo <77422312+Tianrui-Luo@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:27:37 -0500 Subject: [PATCH 08/12] Update edit.py by fixing the "invoice_id not found" problem and invoice can be edited now --- backend/views/core/invoices/edit.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/backend/views/core/invoices/edit.py b/backend/views/core/invoices/edit.py index 802b94d1..6ff80358 100644 --- a/backend/views/core/invoices/edit.py +++ b/backend/views/core/invoices/edit.py @@ -12,14 +12,11 @@ def invoice_edit_page_get(request, id): return render(request, "pages/invoices/edit/edit.html", context) -@require_http_methods(["EDIT"]) -def edit_invoice(request: HttpRequest): - - edit_items = QueryDict(request.body) - invoice = edit_items.get("invoice") +@require_http_methods(["POST"]) +def edit_invoice(request: HttpRequest, invoice_id): try: - invoice = Invoice.objects.get(id=request.POST.get('invoice_id')) + invoice = Invoice.objects.get(id=invoice_id) except: return JsonResponse({"message": "Invoice not found"}, status=404) @@ -63,5 +60,5 @@ def edit_invoice(request: HttpRequest): @require_http_methods(["GET", "POST"]) def edit_invoice_page(request: HttpRequest, id): if request.method == "POST": - return edit_invoice(request) - return invoice_edit_page_get(request, id) \ No newline at end of file + return edit_invoice(request,id) + return invoice_edit_page_get(request, id) From cd5d13007b1982474344efcef94bb47ff83c7314 Mon Sep 17 00:00:00 2001 From: Tianrui-Luo <77422312+Tianrui-Luo@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:53:46 -0500 Subject: [PATCH 09/12] Update fetch.py by fixing the problem of unable to switch payment status --- backend/api/invoices/fetch.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/api/invoices/fetch.py b/backend/api/invoices/fetch.py index 15a782ce..076f6b0f 100644 --- a/backend/api/invoices/fetch.py +++ b/backend/api/invoices/fetch.py @@ -103,6 +103,9 @@ def fetch_all_invoices(request: HttpRequest): if (items.date_due and timezone.now().date() > items.date_due) and items.payment_status == "pending": items.payment_status = "overdue" items.save() + if (items.date_due and timezone.now().date() < items.date_due) and items.payment_status == "overdue": + items.payment_status = "pending" + items.save() # Apply OR conditions to the invoices queryset invoices = invoices.filter(or_conditions) From 513c50f760cd7492dd2aadaf33df04b530c2b971 Mon Sep 17 00:00:00 2001 From: Tianrui-Luo <77422312+Tianrui-Luo@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:55:12 -0500 Subject: [PATCH 10/12] Update edit.py by redirecting to invoice page after successfully editing invoices --- backend/views/core/invoices/edit.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/views/core/invoices/edit.py b/backend/views/core/invoices/edit.py index 6ff80358..581f6ffd 100644 --- a/backend/views/core/invoices/edit.py +++ b/backend/views/core/invoices/edit.py @@ -53,7 +53,8 @@ def edit_invoice(request: HttpRequest, invoice_id): messages.success(request, "Invoice edited") return render(request, "partials/base/toasts.html") - return JsonResponse({"message": "Invoice successfully edited"}, status=200) + return render(request, "pages/invoices/dashboard/dashboard.html") + #return JsonResponse({"message": "Invoice successfully edited"}, status=200) From f04df6bc9920c8b7351a77f95b8608c0027ebdf8 Mon Sep 17 00:00:00 2001 From: HarryHuCodes <48864969+HarryHuCodes@users.noreply.github.com> Date: Tue, 12 Dec 2023 21:57:36 -0500 Subject: [PATCH 11/12] Added method to store existing invoice data in a dict based on id and pass it to frontend for populating fields during an edit --- backend/views/core/invoices/edit.py | 83 +++++++++++++++++++---------- 1 file changed, 56 insertions(+), 27 deletions(-) diff --git a/backend/views/core/invoices/edit.py b/backend/views/core/invoices/edit.py index 581f6ffd..2be5931d 100644 --- a/backend/views/core/invoices/edit.py +++ b/backend/views/core/invoices/edit.py @@ -6,42 +6,71 @@ from backend.models import Invoice from datetime import datetime +# RELATED PATH FILES : \frontend\templates\pages\invoices\dashboard\_fetch_body.html, \backend\urls.py -def invoice_edit_page_get(request, id): + +# Function that takes an invoice object and makes a dict of its attributes +def invoice_get_existing_data(invoice_obj): + stored_data = { + "og_name": invoice_obj.self_name, + "og_company": invoice_obj.self_company, + "og_address": invoice_obj.self_address, + "og_city": invoice_obj.self_city, + "og_county": invoice_obj.self_county, + "og_country": invoice_obj.self_country, + "og_cilent_name": invoice_obj.client_name, + "og_cilent_company": invoice_obj.client_company, + "og_cilent_address": invoice_obj.client_address, + "og_cilent_city": invoice_obj.client_city, + "og_cilent_county": invoice_obj.client_county, + "og_cilent_country": invoice_obj.client_country, + } + return stored_data + + +# gets invoice object from invoice id, convert obj to dict, and renders edit.html while passing the stored invoice values to frontend +def invoice_edit_page_get(request, invoice_id): context = {"type": "edit"} - return render(request, "pages/invoices/edit/edit.html", context) + try: + invoice = Invoice.objects.get(id=invoice_id) + except: + return JsonResponse({"message": "Invoice not found"}, status=404) + + # use to populate fields with existing data in edit_from_destination.html AND edit_to_destination.html + data_to_populate = invoice_get_existing_data(invoice) + return render(request, "pages/invoices/edit/edit.html", data_to_populate) +# when user changes/modifies any of the fields with new information (during edit invoice) @require_http_methods(["POST"]) def edit_invoice(request: HttpRequest, invoice_id): - try: invoice = Invoice.objects.get(id=invoice_id) except: return JsonResponse({"message": "Invoice not found"}, status=404) attributes_to_updates = { - "date_due": datetime.strptime(request.POST.get('date_due'), '%Y-%m-%d').date(), - "date_issued": request.POST.get('date_issued'), - "client_name": request.POST.get('to_name'), - "client_company": request.POST.get('to_company'), - "client_address": request.POST.get('to_address'), - "client_city": request.POST.get('to_city'), - "client_county": request.POST.get('to_county'), - "client_country": request.POST.get('to_country'), - "self_name": request.POST.get('from_name'), - "self_company": request.POST.get('from_company'), - "self_address": request.POST.get('from_address'), - "self_city": request.POST.get('from_city'), - "self_county": request.POST.get('from_county'), - "self_country": request.POST.get('from_country'), - "notes": request.POST.get('notes'), - "invoice_number": request.POST.get('invoice_number'), - "vat_number": request.POST.get('vat_number'), - "reference": request.POST.get('reference'), - "sort_code": request.POST.get('sort_code'), - "account_number": request.POST.get('account_number'), - "account_holder_name": request.POST.get('account_holder_name') + "date_due": datetime.strptime(request.POST.get("date_due"), "%Y-%m-%d").date(), + "date_issued": request.POST.get("date_issued"), + "client_name": request.POST.get("to_name"), + "client_company": request.POST.get("to_company"), + "client_address": request.POST.get("to_address"), + "client_city": request.POST.get("to_city"), + "client_county": request.POST.get("to_county"), + "client_country": request.POST.get("to_country"), + "self_name": request.POST.get("from_name"), + "self_company": request.POST.get("from_company"), + "self_address": request.POST.get("from_address"), + "self_city": request.POST.get("from_city"), + "self_county": request.POST.get("from_county"), + "self_country": request.POST.get("from_country"), + "notes": request.POST.get("notes"), + "invoice_number": request.POST.get("invoice_number"), + "vat_number": request.POST.get("vat_number"), + "reference": request.POST.get("reference"), + "sort_code": request.POST.get("sort_code"), + "account_number": request.POST.get("account_number"), + "account_holder_name": request.POST.get("account_holder_name"), } for column_name, new_value in attributes_to_updates.items(): @@ -54,12 +83,12 @@ def edit_invoice(request: HttpRequest, invoice_id): return render(request, "partials/base/toasts.html") return render(request, "pages/invoices/dashboard/dashboard.html") - #return JsonResponse({"message": "Invoice successfully edited"}, status=200) - + # return JsonResponse({"message": "Invoice successfully edited"}, status=200) +# decorator & view function for rendering page and updating invoice items in the backend @require_http_methods(["GET", "POST"]) def edit_invoice_page(request: HttpRequest, id): if request.method == "POST": - return edit_invoice(request,id) + return edit_invoice(request, id) return invoice_edit_page_get(request, id) From 2adec2143cdb42b3cdacdcf9473f8024292e7736 Mon Sep 17 00:00:00 2001 From: HarryHuCodes <48864969+HarryHuCodes@users.noreply.github.com> Date: Tue, 12 Dec 2023 21:59:25 -0500 Subject: [PATCH 12/12] Added comments and ran black formatter for code organization and clarity --- backend/api/invoices/edit.py | 45 +++++++++---------- backend/api/invoices/fetch.py | 14 +++--- backend/models.py | 11 +++-- backend/urls.py | 6 +-- backend/views/core/invoices/create.py | 4 +- .../templates/pages/invoices/edit/edit.html | 5 +++ .../invoices/edit/edit_from_destination.html | 5 ++- 7 files changed, 51 insertions(+), 39 deletions(-) diff --git a/backend/api/invoices/edit.py b/backend/api/invoices/edit.py index 70a06fe1..e7a8f5b7 100644 --- a/backend/api/invoices/edit.py +++ b/backend/api/invoices/edit.py @@ -9,34 +9,33 @@ @require_http_methods(["POST"]) def edit_invoice(request: HttpRequest): - try: - invoice = Invoice.objects.get(id=request.POST.get('invoice_id')) + invoice = Invoice.objects.get(id=request.POST.get("invoice_id")) except: return JsonResponse({"message": "Invoice not found"}, status=404) attributes_to_updates = { - "date_due": datetime.strptime(request.POST.get('date_due'), '%Y-%m-%d').date(), - "date_issued": request.POST.get('date_issued'), - "client_name": request.POST.get('to_name'), - "client_company": request.POST.get('to_company'), - "client_address": request.POST.get('to_address'), - "client_city": request.POST.get('to_city'), - "client_county": request.POST.get('to_county'), - "client_country": request.POST.get('to_country'), - "self_name": request.POST.get('from_name'), - "self_company": request.POST.get('from_company'), - "self_address": request.POST.get('from_address'), - "self_city": request.POST.get('from_city'), - "self_county": request.POST.get('from_county'), - "self_country": request.POST.get('from_country'), - "notes": request.POST.get('notes'), - "invoice_number": request.POST.get('invoice_number'), - "vat_number": request.POST.get('vat_number'), - "reference": request.POST.get('reference'), - "sort_code": request.POST.get('sort_code'), - "account_number": request.POST.get('account_number'), - "account_holder_name": request.POST.get('account_holder_name') + "date_due": datetime.strptime(request.POST.get("date_due"), "%Y-%m-%d").date(), + "date_issued": request.POST.get("date_issued"), + "client_name": request.POST.get("to_name"), + "client_company": request.POST.get("to_company"), + "client_address": request.POST.get("to_address"), + "client_city": request.POST.get("to_city"), + "client_county": request.POST.get("to_county"), + "client_country": request.POST.get("to_country"), + "self_name": request.POST.get("from_name"), + "self_company": request.POST.get("from_company"), + "self_address": request.POST.get("from_address"), + "self_city": request.POST.get("from_city"), + "self_county": request.POST.get("from_county"), + "self_country": request.POST.get("from_country"), + "notes": request.POST.get("notes"), + "invoice_number": request.POST.get("invoice_number"), + "vat_number": request.POST.get("vat_number"), + "reference": request.POST.get("reference"), + "sort_code": request.POST.get("sort_code"), + "account_number": request.POST.get("account_number"), + "account_holder_name": request.POST.get("account_holder_name"), } for column_name, new_value in attributes_to_updates.items(): diff --git a/backend/api/invoices/fetch.py b/backend/api/invoices/fetch.py index 076f6b0f..2e717e0c 100644 --- a/backend/api/invoices/fetch.py +++ b/backend/api/invoices/fetch.py @@ -57,8 +57,6 @@ def fetch_all_invoices(request: HttpRequest): ) ) - - # Initialize context variables context["selected_filters"] = [] context["all_filters"] = { @@ -98,12 +96,16 @@ def fetch_all_invoices(request: HttpRequest): # Combine OR conditions for each filter type with AND or_conditions &= or_conditions_filter - #check/update payment status to make sure it is correct before invoices are filtered and displayed + # check/update payment status to make sure it is correct before invoices are filtered and displayed for items in invoices: - if (items.date_due and timezone.now().date() > items.date_due) and items.payment_status == "pending": + if ( + items.date_due and timezone.now().date() > items.date_due + ) and items.payment_status == "pending": items.payment_status = "overdue" items.save() - if (items.date_due and timezone.now().date() < items.date_due) and items.payment_status == "overdue": + if ( + items.date_due and timezone.now().date() < items.date_due + ) and items.payment_status == "overdue": items.payment_status = "pending" items.save() @@ -116,7 +118,7 @@ def fetch_all_invoices(request: HttpRequest): context["sort"] = sort_by # Add invoices to the context - + context["invoices"] = invoices # Render the HTMX response diff --git a/backend/models.py b/backend/models.py index 97841c99..f655df3b 100644 --- a/backend/models.py +++ b/backend/models.py @@ -9,7 +9,6 @@ from shortuuid.django_fields import ShortUUIDField - class CustomUserManager(UserManager): def get_queryset(self): return super().get_queryset().select_related("user_profile") @@ -161,7 +160,7 @@ def get_total_price(self): def __str__(self): return self.description - + class Invoice(models.Model): STATUS_CHOICES = ( ("pending", "Pending"), @@ -210,11 +209,15 @@ class Invoice(models.Model): @property def dynamic_payment_status(self): - if self.date_due and timezone.now().date() > self.date_due and self.payment_status == "pending": + if ( + self.date_due + and timezone.now().date() > self.date_due + and self.payment_status == "pending" + ): return "overdue" else: return self.payment_status - + def __str__(self): invoice_id = self.invoice_id or self.id if self.client_name: diff --git a/backend/urls.py b/backend/urls.py index a9f610c2..efddd38f 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -116,15 +116,15 @@ invoices.create.create_invoice_page, name="invoices dashboard create", ), - #path( + # path( # "dashboard/invoices/", # invoices.dashboard.invoices_dashboard_id, # name="invoices dashboard edit", - #), + # ), path( "dashboard/invoices/edit/", invoices.edit.edit_invoice_page, - #invoices.edit.invoice_edit_page_get, + # invoices.edit.invoice_edit_page_get, name="invoices dashboard edit", ), # path('dashboard/invoices//edit', invoices.dashboard.invoices_dash~board_id, name='invoices dashboard'), diff --git a/backend/views/core/invoices/create.py b/backend/views/core/invoices/create.py index 5f7aa62e..f701dc6e 100644 --- a/backend/views/core/invoices/create.py +++ b/backend/views/core/invoices/create.py @@ -27,7 +27,7 @@ def invoice_page_post(request: HttpRequest): invoice = Invoice.objects.create( user=request.user, - date_due=datetime.strptime(request.POST.get("date_due"), '%Y-%m-%d').date(), + date_due=datetime.strptime(request.POST.get("date_due"), "%Y-%m-%d").date(), date_issued=request.POST.get("date_issued"), client_name=request.POST.get("to_name"), client_company=request.POST.get("to_company"), @@ -70,4 +70,4 @@ def create_invoice_page(request: HttpRequest): def edit_invoice_page(request: HttpRequest): if request.method == "POST": return invoice_page_post(request) - return invoice_page_get(request) \ No newline at end of file + return invoice_page_get(request) diff --git a/frontend/templates/pages/invoices/edit/edit.html b/frontend/templates/pages/invoices/edit/edit.html index 44565e6c..d3c62a64 100644 --- a/frontend/templates/pages/invoices/edit/edit.html +++ b/frontend/templates/pages/invoices/edit/edit.html @@ -64,4 +64,9 @@