* Renamed GlobalSettings.get_yuan_rate -> full_yuan_rate property

* In GlobalSettingsSerializer show full yuan rate
This commit is contained in:
Phil Zhitnikov 2023-11-24 17:58:42 +04:00
parent d22f492df0
commit 8a7b53b069
3 changed files with 7 additions and 6 deletions

View File

@ -66,7 +66,8 @@ class GlobalSettings(models.Model):
InMemoryCache.set('GlobalSettings', obj) InMemoryCache.set('GlobalSettings', obj)
return 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) """ """ Get rate either from CurrencyAPI or from DB (if fresh enough) """
if self.yuan_rate_last_updated is not None: if self.yuan_rate_last_updated is not None:
diff_minutes = (timezone.now() - self.yuan_rate_last_updated).total_seconds() / 60 diff_minutes = (timezone.now() - self.yuan_rate_last_updated).total_seconds() / 60
@ -294,7 +295,7 @@ class ChecklistQuerySet(models.QuerySet):
return self.annotate( return self.annotate(
_yuan_rate=Case( _yuan_rate=Case(
When(price_snapshot_id__isnull=False, then=F('price_snapshot__yuan_rate')), 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')) _price_rub=Ceil(F('_yuan_rate') * F('price_yuan'))
) )
@ -471,7 +472,7 @@ class Checklist(models.Model):
if self.price_snapshot_id: if self.price_snapshot_id:
yuan_rate = self.price_snapshot.yuan_rate yuan_rate = self.price_snapshot.yuan_rate
else: else:
yuan_rate = GlobalSettings.load().get_yuan_rate() yuan_rate = GlobalSettings.load().full_yuan_rate
return math.ceil(yuan_rate * self.price_yuan) return math.ceil(yuan_rate * self.price_yuan)
@ -506,7 +507,7 @@ class Checklist(models.Model):
if self.price_snapshot_id: if self.price_snapshot_id:
return self.price_snapshot.yuan_rate return self.price_snapshot.yuan_rate
else: else:
return GlobalSettings.load().get_yuan_rate() return GlobalSettings.load().full_yuan_rate
@property @property
def delivery_price_CN(self) -> Decimal: def delivery_price_CN(self) -> Decimal:

View File

@ -211,7 +211,7 @@ class AnonymousUserChecklistSerializer(ChecklistSerializer):
class GlobalSettingsSerializer(serializers.ModelSerializer): 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) yuan_rate_last_updated = serializers.DateTimeField(read_only=True)
chinadelivery = serializers.DecimalField(source='delivery_price_CN', max_digits=10, decimal_places=2) 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) commission = serializers.DecimalField(source='commission_rub', max_digits=10, decimal_places=2)

View File

@ -208,7 +208,7 @@ class StatisticsAPI(viewsets.GenericViewSet):
@action(url_path='orders', detail=False, methods=['get']) @action(url_path='orders', detail=False, methods=['get'])
def stats_by_orders(self, request, *args, **kwargs): def stats_by_orders(self, request, *args, **kwargs):
global_settings = GlobalSettings.load() global_settings = GlobalSettings.load()
yuan_rate = global_settings.get_yuan_rate() yuan_rate = global_settings.full_yuan_rate
# Prepare query to collect the stats # Prepare query to collect the stats
qs = self.get_queryset() \ qs = self.get_queryset() \