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:
parent
64be995ad4
commit
bc36db57e1
5 changed files with 8 additions and 25 deletions
|
|
@ -1,7 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import Collection
|
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
|
@ -115,21 +113,14 @@ class CategoryDetailSerializer(ModelSerializer):
|
||||||
|
|
||||||
return filterable_results
|
return filterable_results
|
||||||
|
|
||||||
def get_children(self, obj) -> Collection[Any]:
|
def get_children(self, obj) -> list:
|
||||||
request = self.context.get("request")
|
request = self.context.get("request")
|
||||||
if request is not None and request.user.has_perm("view_category"):
|
if request is not None and request.user.has_perm("view_category"):
|
||||||
children = obj.children.all()
|
children = obj.children.all()
|
||||||
else:
|
else:
|
||||||
children = obj.children.filter(is_active=True)
|
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 []
|
||||||
return (
|
|
||||||
CategoryDetailSerializer(children, many=True, context=self.context).data
|
|
||||||
if obj.children.exists()
|
|
||||||
else []
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class BrandDetailSerializer(ModelSerializer):
|
class BrandDetailSerializer(ModelSerializer):
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
from collections.abc import Collection
|
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
|
||||||
from rest_framework.fields import JSONField, SerializerMethodField
|
from rest_framework.fields import JSONField, SerializerMethodField
|
||||||
|
|
@ -59,21 +58,14 @@ class CategorySimpleSerializer(ModelSerializer):
|
||||||
return obj.image.url
|
return obj.image.url
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_children(self, obj) -> Collection:
|
def get_children(self, obj) -> list:
|
||||||
request = self.context.get("request")
|
request = self.context.get("request")
|
||||||
if request is not None and request.user.has_perm("view_category"):
|
if request is not None and request.user.has_perm("view_category"):
|
||||||
children = obj.children.all()
|
children = obj.children.all()
|
||||||
else:
|
else:
|
||||||
children = obj.children.filter(is_active=True)
|
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 []
|
||||||
return (
|
|
||||||
CategorySimpleSerializer(children, many=True, context=self.context).data
|
|
||||||
if obj.children.exists()
|
|
||||||
else []
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class BrandSimpleSerializer(ModelSerializer):
|
class BrandSimpleSerializer(ModelSerializer):
|
||||||
|
|
|
||||||
|
|
@ -19,11 +19,11 @@ class TransactionProcessSerializer(ModelSerializer):
|
||||||
order_hr_id = SerializerMethodField(read_only=True, required=False)
|
order_hr_id = SerializerMethodField(read_only=True, required=False)
|
||||||
order_uuid = 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
|
return obj.order.human_readable_id if obj.order else None # type: ignore
|
||||||
|
|
||||||
def get_order_uuid(self, obj: Transaction):
|
def get_order_uuid(self, obj: Transaction) -> str | None:
|
||||||
return obj.order.uuid if obj.order else None # type: ignore
|
return str(obj.order.uuid) if obj.order else None # type: ignore
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Transaction
|
model = Transaction
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -282,7 +282,7 @@ msgstr "Токен в черном списке"
|
||||||
|
|
||||||
#: vibes_auth/models.py:131
|
#: vibes_auth/models.py:131
|
||||||
msgid "blacklisted tokens"
|
msgid "blacklisted tokens"
|
||||||
msgstr "Токены, внесенные в черный список"
|
msgstr "Чёрный список токенов"
|
||||||
|
|
||||||
#: vibes_auth/serializers.py:110 vibes_auth/serializers.py:132
|
#: vibes_auth/serializers.py:110 vibes_auth/serializers.py:132
|
||||||
msgid "no active account"
|
msgid "no active account"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue