Skip to content

Commit fbbdb4e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 30538b8 commit fbbdb4e

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

ecommerce/serializers/__init__.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,12 @@ class OrderHistorySerializer(serializers.ModelSerializer):
567567
@extend_schema_field(serializers.ListField)
568568
def get_titles(self, instance):
569569
titles = []
570-
# Access prefetched lines without method calls to avoid N+1
571-
lines = getattr(instance, '_prefetched_objects_cache', {}).get('lines', [])
570+
# Access prefetched lines without method calls to avoid N+1
571+
lines = getattr(instance, "_prefetched_objects_cache", {}).get("lines", [])
572572
if not lines:
573573
# Fallback if not prefetched
574574
lines = list(instance.lines.all())
575-
575+
576576
product_ids = [line.product_version.field_dict["id"] for line in lines]
577577
products_by_id = models.Product.all_objects.in_bulk(product_ids)
578578

@@ -710,12 +710,16 @@ def to_representation(self, instance):
710710
raise AttributeError # noqa: TRY004
711711

712712
# Access prefetched transactions without method calls to avoid N+1
713-
transactions = getattr(instance, '_prefetched_objects_cache', {}).get('transactions', [])
713+
transactions = getattr(instance, "_prefetched_objects_cache", {}).get(
714+
"transactions", []
715+
)
714716
if not transactions:
715-
# Fallback if not prefetched
717+
# Fallback if not prefetched
716718
transactions = list(instance.transactions.all())
717-
718-
transaction = max(transactions, key=lambda t: t.created_on) if transactions else None
719+
720+
transaction = (
721+
max(transactions, key=lambda t: t.created_on) if transactions else None
722+
)
719723

720724
return transaction # noqa: RET504
721725

@@ -833,11 +837,13 @@ class Meta:
833837
class TransactionLineSerializer(serializers.BaseSerializer):
834838
def to_representation(self, instance):
835839
# Access prefetched discounts without method calls to avoid N+1
836-
discounts = getattr(instance.order, '_prefetched_objects_cache', {}).get('discounts', [])
840+
discounts = getattr(instance.order, "_prefetched_objects_cache", {}).get(
841+
"discounts", []
842+
)
837843
if not discounts:
838844
# Fallback if not prefetched
839845
discounts = list(instance.order.discounts.all())
840-
846+
841847
coupon_redemption = discounts[0] if discounts else None
842848
discount = 0.0
843849

@@ -909,11 +915,13 @@ def get_order(self, instance):
909915
def get_coupon(self, instance):
910916
"""Get discount code from the discount redemption if available"""
911917
# Access prefetched discounts without method calls to avoid N+1
912-
discounts = getattr(instance, '_prefetched_objects_cache', {}).get('discounts', [])
918+
discounts = getattr(instance, "_prefetched_objects_cache", {}).get(
919+
"discounts", []
920+
)
913921
if not discounts:
914922
# Fallback if not prefetched
915923
discounts = list(instance.discounts.all())
916-
924+
917925
coupon_redemption = discounts[0] if discounts else None
918926
if not coupon_redemption:
919927
return None

ecommerce/serializers/v0/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,11 @@ class OrderHistorySerializer(serializers.ModelSerializer):
654654
def get_titles(self, instance):
655655
titles = []
656656
# Access prefetched lines without method calls to avoid N+1
657-
lines = getattr(instance, '_prefetched_objects_cache', {}).get('lines', [])
657+
lines = getattr(instance, "_prefetched_objects_cache", {}).get("lines", [])
658658
if not lines:
659659
# Fallback if not prefetched
660660
lines = list(instance.lines.all())
661-
661+
662662
product_ids = [line.product_version.field_dict["id"] for line in lines]
663663
products_by_id = models.Product.all_objects.in_bulk(product_ids)
664664

@@ -816,12 +816,16 @@ def to_representation(self, instance):
816816
raise AttributeError # noqa: TRY004
817817

818818
# Access prefetched transactions without method calls to avoid N+1
819-
transactions = getattr(instance, '_prefetched_objects_cache', {}).get('transactions', [])
819+
transactions = getattr(instance, "_prefetched_objects_cache", {}).get(
820+
"transactions", []
821+
)
820822
if not transactions:
821823
# Fallback if not prefetched
822824
transactions = list(instance.transactions.all())
823-
824-
transaction = max(transactions, key=lambda t: t.created_on) if transactions else None
825+
826+
transaction = (
827+
max(transactions, key=lambda t: t.created_on) if transactions else None
828+
)
825829

826830
return transaction # noqa: RET504
827831

@@ -966,11 +970,13 @@ def get_order(self, instance):
966970
def get_coupon(self, instance):
967971
"""Get discount code from the discount redemption if available"""
968972
# Access prefetched discounts without method calls to avoid N+1
969-
discounts = getattr(instance, '_prefetched_objects_cache', {}).get('discounts', [])
973+
discounts = getattr(instance, "_prefetched_objects_cache", {}).get(
974+
"discounts", []
975+
)
970976
if not discounts:
971977
# Fallback if not prefetched
972978
discounts = list(instance.discounts.all())
973-
979+
974980
coupon_redemption = discounts[0] if discounts else None
975981
if not coupon_redemption:
976982
return None

0 commit comments

Comments
 (0)