def _user_balance(self, valuta): q = self.profilebalance_set.filter( valuta=Valuta.objects.filter(value=valuta), profile=self, cancel=False, confirm=True) if not q: return _Zero _c1 = q.filter(accept=True).count() _c2 = q.aggregate(Sum('value')).get('value__sum') or _Zero md5key = strmd5sum("_user_balance" + str(q.count()) + str(valuta) + str(self.pk) + str(_c1) + str(_c2)) b = cache.get(md5key) if b is None: balance_plus = sum( q.filter(action="+", accept=True).distinct().extra(select={ 'total': """ CASE WHEN users_profilebalance.min_commission and (users_profilebalance.value * users_profilebalance.commission / 100) < users_profilebalance.min_commission THEN users_profilebalance.value - users_profilebalance.min_commission WHEN users_profilebalance.max_commission > 0 and (users_profilebalance.value * users_profilebalance.commission / 100) > users_profilebalance.max_commission THEN users_profilebalance.value - users_profilebalance.max_commission ELSE (users_profilebalance.value * (1 - users_profilebalance.commission / 100)) END """ }, ).only('id').values_list('total', flat=True)) or _Zero balance_plus = normalized(balance_plus, where="DOWN") balance_minus = q.filter(action="-").distinct().aggregate( total=Sum('value')).get('total') or _Zero balance_minus = normalized(balance_minus, where="DOWN") b = balance_plus - balance_minus cache.set(md5key, b) return b
def action(self): md5key = strmd5sum("order actions" + str(self.pk)) a = cache.get(md5key) if a is None: if hasattr(self, 'sale'): a = 'sale' if hasattr(self, 'buy'): a = 'buy' cache.set(md5key, a) return a
def sum_from_commission(cls, valuta): _c1 = cls.objects.filter(cancel=False).count() _c2 = cls.objects.filter(completed=True).count() _md5key = strmd5sum("sum cr" + str(_c1) + str(_c2) + str(valuta)) _s = cache.get(_md5key) if _s is None: obj = cls.objects.filter( Q(pair__left__value=valuta) | Q(pair__right__value=valuta)).only('pair', 'rate', 'amount').distinct() _s = _Zero for c in obj: if c.is_action('sale'): if c.sale.pair.right.value == valuta: _s += c.sale._commission_debit if c.is_action('buy'): if c.buy.pair.left.value == valuta: _s += c.buy._commission_debit cache.set(_md5key, _s) return _s
def _md5key_subtotal(self): s = "Sale _md5key_subtotal" + self._keys return strmd5sum(s)
def _md5key_adeudo(self): s = "Buy _md5key__adeudo" + self._keys return strmd5sum(s)
def _md5key_total(self): s = "Buy _md5key_total" + self._keys return strmd5sum(s)
def sum_from_user_buy_sale( cls, user, valuta ): # TODO подсчет переписать на sql или результат писать в отдельную таблицу _c1 = cls.objects.filter(user=user).count() _c2 = cls.objects.filter(Q(buy__user=user) | Q(sale__user=user)).count() _c3 = cls.objects.filter( Q(buy__user=user) | Q(sale__user=user) | Q(sale__sale_sale__user=user) | Q(buy__buy_buy__user=user)).count() _c4 = cls.objects.filter(cancel=False).count() _c5 = cls.objects.filter(completed=True).count() _md5key = strmd5sum("sum" + str(_c1) + str(_c2) + str(_c3) + str(_c4) + str(_c5) + str(user.pk) + str(valuta)) _s = cache.get(_md5key) if _s is None: value_valuta = currency.models.Valuta.objects.filter(value=valuta) obj = cls.objects.filter(user=user).filter( pair=currency.models.TypePair.objects.filter( Q(left=value_valuta) | Q(right=value_valuta)) #Q(pair__left__value=valuta) | #Q(pair__right__value=valuta) ).distinct() _s = _Zero for c in obj: if c.is_action('sale'): # obj.filter(sale = currency.models.TypePair.objects.filter(left=currency.models.Valuta.objects.filter(value=valuta))) # # 589 # self.amount if not self.cancel else self._adeudo if c.sale.pair.left.value == valuta: _s += c.sale._debit_left pass # btc # obj.filter(sale = currency.models.TypePair.objects.filter(right=currency.models.Valuta.objects.filter(value=valuta))) # # 600 # normalized(self._total - self._commission_debit, where="DOWN") if c.sale.pair.right.value == valuta: _s -= c.sale._debit_right pass # usd if c.is_action('buy'): # obj.filter(buy = currency.models.TypePair.objects.filter(left=currency.models.Valuta.objects.filter(value=valuta))) # # 427 # normalized(self._total - self._commission_debit, where="DOWN") if c.buy.pair.left.value == valuta: # btc _s -= c.buy._debit_left pass # obj.filter(buy = currency.models.TypePair.objects.filter(right=currency.models.Valuta.objects.filter(value=valuta))) # # 435 # self._adeudo if c.buy.pair.right.value == valuta: # usd _s += c.buy._debit_right pass cache.set(_md5key, _s) return _s