From 8a7b53b069a80a2b21fd27ccb46c9aa66ff1c50d Mon Sep 17 00:00:00 2001 From: phzhik Date: Fri, 24 Nov 2023 17:58:42 +0400 Subject: [PATCH] * Renamed GlobalSettings.get_yuan_rate -> full_yuan_rate property * In GlobalSettingsSerializer show full yuan rate --- store/models.py | 9 +++++---- store/serializers.py | 2 +- store/views.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/store/models.py b/store/models.py index 231956b..7df56c9 100644 --- a/store/models.py +++ b/store/models.py @@ -66,7 +66,8 @@ class GlobalSettings(models.Model): InMemoryCache.set('GlobalSettings', obj) return obj - def get_yuan_rate(self): + @property + def full_yuan_rate(self): """ Get rate either from CurrencyAPI or from DB (if fresh enough) """ if self.yuan_rate_last_updated is not None: diff_minutes = (timezone.now() - self.yuan_rate_last_updated).total_seconds() / 60 @@ -294,7 +295,7 @@ class ChecklistQuerySet(models.QuerySet): return self.annotate( _yuan_rate=Case( When(price_snapshot_id__isnull=False, then=F('price_snapshot__yuan_rate')), - default=GlobalSettings.load().get_yuan_rate() + default=GlobalSettings.load().full_yuan_rate ), _price_rub=Ceil(F('_yuan_rate') * F('price_yuan')) ) @@ -471,7 +472,7 @@ class Checklist(models.Model): if self.price_snapshot_id: yuan_rate = self.price_snapshot.yuan_rate else: - yuan_rate = GlobalSettings.load().get_yuan_rate() + yuan_rate = GlobalSettings.load().full_yuan_rate return math.ceil(yuan_rate * self.price_yuan) @@ -506,7 +507,7 @@ class Checklist(models.Model): if self.price_snapshot_id: return self.price_snapshot.yuan_rate else: - return GlobalSettings.load().get_yuan_rate() + return GlobalSettings.load().full_yuan_rate @property def delivery_price_CN(self) -> Decimal: diff --git a/store/serializers.py b/store/serializers.py index 208e81b..1759fe8 100644 --- a/store/serializers.py +++ b/store/serializers.py @@ -211,7 +211,7 @@ class AnonymousUserChecklistSerializer(ChecklistSerializer): class GlobalSettingsSerializer(serializers.ModelSerializer): - currency = serializers.DecimalField(source='yuan_rate', read_only=True, max_digits=10, decimal_places=2) + currency = serializers.DecimalField(source='full_yuan_rate', read_only=True, max_digits=10, decimal_places=2) yuan_rate_last_updated = serializers.DateTimeField(read_only=True) chinadelivery = serializers.DecimalField(source='delivery_price_CN', max_digits=10, decimal_places=2) commission = serializers.DecimalField(source='commission_rub', max_digits=10, decimal_places=2) diff --git a/store/views.py b/store/views.py index eeaf7c0..0dcddf4 100644 --- a/store/views.py +++ b/store/views.py @@ -208,7 +208,7 @@ class StatisticsAPI(viewsets.GenericViewSet): @action(url_path='orders', detail=False, methods=['get']) def stats_by_orders(self, request, *args, **kwargs): global_settings = GlobalSettings.load() - yuan_rate = global_settings.get_yuan_rate() + yuan_rate = global_settings.full_yuan_rate # Prepare query to collect the stats qs = self.get_queryset() \