def test_apply_annotation_applies_annotation(self): """ Test apply_annotation applies the annotation given in the constructor """ aggregation = Aggregation( query_param='count', field='count', annotate=Count('id'), ) aggregation.apply_annotation(self.queryset)
def test_extra_filter_is_applied(self): """ Test the extra filter is applied when given in the constructor """ aggregation = Aggregation(query_param='count', field='count', annotate=Count('id'), extra_filter=Q(iati_identifier="test")) queryset = aggregation.apply_extra_filter(self.queryset) self.assertEquals(len(queryset), 1) self.assertEquals(queryset[0], self.activity)
def test_required_params_passes(self): """ Test giving required params constructs succesfully """ aggregation = Aggregation( query_param='count', field='count', annotate=Count('id'), )
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"), ), )
def test_extra_filter_non_Q_object_raises(self): """ The "extra_filter" constructor param must be a django Q object """ # TODO: Should we allow key-value dicts as well? - 2016-04-11 with self.assertRaises(ValueError): aggregation = Aggregation( query_param='count', field='count', annotate=Count('id'), extra_filter={'key': "value"}, )
def test_required_params_throws(self): """ Test errors are thrown when not all required params are set. """ with self.assertRaises(ValueError): aggregation = Aggregation( field='count', annotate=Count('id'), ) with self.assertRaises(ValueError): aggregation = Aggregation( query_param='count', annotate=Count('id'), ) with self.assertRaises(ValueError): aggregation = Aggregation( query_param='count', field='count', )
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"), )
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"), ), )
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")), )
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")), )
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, ), )