Ejemplo n.º 1
0
def obtain_recipient_totals(recipient_id,
                            children=False,
                            year="latest",
                            subawards=False):
    """ Extract the total amount and transaction count for the recipient_hash given the timeframe

        Args:
            recipient_id: string of hash(duns, name)-[recipient-level]
            children: whether or not to group by children
            year: the year the totals/counts are based on
            subawards: whether to total based on subawards
        Returns:
            list of dictionaries representing hashes and their totals/counts
    """
    if year == "latest" and children is False:
        # Simply pull the total and count from RecipientProfile
        recipient_hash = recipient_id[:-2]
        recipient_level = recipient_id[-1]
        results = list(
            RecipientProfile.objects.filter(
                recipient_hash=recipient_hash,
                recipient_level=recipient_level).annotate(
                    total=F("last_12_months"),
                    count=F("last_12_months_count")).values(
                        "recipient_hash", "recipient_unique_id",
                        "recipient_name", "total", "count"))

    else:
        filters = reshape_filters(recipient_id=recipient_id, year=year)
        queryset, model = recipient_totals(filters)
        if children:
            # Group by the child recipients
            queryset = (queryset.values(
                "recipient_hash", "recipient_unique_id",
                "recipient_name").annotate(
                    total=Sum("generated_pragmatic_obligation"),
                    count=Sum("counts")).values("recipient_hash",
                                                "recipient_unique_id",
                                                "recipient_name", "total",
                                                "count"))
            results = list(queryset)
        else:
            # Calculate the overall totals
            aggregates = queryset.aggregate(
                total=Sum("generated_pragmatic_obligation"),
                count=Sum("counts"))
            aggregates.update({"recipient_hash": recipient_id[:-2]})
            results = [aggregates]
    for result in results:
        result["count"] = result["count"] if result["count"] else 0
        result["total"] = result["total"] if result["total"] else 0
    return results
def obtain_recipient_totals(recipient_id, children=False, year='latest', subawards=False):
    """ Extract the total amount and transaction count for the recipient_hash given the timeframe

        Args:
            recipient_id: string of hash(duns, name)-[recipient-level]
            children: whether or not to group by children
            year: the year the totals/counts are based on
            subawards: whether to total based on subawards
        Returns:
            list of dictionaries representing hashes and their totals/counts
    """
    if year == 'latest' and children is False:
        # Simply pull the total and count from RecipientProfile
        recipient_hash = recipient_id[:-2]
        recipient_level = recipient_id[-1]
        results = list(RecipientProfile.objects.filter(recipient_hash=recipient_hash, recipient_level=recipient_level)
                       .annotate(total=F('last_12_months'), count=F('last_12_months_count'))
                       .values('recipient_hash', 'recipient_unique_id', 'recipient_name', 'total', 'count'))

    else:
        filters = reshape_filters(recipient_id=recipient_id, year=year)
        queryset, model = recipient_totals(filters)
        if children:
            # Group by the child recipients
            queryset = queryset.values('recipient_hash', 'recipient_unique_id', 'recipient_name') \
                .annotate(total=Sum('generated_pragmatic_obligation'), count=Sum('counts')) \
                .values('recipient_hash', 'recipient_unique_id', 'recipient_name', 'total', 'count')
            results = list(queryset)
        else:
            # Calculate the overall totals
            aggregates = queryset.aggregate(total=Sum('generated_pragmatic_obligation'), count=Sum('counts'))
            aggregates.update({'recipient_hash': recipient_id[:-2]})
            results = [aggregates]
    for result in results:
        result['count'] = result['count'] if result['count'] else 0
        result['total'] = result['total'] if result['total'] else 0
    return results