Exemple #1
0
    def test_required_params_throws(self):
        """
        Test errors are thrown when not all required params are set.
        """
        with self.assertRaises(ValueError):
            group_by = GroupBy(fields="id", )

        with self.assertRaises(ValueError):
            group_by = GroupBy(query_param="test", )
Exemple #2
0
    def test_serialize_result_expands_key_given_results_array(self):
        """
        When calling serialize_results with an array of results, should
        serialize the result[key] with the given serializer and queryset from
        the constructor and return the resulting array.
        Here {key} is the first field given in the fields tuple in the
        constructor
        """

        class DummySerializer(DynamicFieldsSerializer):
            id = serializers.CharField()
            iati_identifier = serializers.CharField()

        group_by = GroupBy(
            query_param="test",
            fields="key",
            queryset=self.queryset,
            serializer=DummySerializer,
            serializer_main_field='iati_identifier',
            serializer_fk='iati_identifier',
            serializer_fields=['iati_identifier']
        )

        results = [
            {
                "key": "test",
                "count": 12345,
            },
            {
                "key": "test2",
                "count": 67890,
            }
        ]

        rf = RequestFactory()
        request = rf.get('/api/activities/')

        new_results = group_by.serialize_results(results, request)

        self.assertEqual(list(new_results), [
            {
                "key": OrderedDict([
                        ('iati_identifier', 'test'),
                ]),
                "count": 12345,
            },
            {
                "key": OrderedDict([
                    ('iati_identifier', 'test2'),
                ]),
                "count": 67890,
            }
        ])
Exemple #3
0
    def test_get_fields_no_renaming(self):
        """
        Test get_fields() returns original fields if no renaming is done.
        """

        group_by = GroupBy(
            query_param="test",
            fields=("id", "name"),
            queryset=self.queryset,
        )

        self.assertEqual(group_by.get_fields(), group_by.fields)
Exemple #4
0
    def test_get_renamed_fields_no_renaming(self):
        """
        Test when not renaming fields, returns an empty dictionary
        """

        group_by = GroupBy(
            query_param="test",
            fields="id",
            queryset=self.queryset,
        )

        self.assertEquals(bool(group_by.get_renamed_fields()), False)
