Skip to content

Commit 365aa2b

Browse files
author
pietro convalle
committed
formatting and typing
1 parent 1013a67 commit 365aa2b

File tree

11 files changed

+74
-34
lines changed

11 files changed

+74
-34
lines changed

answerking_app/models/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def create(self, validated_data: dict) -> Order:
145145
)
146146
for order_item in order_items_data:
147147
item_data: ItemType = order_item.pop("item")
148-
item: Item = Item.objects.get(pk=item_data["id"])
148+
item: Item = Item.objects.get(pk=item_data["id"]) # type: ignore[reportTypedDictNotRequiredAccess]
149149
OrderLine.objects.create(order=order, item=item, **order_item)
150150
order.calculate_total()
151151
return order

answerking_app/tests/test_categories.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
from answerking_app.models.models import Category, Item
66
from answerking_app.utils.ErrorType import ErrorMessage
7-
from answerking_app.utils.model_types import (CategoryType, DetailError,
8-
ItemType)
7+
from answerking_app.utils.model_types import (
8+
CategoryType,
9+
DetailError,
10+
ItemType,
11+
)
912

1013
client = Client()
1114

answerking_app/tests/test_orderlines.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from django.test import Client, TestCase
22

33
from answerking_app.models.models import Item, Order, Status
4-
from answerking_app.utils.ErrorType import ErrorMessage
5-
from answerking_app.utils.model_types import DetailError, OrderType
4+
from answerking_app.utils.model_types import (
5+
DetailError,
6+
OrderItemType,
7+
OrderType,
8+
)
69

710
client = Client()
811

@@ -78,7 +81,7 @@ def test_add_new_orderline_valid_returns_ok(self):
7881
],
7982
"total": "6.50",
8083
}
81-
post_data: OrderType = {"quantity": 1}
84+
post_data: OrderItemType = {"quantity": 1}
8285

8386
# Act
8487
response = client.put(
@@ -109,7 +112,7 @@ def test_update_existing_orderline_valid_returns_ok(self):
109112
],
110113
"total": "2.50",
111114
}
112-
post_data: OrderType = {"quantity": 1}
115+
post_data: OrderItemType = {"quantity": 1}
113116

114117
# Act
115118
response = client.put(
@@ -132,7 +135,7 @@ def test_update_existing_orderline_zero_quantity_returns_ok(self):
132135
"order_items": [],
133136
"total": "0.00",
134137
}
135-
post_data: OrderType = {"quantity": 0}
138+
post_data: OrderItemType = {"quantity": 0}
136139

