Skip to content

Commit

Permalink
fixed mypy types
Browse files Browse the repository at this point in the history
  • Loading branch information
TreyWW committed Nov 14, 2024
1 parent 2078f72 commit 0b96d50
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
14 changes: 6 additions & 8 deletions backend/core/api/public/endpoints/Invoices/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def edit_invoice_endpoint(request: APIRequest):

@api_view(["POST"])
def change_status_endpoint(request, invoice_id: int, invoice_status: str):
invoice_status: Literal["paid", "draft", "pending"] = invoice_status.lower() if invoice_status else ""
new_status = invoice_status.lower() if invoice_status else ""

try:
invoice = Invoice.objects.get(id=invoice_id)
Expand All @@ -86,15 +86,13 @@ def change_status_endpoint(request, invoice_id: int, invoice_status: str):
if request.user.logged_in_as_team and request.user.logged_in_as_team != invoice.organization or request.user != invoice.user:
return APIResponse(False, {"error": "You do not have permission to edit this invoice"}, status=status.HTTP_403_FORBIDDEN)

if invoice_status not in ["paid", "draft", "pending"]:
return APIResponse(False, {"error": "Invalid status. Please choose from: pending, paid, draft"}, status=status.HTTP_400_BAD_REQUEST)

if invoice.status == invoice_status:
return APIResponse(False, {"error": f"Invoice status is already {invoice_status}"}, status=status.HTTP_400_BAD_REQUEST)
if invoice.status == new_status:
return APIResponse(False, {"error": f"Invoice status is already {new_status}"}, status=status.HTTP_400_BAD_REQUEST)

invoice.set_status(invoice_status)
if not invoice.set_status(new_status, save=True):
return APIResponse(False, {"error": "Invalid status. Please choose from: pending, paid, draft"}, status=status.HTTP_400_BAD_REQUEST)

return APIResponse(True, {"message": f"Invoice status been changed to <strong>{invoice_status}</strong>"}, status=status.HTTP_200_OK)
return APIResponse(True, {"message": f"Invoice status been changed to <strong>{new_status}</strong>"}, status=status.HTTP_200_OK)


@api_view(["POST"])
Expand Down
2 changes: 1 addition & 1 deletion backend/finance/api/invoices/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def edit_invoice(request: HtmxHttpRequest):
@require_POST
@web_require_scopes("invoices:write", True, True)
def change_status(request: HtmxHttpRequest, invoice_id: int, status: str) -> HttpResponse:
status: Literal["paid", "draft", "pending"] = status.lower() if status else ""
status = status.lower() if status else ""

if not request.htmx:
return redirect("finance:invoices:single:dashboard")
Expand Down
1 change: 1 addition & 0 deletions backend/finance/api/invoices/recurring/update_status.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Literal
from django.conf import settings
from django.contrib import messages
from django.http import HttpRequest, HttpResponse
Expand Down
4 changes: 3 additions & 1 deletion backend/finance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def __str__(self):

return f"Invoice #{invoice_id} for {client}"

def set_status(self, status: Literal["draft", "pending", "paid"], save=True):
def set_status(self, status: str, save=True):
if status not in ["draft", "pending", "paid"]:
return False
self.status = status
self.status_updated_at = timezone.now()
if save:
Expand Down

0 comments on commit 0b96d50

Please sign in to comment.