diff --git a/backend/api/clients/delete.py b/backend/api/clients/delete.py new file mode 100644 index 00000000..9159d529 --- /dev/null +++ b/backend/api/clients/delete.py @@ -0,0 +1,46 @@ +from django.contrib import messages +from django.shortcuts import render +from django.contrib.auth.decorators import login_required +from django.views.decorators.http import require_http_methods + +from backend.models import Client +from backend.types.htmx import HtmxHttpRequest + + +@require_http_methods(["DELETE"]) +@login_required +def client_delete(request: HtmxHttpRequest, id: int): + try: + client = Client.objects.get(id=id) + except Client.DoesNotExist: + messages.error(request, "Client not found") + return render(request, "pages/clients/dashboard/_table.html", {"delete": True}) + + if not client: + messages.error(request, "Client not found") + return render(request, "pages/clients/dashboard/_table.html", {"delete": True}) + + if ( + not request.user.is_authenticated + or request.user != client.user + or request.user.logged_in_as_team + and request.user.logged_in_as_team != client.organization + ): + messages.error(request, "You do not have permission to delete this client") + return render(request, "pages/clients/dashboard/_table.html", {"delete": True}) + + client.delete() + messages.success(request, f'Client "{client.name}" deleted successfully') + + if request.user.logged_in_as_team: + return render( + request, + "pages/clients/dashboard/_table.html", + {"clients": Client.objects.filter(organization=request.user.logged_in_as_team).order_by("-name"), "delete": True}, + ) + else: + return render( + request, + "pages/clients/dashboard/_table.html", + {"clients": Client.objects.filter(user=request.user).order_by("-name"), "delete": True}, + ) diff --git a/backend/api/clients/urls.py b/backend/api/clients/urls.py index 6ae98023..08f3f522 100644 --- a/backend/api/clients/urls.py +++ b/backend/api/clients/urls.py @@ -1,5 +1,6 @@ from django.urls import path -from . import fetch +from . import fetch, delete + urlpatterns = [ path( @@ -12,6 +13,10 @@ fetch.fetch_clients_dropdown, name="fetch dropdown", ), + path( + "delete//", + delete.client_delete, + name="delete", + ), ] - app_name = "clients" diff --git a/frontend/templates/pages/clients/create/create.html b/frontend/templates/pages/clients/create/create.html index 7b6d1e7b..c06abcef 100644 --- a/frontend/templates/pages/clients/create/create.html +++ b/frontend/templates/pages/clients/create/create.html @@ -4,7 +4,7 @@
{% csrf_token %} -
REQUIRED DETAILS
+
IDENTIFICATION DETAILS [REQUIRED]