Features: 1) Add type annotations to methods in payments serializers for enhanced type safety;

Fixes: 1) Correct unnecessary use of `Collection` type hint in core and simple serializers; 2) Fix translation of "blacklisted tokens" in Russian localization;

Extra: 1) Simplify conditional logic in `get_children` methods across serializers; 2) Remove unused imports and improve code readability.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-29 21:30:17 +03:00
parent 64be995ad4
commit bc36db57e1
5 changed files with 8 additions and 25 deletions

View file

@ -1,7 +1,5 @@
import logging
from collections.abc import Collection
from contextlib import suppress
from typing import Any
from django.contrib.auth.models import AnonymousUser
from django.core.cache import cache
@ -115,21 +113,14 @@ class CategoryDetailSerializer(ModelSerializer):
return filterable_results
def get_children(self, obj) -> Collection[Any]:
def get_children(self, obj) -> list:
request = self.context.get("request")
if request is not None and request.user.has_perm("view_category"):
children = obj.children.all()
else:
children = obj.children.filter(is_active=True)
if obj.children.exists():
return (
CategoryDetailSerializer(children, many=True, context=self.context).data
if obj.children.exists()
else []
)
else:
return []
return CategoryDetailSerializer(children, many=True, context=self.context).data if obj.children.exists() else []
class BrandDetailSerializer(ModelSerializer):

View file

@ -1,4 +1,3 @@
from collections.abc import Collection
from contextlib import suppress
from rest_framework.fields import JSONField, SerializerMethodField
@ -59,21 +58,14 @@ class CategorySimpleSerializer(ModelSerializer):
return obj.image.url
return None
def get_children(self, obj) -> Collection:
def get_children(self, obj) -> list:
request = self.context.get("request")
if request is not None and request.user.has_perm("view_category"):
children = obj.children.all()
else:
children = obj.children.filter(is_active=True)
if obj.children.exists():
return (
CategorySimpleSerializer(children, many=True, context=self.context).data
if obj.children.exists()
else []
)
else:
return []
return CategorySimpleSerializer(children, many=True, context=self.context).data if obj.children.exists() else []
class BrandSimpleSerializer(ModelSerializer):

View file

@ -19,11 +19,11 @@ class TransactionProcessSerializer(ModelSerializer):
order_hr_id = SerializerMethodField(read_only=True, required=False)
order_uuid = SerializerMethodField(read_only=True, required=False)
def get_order_hr_id(self, obj: Transaction):
def get_order_hr_id(self, obj: Transaction) -> str | None:
return obj.order.human_readable_id if obj.order else None # type: ignore
def get_order_uuid(self, obj: Transaction):
return obj.order.uuid if obj.order else None # type: ignore
def get_order_uuid(self, obj: Transaction) -> str | None:
return str(obj.order.uuid) if obj.order else None # type: ignore
class Meta:
model = Transaction

View file

@ -282,7 +282,7 @@ msgstr "Токен в черном списке"
#: vibes_auth/models.py:131
msgid "blacklisted tokens"
msgstr "Токены, внесенные в черный список"
msgstr "Чёрный список токенов"
#: vibes_auth/serializers.py:110 vibes_auth/serializers.py:132
msgid "no active account"