From 4cfb5395b04272a79a70aafd8d4194215b3c4ad7 Mon Sep 17 00:00:00 2001 From: Trey <73353716+TreyWW@users.noreply.github.com> Date: Sat, 19 Oct 2024 19:27:35 +0100 Subject: [PATCH] started replacing Response with new APIResponse to prefill metadata when we add it (#517) Signed-off-by: Trey <73353716+TreyWW@users.noreply.github.com> --- .../api/public/endpoints/Invoices/create.py | 9 +++++---- .../core/api/public/endpoints/clients/list.py | 8 +++++++- backend/core/api/public/helpers/response.py | 19 +++++++++++++++++++ backend/core/models.py | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 backend/core/api/public/helpers/response.py diff --git a/backend/core/api/public/endpoints/Invoices/create.py b/backend/core/api/public/endpoints/Invoices/create.py index dec229a3..3382c93c 100644 --- a/backend/core/api/public/endpoints/Invoices/create.py +++ b/backend/core/api/public/endpoints/Invoices/create.py @@ -6,6 +6,7 @@ from backend.clients.models import Client from backend.core.api.public.decorators import require_scopes +from backend.core.api.public.helpers.response import APIResponse from backend.core.api.public.serializers.invoices import InvoiceSerializer from backend.core.api.public.swagger_ui import TEAM_PARAMETER from backend.core.api.public.types import APIRequest @@ -101,25 +102,25 @@ def create_invoice_endpoint(request: APIRequest) -> Response: serializer = InvoiceSerializer(data=request.data) if not serializer.is_valid(): - return Response({"success": False, "message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) + return APIResponse(False, serializer.errors, status=status.HTTP_400_BAD_REQUEST) if "client_id" in request.data and request.data["client_id"]: try: client = get_client(request) serializer.validated_data["client_to"] = client except Client.DoesNotExist: - return Response({"success": False, "message": "Client not found"}, status=status.HTTP_400_BAD_REQUEST) + return APIResponse(False, "Client not found", status=status.HTTP_400_BAD_REQUEST) if "product_id" in request.data and request.data["product_id"]: try: items_data = get_products(request) serializer.validated_data["items"] = items_data except InvoiceProduct.DoesNotExist: - return Response({"success": False, "message": "InvoiceProduct not found"}, status=status.HTTP_400_BAD_REQUEST) + return APIResponse(False, "InvoiceProduct not found", status=status.HTTP_400_BAD_REQUEST) if request.team: invoice = serializer.save(organization=request.team) else: invoice = serializer.save(user=request.user) - return Response({"success": True, "invoice_id": invoice.id}, status=status.HTTP_201_CREATED) + return APIResponse(True, {"invoice_id": invoice.id}, status=status.HTTP_201_CREATED) diff --git a/backend/core/api/public/endpoints/clients/list.py b/backend/core/api/public/endpoints/clients/list.py index 75819b0d..af55c6b3 100644 --- a/backend/core/api/public/endpoints/clients/list.py +++ b/backend/core/api/public/endpoints/clients/list.py @@ -5,11 +5,14 @@ from rest_framework.response import Response from backend.core.api.public.decorators import require_scopes +from backend.core.api.public.helpers.response import APIResponse from backend.core.api.public.serializers.clients import ClientSerializer from backend.core.api.public.swagger_ui import TEAM_PARAMETER from backend.core.api.public.types import APIRequest from backend.core.service.clients.get import fetch_clients, FetchClientServiceResponse +from backend.models import Organization + @swagger_auto_schema( method="get", @@ -41,10 +44,13 @@ def list_clients_endpoint(request: APIRequest): search_text = request.data.get("search") + if not request.team and isinstance(request.auth.owner, Organization): + return APIResponse(False, "When using a team API Key the team_id field must be provided.") + clients: FetchClientServiceResponse = fetch_clients(request, search_text=search_text, team=request.team) # queryset = paginator.paginate_queryset(clients, request) serializer = ClientSerializer(clients.response, many=True) - return Response({"success": True, "clients": serializer.data}) + return APIResponse(True, {"clients": serializer.data}) diff --git a/backend/core/api/public/helpers/response.py b/backend/core/api/public/helpers/response.py new file mode 100644 index 00000000..f3f43a42 --- /dev/null +++ b/backend/core/api/public/helpers/response.py @@ -0,0 +1,19 @@ +from rest_framework.response import Response + + +def APIResponse(success: bool = True, data: str | dict | None = None, meta=None, status: int = 0, **kwargs) -> Response: + """ + + Returns a rest_framework Response object, but prefills meta (success etc) aswell as the data with KWARGS. + + """ + meta = meta or {} + if not status and success: + status = 201 + elif not status: + status = 400 + + if success: + return Response({"meta": {"success": True, **meta}, "data": {**data, **kwargs}}, status=status) + else: + return Response({"meta": {"success": False}, "error": data}, status=status) diff --git a/backend/core/models.py b/backend/core/models.py index cdd4a7f9..3ef56225 100644 --- a/backend/core/models.py +++ b/backend/core/models.py @@ -254,7 +254,7 @@ def is_logged_in_as_team(self, request) -> bool: if isinstance(request.auth, User): return False - if request.auth and request.auth.team_id == self.id: + if request.auth and request.auth.organization_id == self.id: return True return False