Exemple #5
0
class ChainAggregations(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:

    - `tier`
    - `reporting_org`

    ## 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` - node count

    ## Request parameters

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

    """

    queryset = ChainNode.objects.all()
    filter_backends = (DjangoFilterBackend, )
    filter_class = ChainNodeFilter

    allowed_aggregations = (Aggregation(
        query_param='count',
        field='count',
        annotate=Count('id', distinct=True),
    ), )

    allowed_groupings = (
        GroupBy(query_param="tier", fields=("tier", )),
        GroupBy(
            query_param="reporting_organisation",
            fields=("activity__reporting_organisations__organisation__\
                     organisation_identifier",
                    "activity__reporting_organisations__organisation__\
                     primary_name"),
            renamed_fields=("reporting_organisation_ref",
                            "reporting_organisation_name"),
        ),
    )
Exemple #6
0
    def test_get_fields_with_renaming(self):
        """
        Test get_fields() returns renamed fields when renaming.
        """

        group_by = GroupBy(
            query_param="test",
            fields=("id", "name"),
            renamed_fields=("id_renamed", "name_renamed"),
            queryset=self.queryset,
        )

        self.assertEqual(group_by.get_fields(), group_by.renamed_fields)
Exemple #7
0
    def test_get_fields_with_partly_renaming(self):
        """
        Test get_fields() returns renamed fields when renaming only part of the fields.
        """

        group_by = GroupBy(
            query_param="test",
            fields=("id", "name"),
            renamed_fields="id_renamed",
            queryset=self.queryset,
        )

        self.assertItemsEqual(group_by.get_fields(), ["id_renamed", "name"])
Exemple #8
0
    def test_serializer_fields_limits_serializer_fields(self):
        """
        When given a dynamic serializer, serializer_fields should determine
        the fields that the serialized field should expose
        """
        class DummySerializer(DynamicFieldsSerializer):
            id = serializers.CharField()
            iati_identifier = serializers.CharField()

        group_by = GroupBy(
            query_param="test",
            fields="key",
            queryset=self.queryset,
            serializer=DummySerializer,
            serializer_main_field='iati_identifier',
            serializer_fk='iati_identifier',
            serializer_fields=['iati_identifier']
        )

        results = [
            {
                "key": "test",
                "count": 12345,
            },
            {
                "key": "test2",
                "count": 67890,
            }
        ]

        rf = RequestFactory()
        request = rf.get('/api/activities/')

        new_results = group_by.serialize_results(results, request)

        self.assertCountEqual(new_results, [
            {
                "key": {
                    "iati_identifier": "test",
                },
                "count": 12345,
            },
            {
                "key": {
                    "iati_identifier": "test2",
                },
                "count": 67890,
            }
        ])
Exemple #9
0
    def test_get_renamed_fields_partly(self):
        """
        Test renaming only a few of the fields also works
        """

        group_by = GroupBy(
            query_param="test",
            fields=("id", "name"),
            renamed_fields="id_renamed",
            queryset=self.queryset,
        )

        renamed_fields = group_by.get_renamed_fields()

        self.assertEqual(len(renamed_fields), 1)
        self.assertCountEqual(renamed_fields.keys(), ["id_renamed"])
Exemple #10
0
    def test_get_renamed_fields_with_renaming(self):
        """
        Test when renaming fields, returns a key-value with renamed-actual
        """

        group_by = GroupBy(
            query_param="test",
            fields="id",
            renamed_fields="id_renamed",
            queryset=self.queryset,
        )

        renamed_fields = group_by.get_renamed_fields()

        self.assertEqual(len(renamed_fields), 1)
        self.assertItemsEqual(renamed_fields.keys(), ["id_renamed"])
Exemple #11
0
    def test_get_renamed_fields_with_multiple_renaming(self):
        """
        Test when renaming multiple fields, returns a key-value with
        renamed-actual
        """

        group_by = GroupBy(
            query_param="test",
            fields=("id", "name"),
            renamed_fields=("id_renamed", "name_renamed"),
            queryset=self.queryset,
        )

        renamed_fields = group_by.get_renamed_fields()

        self.assertEqual(len(renamed_fields), 2)
        self.assertCountEqual(renamed_fields.keys(), [
                              "id_renamed", "name_renamed"])
Exemple #12
0
    def test_required_params_passes(self):
        """
        Test giving required params constructs succesfully
        """

        group_by = GroupBy(
            query_param="test",
            fields="id",
            queryset=self.queryset,
        )
Exemple #13
0
 def test_renamed_fields_exceeds_fields_len_raises(self):
     """
     Test when len(renamed_fields) > len(fields), throws error
     """
     with self.assertRaises(ValueError):
         group_by = GroupBy(
             query_param="test",
             fields=("id", "name"),
             renamed_fields=("id_renamed", "name_renamed", "ew_renamed"),
             queryset=self.queryset,
         )
Exemple #14
0
class ResultAggregations(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:

    - `result_indicator_title`
    - `result_title`


    ## Aggregation options

    API request has to include `aggregations` parameter.

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

    - `target` Indicator period target. Currently breaks on non number results.
    - `actual` Indicator period actual. Currently breaks on non number results.

    ## Request parameters

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

    """

    queryset = Result.objects.all()
    filter_backends = (
        SearchFilter,
        DjangoFilterBackend,
    )
    filter_class = ResultFilter

    allowed_aggregations = (
        Aggregation(
            query_param='targets',
            field='targets',
            annotate=Sum(
                Func(
                    F('resultindicator__resultindicatorperiod__targets__value'
                      ),
                    function='CAST',
                    template='%(function)s(%(expressions)s as double precision)'
                )),
        ),
        Aggregation(
            query_param='actuals',
            field='actuals',
            annotate=Sum(
                Func(
                    F('resultindicator__resultindicatorperiod__actuals__value'
                      ),
                    function='CAST',
                    template='%(function)s(%(expressions)s as double precision)'
                )),
        ),
        Aggregation(
            query_param='activity_count',
            field='activity_count',
            annotate=Count('activity', distinct=True),
        ),
    )

    allowed_groupings = (
        GroupBy(query_param="result_indicator_title",
                fields=("resultindicator__resultindicatortitle__primary_name"),
                renamed_fields="result_indicator_title"),
        GroupBy(query_param="result_title",
                fields=("resulttitle__narratives__content"),
                renamed_fields="result_title"),
    )
Exemple #15
0
class DatasetAggregations(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:

    - `dataset`
    - `publisher`
    - `exception_type`
    - `field`
    - `model`
    - `message`
    

    ## Aggregation options

    API request has to include `aggregations` parameter.
    
    This parameter controls result aggregations and
    can be one or more (comma separated values) of:

    - `note_count` count the amount of notes

    ## Request parameters

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

    """

    queryset = Dataset.objects.all()

    filter_backends = ( DjangoFilterBackend,)
    filter_class = DatasetFilter
    
    allowed_aggregations = (
        Aggregation(
            query_param='note_count',
            field='note_count',
            annotate=Count('datasetnote__id'),
        ),
    )

    allowed_groupings = (
        GroupBy(
            query_param="dataset",
            fields=("id"),
            renamed_fields="dataset",
            queryset=Dataset.objects.all(),
            serializer=SimpleDatasetSerializer,
            serializer_main_field='id'
        ),
        GroupBy(
            query_param="publisher",
            fields=("publisher__id"),
            renamed_fields="_publisher",
            queryset=Publisher.objects.all(),
            serializer=SimplePublisherSerializer,
            serializer_main_field='id'
        ),
        GroupBy(
            query_param="exception_type",
            fields=("datasetnote__exception_type"),
            renamed_fields="exception_type",
        ),
        GroupBy(
            query_param="field",
            fields=("datasetnote__field", "datasetnote__exception_type"),
            renamed_fields=("field", "exception_type"),
        ),
        GroupBy(
            query_param="model",
            fields=("datasetnote__field", "datasetnote__model", "datasetnote__exception_type"),
            renamed_fields=("field", "model", "exception_type"),
        ),
        GroupBy(
            query_param="message",
            fields=("datasetnote__message", "datasetnote__field", "datasetnote__model", "datasetnote__exception_type"),
            renamed_fields=("message", "field", "model", "exception_type"),
        ),
    )
Exemple #16
0
class TransactionAggregation(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`
    - `recipient_region`
    - `sector`
    - `related_activity`
    - `transaction_type`
    - `reporting_organisation`
    - `participating_organisation`
    - `receiver_org`
    - `provider_org`
    - `document_link_category`
    - `activity_status`
    - `participating_organisation_type`
    - `collaboration_type`
    - `default_flow_type`
    - `default_finance_type`
    - `default_aid_type`
    - `default_tied_status`
    - `transaction_date_month`
    - `transaction_date_quarter`
    - `transaction_date_year`

    ## 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`
    - `activity_count`
    - `value`
    - `disbursement`
    - `expenditure`
    - `commitment`
    - `incoming_fund`
    - `incoming_commitment`
    - `disbursement_expenditure` Disbursement and expenditure summed togehther


    ## Currency options

    By default the values returned by the aggregations are in the reported
    currency. This only renders meaningful results when all values were in the
    same currency. Which is only the case when you filter your results down.

    The aggregation endpoints have the ability to return values in a currency.
    Options for this `convert_to` parameter are:

    - `xdr`
    - `usd`
    - `eur`
    - `gbp`
    - `jpy`
    - `cad`

    This results in converted values when the original value was in another
    currency.

    Information on used exchange rates can be found
    <a href='https://docs.oipa.nl/'>in the docs</a>.


    ## Request parameters

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

    """

    queryset = Transaction.objects.all()

    filter_backends = (
        SearchFilter,
        DjangoFilterBackend,
    )
    filter_class = TransactionAggregationFilter

    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,
                            ),
                            Aggregation(
                                query_param='incoming_fund',
                                field='incoming_fund',
                                annotate=annotate_currency,
                                extra_filter=Q(transaction_type=1),
                            ),
                            Aggregation(
                                query_param='commitment',
                                field='commitment',
                                annotate=annotate_currency,
                                extra_filter=Q(transaction_type=2),
                            ),
                            Aggregation(
                                query_param='disbursement',
                                field='disbursement',
                                annotate=annotate_currency,
                                extra_filter=Q(transaction_type=3),
                            ),
                            Aggregation(
                                query_param='expenditure',
                                field='expenditure',
                                annotate=annotate_currency,
                                extra_filter=Q(transaction_type=4),
                            ),
                            Aggregation(
                                query_param='incoming_commitment',
                                field='incoming_commitment',
                                annotate=annotate_currency,
                                extra_filter=Q(transaction_type=11),
                            ),
                            Aggregation(
                                query_param='disbursement_expenditure',
                                field='disbursement_expenditure',
                                annotate=annotate_currency,
                                extra_filter=Q(transaction_type__in=[3, 4]),
                            ))

    allowed_groupings = (
        GroupBy(
            query_param="recipient_country",
            fields="transactionrecipientcountry__country",
            renamed_fields="recipient_country",
            queryset=Country.objects.all(),
            serializer=CountrySerializer,
            serializer_fields=('url', 'code', 'name', 'location', 'region'),
            name_search_field='transactionrecipientcountry__country__name',
            renamed_name_search_field='recipient_country_name',
        ),
        GroupBy(
            query_param="recipient_region",
            fields="transactionrecipientregion__region",
            renamed_fields="recipient_region",
            queryset=Region.objects.all(),
            serializer=RegionSerializer,
            serializer_fields=('url', 'code', 'name', 'location'),
            name_search_field="transactionrecipientregion__region__name",
            renamed_name_search_field="recipient_region_name",
        ),
        GroupBy(
            query_param="sector",
            fields="transactionsector__sector",
            renamed_fields="sector",
            queryset=Sector.objects.all(),
            serializer=SectorSerializer,
            serializer_fields=('url', 'code', 'name', 'location'),
            name_search_field="transactionsector__sector__name",
            renamed_name_search_field="sector_name",
        ),
        GroupBy(
            query_param="related_activity",
            fields=(
                "activity__relatedactivity__ref_activity__iati_identifier"),
            renamed_fields="related_activity",
        ),
        GroupBy(
            query_param="transaction_type",
            fields=("transaction_type"),
            queryset=TransactionType.objects.all(),
            serializer=CodelistSerializer,
        ),
        GroupBy(query_param="reporting_organisation",
                fields="activity__reporting_organisations__organisation__id",
                renamed_fields="reporting_organisation",
                queryset=Organisation.objects.all(),
                serializer=OrganisationSerializer,
                serializer_main_field='id',
                name_search_field="activity__reporting_organisations__\
                    organisation__primary_name",
                renamed_name_search_field="reporting_organisation_name"),
        GroupBy(
            query_param="participating_organisation",
            fields=("activity__participating_organisations__primary_name",
                    "activity__participating_organisations__normalized_ref"),
            renamed_fields=("participating_organisation",
                            "participating_organisation_ref"),
            queryset=ActivityParticipatingOrganisation.objects.all(),
            name_search_field="activity__participating_organisations\
                    __primary_name",
            renamed_name_search_field="participating_organisation_name"),
        GroupBy(query_param="provider_org",
                fields=("provider_organisation__primary_name"),
                renamed_fields="provider_org",
                name_search_field="provider_organisation__primary_name",
                renamed_name_search_field="provider_org_name"),
        GroupBy(query_param="receiver_org",
                fields=("receiver_organisation__primary_name"),
                renamed_fields="receiver_org",
                name_search_field="receiver_organisation__primary_name",
                renamed_name_search_field="receiver_org_name"),
        GroupBy(query_param="document_link_category",
                fields="activity__documentlink__categories__code",
                renamed_fields="document_link_category",
                queryset=DocumentCategory.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__documentlink__categories__name",
                renamed_name_search_field="document_link_category_name"),
        GroupBy(query_param="activity_status",
                fields="activity__activity_status",
                renamed_fields="activity_status",
                queryset=ActivityStatus.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__activity_status__name",
                renamed_name_search_field="activity_status_name"),
        GroupBy(query_param="participating_organisation_type",
                fields="activity__participating_organisations__type",
                renamed_fields="participating_organisation_type",
                queryset=OrganisationType.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__participating_organisations__type\
                    __name",
                renamed_name_search_field="participating_organisations_type\
                    _name"),
        GroupBy(query_param="collaboration_type",
                fields="activity__collaboration_type",
                renamed_fields="collaboration_type",
                queryset=CollaborationType.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__collaboration_type__name",
                renamed_name_search_field="collaboration_type_name"),
        GroupBy(
            query_param="default_flow_type",
            fields="activity__default_flow_type",
            renamed_fields="default_flow_type",
            queryset=FlowType.objects.all(),
            serializer=CodelistSerializer,
        ),
        GroupBy(
            query_param="default_finance_type",
            fields="activity__default_finance_type",
            renamed_fields="default_finance_type",
            queryset=FinanceType.objects.all(),
            serializer=CodelistSerializer,
        ),
        GroupBy(
            query_param="default_aid_type",
            fields="activity__default_aid_type",
            renamed_fields="default_aid_type",
            queryset=AidType.objects.all(),
            serializer=CodelistSerializer,
        ),
        GroupBy(
            query_param="default_tied_status",
            fields="activity__default_tied_status",
            renamed_fields="default_tied_status",
            queryset=TiedStatus.objects.all(),
            serializer=CodelistSerializer,
        ),
        GroupBy(
            query_param="policy_marker_significance",
            fields="activity__activitypolicymarker__significance",
            renamed_fields="significance",
            queryset=PolicySignificance.objects.all(),
            serializer=CodelistSerializer,
        ),
        GroupBy(
            query_param="transaction_date_year",
            extra={
                'select': {
                    'transaction_date_year':
                    'EXTRACT(\
                            YEAR FROM "transaction_date")::integer',
                },
                'where': [
                    'EXTRACT(\
                        YEAR FROM "transaction_date")::integer IS NOT NULL',
                ],
            },
            fields="transaction_date_year",
        ),
        GroupBy(query_param="transaction_date_quarter",
                extra={
                    'select': {
                        'transaction_date_year':
                        'EXTRACT(YEAR \
                            FROM "transaction_date")::integer',
                        'transaction_date_quarter':
                        'EXTRACT(QUARTER FROM \
                            "transaction_date")::integer',
                    },
                    'where': [
                        'EXTRACT(YEAR FROM "transaction_date")::integer \
                            IS NOT NULL',
                        'EXTRACT(QUARTER FROM "transaction_date")::integer \
                            IS NOT NULL',
                    ],
                },
                fields=("transaction_date_year", "transaction_date_quarter")),
        GroupBy(query_param="transaction_date_month",
                extra={
                    'select': {
                        'transaction_date_year':
                        'EXTRACT(YEAR \
                            FROM "transaction_date")::integer',
                        'transaction_date_month':
                        'EXTRACT(MONTH \
                            FROM "transaction_date")::integer',
                    },
                    'where': [
                        'EXTRACT(YEAR FROM "transaction_date")::integer \
                            IS NOT NULL',
                        'EXTRACT(MONTH FROM "transaction_date")::integer \
                            IS NOT NULL',
                    ],
                },
                fields=("transaction_date_year", "transaction_date_month")),
    )