137140
# Act
138141
response = client.put(

answerking_app/tests/test_orders.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
from answerking_app.models.models import Item, Order, OrderLine, Status
66
from answerking_app.utils.ErrorType import ErrorMessage
7-
from answerking_app.utils.model_types import (DetailError, OrderItemType,
8-
OrderType)
7+
from answerking_app.utils.model_types import (
8+
DetailError,
9+
OrderItemType,
10+
OrderType,
11+
)
912

1013
client = Client()
1114

@@ -202,7 +205,7 @@ def test_post_valid_with_empty_items_returns_ok(self):
202205
# Arrange
203206
old_list = client.get("/api/orders").json()
204207

205-
post_data: NewOrderType = {
208+
post_data: OrderType = {
206209
"address": "test street 123",
207210
"order_items": [],
208211
}

answerking_app/utils/mixins/NotFoundDetailMixins.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.core.exceptions import ObjectDoesNotExist
22
from django.http import Http404
3+
from rest_framework import status
34
from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin
45
from rest_framework.request import Request
56
from rest_framework.response import Response
@@ -10,17 +11,19 @@
1011
def NotFoundErrorDetailed(exc: Http404 | ObjectDoesNotExist):
1112
if isinstance(exc, ObjectDoesNotExist):
1213
return HttpErrorResponse(
13-
status=404,
14+
status=status.HTTP_404_NOT_FOUND,
1415
detail="Object was not Found",
1516
title="Resource not found",
1617
extensions={"errors": exc.args},
1718
)
18-
if isinstance(exc, Http404):
19+
elif isinstance(exc, Http404):
1920
return HttpErrorResponse(
20-
status=404,
21+
status=status.HTTP_404_NOT_FOUND,
2122
detail="Not Found",
2223
title="Resource not found",
2324
)
25+
else:
26+
return HttpErrorResponse(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
2427

2528

2629
class GetNotFoundDetailMixin(RetrieveModelMixin):

answerking_app/utils/mixins/OrderItemMixins.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from rest_framework.utils.serializer_helpers import ReturnDict
77

88
from answerking_app.models.models import Item, Order
9-
from answerking_app.models.serializers import (OrderLineSerializer,
10-
OrderSerializer)
9+
from answerking_app.models.serializers import (
10+
OrderLineSerializer,
11+
OrderSerializer,
12+
)
1113
from answerking_app.utils.mixins.ApiExceptions import HttpErrorResponse
1214

1315

answerking_app/utils/mixins/SerializeErrorDetailRFCMixins.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from rest_framework import status
12
from rest_framework.exceptions import ParseError
23
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin
34
from rest_framework.request import Request
@@ -10,18 +11,20 @@
1011
def ValidationErrorDetailed(exc: ValidationError | ParseError):
1112
if isinstance(exc, ValidationError):
1213
return HttpErrorResponse(
13-
status=400,
14+
status=status.HTTP_404_NOT_FOUND,
1415
detail="Validation Error",
1516
title="Invalid input.",
1617
extensions={"errors": exc.detail},
1718
)
1819
elif isinstance(exc, ParseError):
1920
return HttpErrorResponse(
20-
status=400,
21+
status=status.HTTP_404_NOT_FOUND,
2122
detail="Parsing JSON Error",
2223
title="Invalid input json.",
2324
extensions={"errors": exc.detail},
2425
)
26+
else:
27+
return HttpErrorResponse(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
2528

2629

2730
class CreateErrorDetailMixin(CreateModelMixin):

answerking_app/utils/model_types.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from typing import Any
22

33
from typing_extensions import ( # for Python <3.11 with (Not)Required
4-
NotRequired, TypedDict)
4+
NotRequired,
5+
TypedDict,
6+
)
57

68

79
class ItemType(TypedDict):
@@ -16,20 +18,21 @@ class ItemType(TypedDict):
1618
class CategoryType(TypedDict):
1719
id: NotRequired[int]
1820
name: NotRequired[str]
19-
items: "NotRequired[list[ItemType]]"
21+
items: NotRequired["list[ItemType]"]
2022

2123

2224
class OrderItemType(TypedDict):
23-
id: int
25+
id: NotRequired[int]
2426
name: NotRequired[str]
2527
price: NotRequired[str]
2628
quantity: int
2729
sub_total: NotRequired[str]
2830

2931

3032
class OrderType(TypedDict):
33+
id: NotRequired[int]
3134
address: NotRequired[str]
32-
order_items: "NotRequired[list[OrderItemType]]"
35+
order_items: NotRequired["list[OrderItemType]"]
3336
status: NotRequired[str]
3437
total: NotRequired[str]
3538

@@ -39,6 +42,6 @@ class DetailError(TypedDict):
3942
type: str
4043
title: str
4144
instance: NotRequired[str]
42-
errors: "NotRequired[str | list[Any] | dict[Any, Any]]"
45+
errors: NotRequired["str | list[Any] | dict[Any, Any]"]
4346
status: NotRequired[int]
4447
traceID: NotRequired[str]

answerking_app/views/category_views.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
from answerking_app.models.models import Category
88
from answerking_app.models.serializers import CategorySerializer
99
from answerking_app.utils.mixins.CategoryItemMixins import (
10-
CategoryItemRemoveMixin, CategoryItemUpdateMixin)
10+
CategoryItemRemoveMixin,
11+
CategoryItemUpdateMixin,
12+
)
1113
from answerking_app.utils.mixins.IntegrityHandlerMixins import (
12-
CreateIntegrityHandlerMixin, UpdateIntegrityHandlerMixin)
14+
CreateIntegrityHandlerMixin,
15+
UpdateIntegrityHandlerMixin,
16+
)
1317
from answerking_app.utils.mixins.NotFoundDetailMixins import (
14-
GetNotFoundDetailMixin, UpdateNotFoundDetailMixin)
18+
GetNotFoundDetailMixin,
19+
UpdateNotFoundDetailMixin,
20+
)
1521
from answerking_app.utils.mixins.SerializeErrorDetailRFCMixins import (
16-
CreateErrorDetailMixin, UpdateErrorDetailMixin)
22+
CreateErrorDetailMixin,
23+
UpdateErrorDetailMixin,
24+
)
1725

1826

1927
class CategoryListView(

answerking_app/views/item_views.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
from answerking_app.models.models import Item
77
from answerking_app.models.serializers import ItemSerializer
88
from answerking_app.utils.mixins.IntegrityHandlerMixins import (
9-
CreateIntegrityHandlerMixin, UpdateIntegrityHandlerMixin)
9+
CreateIntegrityHandlerMixin,
10+
UpdateIntegrityHandlerMixin,
11+
)
1012
from answerking_app.utils.mixins.ItemMixins import DestroyItemMixin
1113
from answerking_app.utils.mixins.NotFoundDetailMixins import (
12-
GetNotFoundDetailMixin, UpdateNotFoundDetailMixin)
14+
GetNotFoundDetailMixin,
15+
UpdateNotFoundDetailMixin,
16+
)
1317
from answerking_app.utils.mixins.SerializeErrorDetailRFCMixins import (
14-
CreateErrorDetailMixin, UpdateErrorDetailMixin)
18+
CreateErrorDetailMixin,
19+
UpdateErrorDetailMixin,
20+
)
1521

1622

1723
class ItemListView(

0 commit comments

Comments
 (0)