<commit_before>from django.conf import settings
from django.db.models import Count, F, Sum
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django_filters.rest_framework import DjangoFilterBackend

from api.activity.serializers import CodelistSerializer
from api.aggregation.views import Aggregation, AggregationView, GroupBy
from api.budget import filters
from api.budget.filters import RelatedOrderingFilter
from api.budget.serializers import BudgetSerializer
from api.country.serializers import CountrySerializer
from api.generics.filters import SearchFilter
from api.generics.views import DynamicListView
from api.organisation.serializers import OrganisationSerializer
from api.region.serializers import RegionSerializer
from api.sector.serializers import SectorSerializer
from geodata.models import Country, Region
from iati.models import (
    ActivityParticipatingOrganisation, ActivityStatus, Budget,
    CollaborationType, DocumentCategory, Organisation, OrganisationType,
    Sector
)
from iati_codelists.models import BudgetType

# These are the accepted currencies
currencies = [
    'xdr',
    'usd',
    'eur',
    'gbp',
    'jpy',
    'cad'
]


def annotate_currency(query_params, groupings):
    """
    Choose the right currency field,
    and aggregate differently based on group_by
    """
    currency = query_params.get('convert_to')

    if currency:
        currency = currency.lower()

    if currency is None or currency not in currencies:
        currency_field = 'value'
    else:
        currency_field = currency + '_value'

    annotation_components = F(currency_field)

    param_additions = []

    for param in query_params:
        if param == 'sector':
            param_additions.append('budgetsector__percentage')

    grouping_additions = []

    for grouping in groupings:
        if grouping.query_param == 'sector':
            grouping_additions.append('budgetsector__percentage')

    additions = list(set(param_additions).union(grouping_additions))

    for percentage_field in additions:
        percentage_expression = F(percentage_field) / 100.0
        annotation_components = annotation_components * percentage_expression

    return Sum(annotation_components)