Exemple #17
0
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`

    ## 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'),
            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(
            query_param="related_activity",
            fields=(
                "activity__relatedactivity__ref_activity__iati_identifier"),
            renamed_fields="related_activity",
        ),
        GroupBy(
            query_param="reporting_organisation",
            fields="activity__reporting_organisations__organisation__id",
            renamed_fields="reporting_organisation",
            queryset=Organisation.objects.all(),
            serializer=OrganisationSerializer,
            serializer_main_field='id',
            name_search_field=
            "activity__reporting_organisations__organisation__primary_name",
            renamed_name_search_field="reporting_organisation_name"),
        GroupBy(
            query_param="participating_organisation",
            fields="activity__participating_organisations__primary_name",
            renamed_fields="participating_organisation",
            queryset=ActivityParticipatingOrganisation.objects.all(),
            # serializer=OrganisationSerializer,
            name_search_field=
            "activity__participating_organisations__primary_name",
            renamed_name_search_field="participating_organisation_name"),
        GroupBy(
            query_param="participating_organisation_type",
            fields="activity__participating_organisations__type",
            renamed_fields="participating_organisation_type",
            queryset=OrganisationType.objects.all(),
            serializer=CodelistSerializer,
            name_search_field=
            "activity__participating_organisations__type__name",
            renamed_name_search_field="participating_organisations_type_name"),
        GroupBy(query_param="document_link_category",
                fields="activity__documentlink__categories__code",
                renamed_fields="document_link_category",
                queryset=DocumentCategory.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__documentlink__categories__name",
                renamed_name_search_field="document_link_category_name"),
        GroupBy(query_param="activity_status",
                fields="activity__activity_status",
                renamed_fields="activity_status",
                queryset=ActivityStatus.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__activity_status__name",
                renamed_name_search_field="activity_status_name"),
        GroupBy(query_param="collaboration_type",
                fields="activity__collaboration_type",
                renamed_fields="collaboration_type",
                queryset=CollaborationType.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity__collaboration_type__name",
                renamed_name_search_field="collaboration_type_name"),
        GroupBy(
            query_param="budget_period_start_year",
            extra={
                'select': {
                    'budget_period_start_year':
                    'EXTRACT(YEAR FROM "period_start")::integer',
                },
                'where': [
                    'EXTRACT(YEAR FROM "period_start")::integer IS NOT NULL',
                ],
            },
            fields="budget_period_start_year",
        ),
        GroupBy(
            query_param="budget_period_end_year",
            extra={
                'select': {
                    'budget_period_end_year':
                    'EXTRACT(YEAR FROM "period_end")::integer',
                },
                'where': [
                    'EXTRACT(YEAR FROM "period_end")::integer IS NOT NULL',
                ],
            },
            fields="budget_period_end_year",
        ),
        GroupBy(
            query_param="budget_period_start_quarter",
            extra={
                'select': {
                    'budget_period_start_year':
                    'EXTRACT(YEAR FROM "period_start")::integer',
                    'budget_period_start_quarter':
                    'EXTRACT(QUARTER FROM "period_start")::integer',
                },
                'where': [
                    'EXTRACT(YEAR FROM "period_start")::integer IS NOT NULL',
                    'EXTRACT(QUARTER FROM "period_start")::integer IS NOT NULL',
                ],
            },
            fields=("budget_period_start_year",
                    "budget_period_start_quarter")),
        GroupBy(
            query_param="budget_period_end_quarter",
            extra={
                'select': {
                    'budget_period_end_year':
                    'EXTRACT(YEAR FROM "period_end")::integer',
                    'budget_period_end_quarter':
                    'EXTRACT(QUARTER FROM "period_end")::integer',
                },
                'where': [
                    'EXTRACT(YEAR FROM "period_end")::integer IS NOT NULL',
                    'EXTRACT(QUARTER FROM "period_end")::integer IS NOT NULL',
                ],
            },
            fields=("budget_period_end_year", "budget_period_end_quarter")),
        GroupBy(
            query_param="budget_period_start_month",
            extra={
                'select': {
                    'budget_period_start_year':
                    'EXTRACT(YEAR FROM "period_start")::integer',
                    'budget_period_start_month':
                    'EXTRACT(MONTH FROM "period_start")::integer',
                },
                'where': [
                    'EXTRACT(YEAR FROM "period_start")::integer IS NOT NULL',
                    'EXTRACT(MONTH FROM "period_start")::integer IS NOT NULL',
                ],
            },
            fields=("budget_period_start_year", "budget_period_start_month")),
        GroupBy(
            query_param="budget_period_end_month",
            extra={
                'select': {
                    'budget_period_end_yer':
                    'EXTRACT(YEAR FROM "period_end")::integer',
                    'budget_period_end_month':
                    'EXTRACT(MONTH FROM "period_end")::integer',
                },
                'where': [
                    'EXTRACT(YEAR FROM "period_end")::integer IS NOT NULL',
                    'EXTRACT(MONTH FROM "period_end")::integer IS NOT NULL',
                ],
            },
            fields=("budget_period_end_year", "budget_period_end_month")),
    )
Exemple #18
0
class ActivityAggregations(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`
    - `recipient_region`
    - `sector`
    - `related_activity`
    - `reporting_organisation`
    - `participating_organisation`
    - `participating_organisation_type`
    - `document_link_category`
    - `activity_status`
    - `collaboration_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_distinct`

    ## Request parameters

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

    """

    queryset = Activity.objects.all()

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

    allowed_aggregations = (
        Aggregation(
            query_param='count',
            field='count',
            annotate=Count('id'),
        ),
        Aggregation(
            query_param='count_distinct',
            field='count',
            annotate=Count('id', distinct=True),
        ),
    )

    allowed_groupings = (
        GroupBy(
            query_param="recipient_country",
            fields="recipient_country",
            queryset=Country.objects.all(),
            serializer=CountrySerializer,
            serializer_fields=('url', 'code', 'name', 'location'),
            name_search_field='recipient_country__name',
            renamed_name_search_field='recipient_country_name',
        ),
        GroupBy(
            query_param="recipient_region",
            fields="recipient_region",
            queryset=Region.objects.all(),
            serializer=RegionSerializer,
            serializer_fields=('url', 'code', 'name', 'location'),
            name_search_field="recipient_region__name",
            renamed_name_search_field="recipient_region_name",
        ),
        GroupBy(
            query_param="sector",
            fields="sector",
            queryset=Sector.objects.all(),
            serializer=SectorSerializer,
            serializer_fields=('url', 'code', 'name', 'location'),
            name_search_field="sector__name",
            renamed_name_search_field="sector_name",
        ),
        GroupBy(
            query_param="related_activity",
            fields=("relatedactivity__ref_activity__id"),
            renamed_fields="related_activity",
        ),
        GroupBy(query_param="reporting_organisation",
                fields="reporting_organisations__normalized_ref",
                renamed_fields="reporting_organisation",
                queryset=Organisation.objects.all(),
                serializer=OrganisationSerializer,
                serializer_main_field='organisation_identifier',
                name_search_field=
                "reporting_organisations__organisation__primary_name",
                renamed_name_search_field="reporting_organisation_name"),
        GroupBy(query_param="participating_organisation",
                fields=("participating_organisations__primary_name",
                        "participating_organisations__normalized_ref"),
                renamed_fields=("participating_organisation",
                                "participating_organisation_ref"),
                queryset=ActivityParticipatingOrganisation.objects.all(),
                name_search_field="participating_organisations__primary_name",
                renamed_name_search_field="participating_organisation_name"),
        GroupBy(
            query_param="participating_organisation_type",
            fields="participating_organisations__type",
            renamed_fields="participating_organisation_type",
            queryset=OrganisationType.objects.all(),
            serializer=CodelistSerializer,
            name_search_field="participating_organisations__type__name",
            renamed_name_search_field="participating_organisations_type_name"),
        GroupBy(query_param="document_link_category",
                fields="documentlink__categories__code",
                renamed_fields="document_link_category",
                queryset=DocumentCategory.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="documentlink__categories__name",
                renamed_name_search_field="document_link_category_name"),
        GroupBy(query_param="activity_status",
                fields="activity_status",
                queryset=ActivityStatus.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="activity_status__name",
                renamed_name_search_field="activity_status_name"),
        GroupBy(query_param="collaboration_type",
                fields="collaboration_type",
                renamed_fields="collaboration_type",
                queryset=CollaborationType.objects.all(),
                serializer=CodelistSerializer,
                name_search_field="collaboration_type__name",
                renamed_name_search_field="collaboration_type_name"),
        GroupBy(
            query_param="policy_marker_significance",
            fields="activitypolicymarker__significance",
            renamed_fields="significance",
            queryset=PolicySignificance.objects.all(),
            serializer=CodelistSerializer,
        ),
    )