Skip to content

Commit

Permalink
started replacing Response with new APIResponse to prefill metadata w…
Browse files Browse the repository at this point in the history
…hen we add it (#517)

Signed-off-by: Trey <[email protected]>
  • Loading branch information
TreyWW committed Oct 19, 2024
1 parent a25ce39 commit 4cfb539
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
9 changes: 5 additions & 4 deletions backend/core/api/public/endpoints/Invoices/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion backend/core/api/public/endpoints/clients/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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})
19 changes: 19 additions & 0 deletions backend/core/api/public/helpers/response.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4cfb539

Please sign in to comment.