class BudgetAggregations(AggregationView):
    """
    Returns aggregations based on the item grouped by,
    and the selected aggregation.

    ## Group by options

    API request has to include `group_by` parameter.

    This parameter controls result aggregations and
    can be one or more (comma separated values) of:

    - `recipient_country` Non percentage weighted
    - `recipient_region` Non percentage weighted
    - `sector` Percentage weighted
    - `related_activity`
    - `reporting_organisation`
    - `participating_organisation`
    - `participating_organisation_type`
    - `document_link_category`
    - `activity_status`
    - `collaboration_type`
    - `budget_period_start_year`
    - `budget_period_end_year`
    - `budget_period_start_quarter`
    - `budget_period_end_quarter`
    - `budget_period_start_month`
    - `budget_period_end_month`
    - `budget_type`

    ## Aggregation options

    API request has to include `aggregations` parameter.

    This parameter controls result aggregations and
    can be one or more (comma separated values) of:

    - `count` Count of budgets
    - `activity_count` Count of activities
    - `value` Sum of budget value (in the selected currency)

    ## Request parameters

    All filters available on the Activity List, can be used on aggregations.

    """

    queryset = Budget.objects.all()

    filter_backends = (SearchFilter, DjangoFilterBackend,)
    filter_class = filters.BudgetFilter

    allowed_aggregations = (
        Aggregation(
            query_param='count',
            field='count',
            annotate=Count('id'),
        ),
        Aggregation(
            query_param='activity_count',
            field='activity_count',
            annotate=Count('activity', distinct=True),
        ),
        Aggregation(
            query_param='value',
            field='value',
            annotate=annotate_currency,
        ),
    )

    allowed_groupings = (
        GroupBy(
            query_param="recipient_country",
            fields="activity__recipient_country",
            renamed_fields="recipient_country",
            queryset=Country.objects.all(),
            serializer=CountrySerializer,
            serializer_fields=('url', 'code', 'name', 'location', 'region'),
            name_search_field='activity__recipient_country__name',
            renamed_name_search_field='recipient_country_name',
        ),
        GroupBy(
            query_param="recipient_region",
            fields="activity__recipient_region",
            renamed_fields="recipient_region",
            queryset=Region.objects.all(),
            serializer=RegionSerializer,
            serializer_fields=('url', 'code', 'name', 'location'),
            name_search_field="activity__recipient_region__name",
            renamed_name_search_field="recipient_region_name",
        ),
        GroupBy(
            query_param="sector",
            fields="budgetsector__sector",
            renamed_fields="sector",
            queryset=Sector.objects.all(),
            serializer=SectorSerializer,
            serializer_fields=('url', 'code', 'name'),
            name_search_field="budgetsector__sector__name",
            renamed_name_search_field="sector_name",
        ),
        GroupBy(<commit_msg>delete location from group_by 'recipient_region'.<commit_after>from django.conf import settings
from django.db.models import Count, F, Sum
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django_filters.rest_framework import DjangoFilterBackend

from api.activity.serializers import CodelistSerializer
from api.aggregation.views import Aggregation, AggregationView, GroupBy
from api.budget import filters
from api.budget.filters import RelatedOrderingFilter
from api.budget.serializers import BudgetSerializer
from api.country.serializers import CountrySerializer
from api.generics.filters import SearchFilter
from api.generics.views import DynamicListView
from api.organisation.serializers import OrganisationSerializer
from api.region.serializers import RegionSerializer
from api.sector.serializers import SectorSerializer
from geodata.models import Country, Region
from iati.models import (
    ActivityParticipatingOrganisation, ActivityStatus, Budget,
    CollaborationType, DocumentCategory, Organisation, OrganisationType,
    Sector
)
from iati_codelists.models import BudgetType

# These are the accepted currencies
currencies = [
    'xdr',
    'usd',
    'eur',
    'gbp',
    'jpy',
    'cad'
]


def annotate_currency(query_params, groupings):
    """
    Choose the right currency field,
    and aggregate differently based on group_by
    """
    currency = query_params.get('convert_to')

    if currency:
        currency = currency.lower()

    if currency is None or currency not in currencies:
        currency_field = 'value'
    else:
        currency_field = currency + '_value'

    annotation_components = F(currency_field)

    param_additions = []

    for param in query_params:
        if param == 'sector':
            param_additions.append('budgetsector__percentage')

    grouping_additions = []

    for grouping in groupings:
        if grouping.query_param == 'sector':
            grouping_additions.append('budgetsector__percentage')

    additions = list(set(param_additions).union(grouping_additions))

    for percentage_field in additions:
        percentage_expression = F(percentage_field) / 100.0
        annotation_components = annotation_components * percentage_expression

    return Sum(annotation_components)


class BudgetAggregations(AggregationView):
    """
    Returns aggregations based on the item grouped by,
    and the selected aggregation.

    ## Group by options

    API request has to include `group_by` parameter.

    This parameter controls result aggregations and
    can be one or more (comma separated values) of:

    - `recipient_country` Non percentage weighted
    - `recipient_region` Non percentage weighted
    - `sector` Percentage weighted
    - `related_activity`
    - `reporting_organisation`
    - `participating_organisation`
    - `participating_organisation_type`
    - `document_link_category`
    - `activity_status`
    - `collaboration_type`
    - `budget_period_start_year`
    - `budget_period_end_year`
    - `budget_period_start_quarter`
    - `budget_period_end_quarter`
    - `budget_period_start_month`
    - `budget_period_end_month`
    - `budget_type`

    ## Aggregation options

    API request has to include `aggregations` parameter.

    This parameter controls result aggregations and
    can be one or more (comma separated values) of:

    - `count` Count of budgets
    - `activity_count` Count of activities
    - `value` Sum of budget value (in the selected currency)

    ## Request parameters

    All filters available on the Activity List, can be used on aggregations.

    """

    queryset = Budget.objects.all()

    filter_backends = (SearchFilter, DjangoFilterBackend,)
    filter_class = filters.BudgetFilter

    allowed_aggregations = (
        Aggregation(
            query_param='count',
            field='count',
            annotate=Count('id'),
        ),
        Aggregation(
            query_param='activity_count',
            field='activity_count',
            annotate=Count('activity', distinct=True),
        ),
        Aggregation(
            query_param='value',
            field='value',
            annotate=annotate_currency,
        ),
    )

    allowed_groupings = (
        GroupBy(
            query_param="recipient_country",
            fields="activity__recipient_country",
            renamed_fields="recipient_country",
            queryset=Country.objects.all(),
            serializer=CountrySerializer,
            serializer_fields=('url', 'code', 'name', 'location', 'region'),
            name_search_field='activity__recipient_country__name',
            renamed_name_search_field='recipient_country_name',
        ),
        GroupBy(
            query_param="recipient_region",
            fields="activity__recipient_region",
            renamed_fields="recipient_region",
            queryset=Region.objects.all(),
            serializer=RegionSerializer,
            serializer_fields=('url', 'code', 'name',),
            name_search_field="activity__recipient_region__name",
            renamed_name_search_field="recipient_region_name",
        ),
        GroupBy(
            query_param="sector",
            fields="budgetsector__sector",
            renamed_fields="sector",
            queryset=Sector.objects.all(),
            serializer=SectorSerializer,
            serializer_fields=('url', 'code', 'name'),
            name_search_field="budgetsector__sector__name",
            renamed_name_search_field="sector_name",
        ),
        GroupBy(
{'fraction_non_alphanumeric': 0.08075647575513131, 'fraction_numerical': 0.0007170386304562158, 'mean_word_length': 3.1097605893186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '480685', 'n_tokens_mistral': 3117, 'n_tokens_neox': 2916, 'n_words': 696}