Ejemplo n.º 1
0
def minimum_collection_depth(collections: QuerySet) -> int:
    """
    Returns the minimum depth of the Collections in the given queryset.
    Call this before beginning a loop through Collections that will
    use {% format_collection collection min_depth %}.
    """
    return collections.aggregate(Min('depth'))['depth__min'] or 2
Ejemplo n.º 2
0
def get_center(stations_qs: QuerySet):
    p = stations_qs.aggregate(long_max=Max('longitude'),
                              long_min=Min('longitude'),
                              lat_max=Max('latitude'),
                              lat_min=Min('latitude'))

    x = p.get('lat_min') + (p.get('lat_max') - p.get('lat_min')) / 2
    y = p.get('long_min') + (p.get('long_max') - p.get('long_min')) / 2

    return {'x': x, 'y': y}
Ejemplo n.º 3
0
    def get_po_item_data(self, queryset: QuerySet = None) -> Tuple:
        if not queryset:
            # pylint: disable=no-member
            queryset = self.itemthroughmodel_set.all().select_related(
                'bill_model')

        return queryset, queryset.aggregate(
            amount_due=Sum('po_total_amount'),
            total_paid=Sum('bill_model__amount_paid'),
            total_items=Count('uuid'))
Ejemplo n.º 4
0
def sum_queryset(qs: QuerySet,
                 key: str = "amount",
                 default: Decimal = Decimal(0)) -> Decimal:
    """
    Returns aggregate sum of queryset 'amount' field.
    :param qs: QuerySet
    :param key: Field to sum (default: 'amount')
    :param default: Default value if no results
    :return: Sum of 'amount' field values (coalesced 0 if None)
    """
    res = qs.aggregate(b=Sum(key))["b"]
    return default if res is None else res
Ejemplo n.º 5
0
def get_provider_payment_info(
    list_of_settlement_payments: List[ForcedPaymentEvent],
    list_of_transactions: List[BatchTransferEvent],
    settlement_payment_claims: QuerySet,
    subtask_results_accepted_list: List[SubtaskResultsAccepted],
) -> Tuple[int, int]:
    assert isinstance(settlement_payment_claims, QuerySet)
    assert isinstance(list_of_settlement_payments, list)
    assert isinstance(list_of_transactions, list)
    assert isinstance(subtask_results_accepted_list, list)

    settlement_payment_total_price = sum_payments(list_of_settlement_payments)
    payments_price = sum_payments(list_of_transactions)
    satisfied_payments_price = settlement_payment_claims.aggregate(
        sum_of_already_satisfied_claims=Coalesce(Sum(
            'amount'), 0))['sum_of_already_satisfied_claims']
    subtasks_price = sum_subtask_price(subtask_results_accepted_list)

    amount_paid = payments_price + satisfied_payments_price + settlement_payment_total_price
    amount_pending = subtasks_price - amount_paid

    return (amount_paid, amount_pending)
Ejemplo n.º 6
0
 def execute(self, queryset: QuerySet) -> Decimal:
     """Return sum of queryset entries sums"""
     total_sum = queryset.aggregate(total_sum=Sum(self.sum_field_name))
     return total_sum['total_sum'] or Decimal('0')
Ejemplo n.º 7
0
 def get_denormalized_value(self, queryset: QuerySet):
     aggregate = queryset.aggregate(aggregate=self.aggregate)
     value = aggregate['aggregate']
     if value is None:
         return self.value_for_empty_set
     return value
Ejemplo n.º 8
0
 def get_max(qs: QuerySet) -> int:
     return qs.aggregate(models.Max('number'))['number__max']
Ejemplo n.º 9
0
    def __sum(query: QuerySet) -> Optional[float]:
        """Helper method to calculate sum of value fields"""

        return query.aggregate(total=Sum('value'))['total']
Ejemplo n.º 10
0
 def _get_sum_total_of_work_hours_sum_from_hours_per_project_queryset(
         hours_per_project_queryset: QuerySet) -> timezone.timedelta:
     return hours_per_project_queryset.aggregate(
         Sum("project_hours"))["project_hours__sum"]
Ejemplo n.º 11
0
def get_amount_of(queryset: QuerySet) -> Decimal:
    """ """
    amount = queryset.aggregate(sum=Sum('amount'))

    return amount['sum']