def test_prefetch_policy_markers(self): """ Test if the prefetches are applied correctly Here we expect 4 queries: 1. Fetch Activity objects 2. Fetch ActivityPolicyMarker objects 3. Fetch content_type objects 4. Fetch narrative objects """ with self.assertNumQueries(4): queryset = Activity.objects.all().prefetch_policy_markers() serializer = ActivitySerializer( queryset, many=True, context={'request': self.request_dummy}, fields=('policy_markers', )) list(serializer.data)
def test_prefetch_reporting_organisations(self): """ Test if the prefetches are applied correctly Here we expect 3 queries: 1. Fetch Activity objects 2. Fetch ReportingOrganisation objects 3. Fetch corresponding narratives """ with self.assertNumQueries(2): queryset = Activity.objects.all()\ .prefetch_reporting_organisations() serializer = ActivitySerializer( queryset, many=True, context={'request': self.request_dummy}, fields=('reporting_organisations', )) list(serializer.data)
class TransactionSerializer(DynamicFieldsModelSerializer): """ Transaction serializer class """ url = serializers.HyperlinkedIdentityField( view_name='transactions:transaction-detail', lookup_field='pk') activity = ActivitySerializer(read_only=True, fields=('id', 'url')) aid_type = CodelistSerializer() finance_type = CodelistSerializer() flow_type = CodelistSerializer() provider_organisation = TransactionProviderSerializer() receiver_organisation = TransactionReceiverSerializer() tied_status = CodelistSerializer() transaction_type = CodelistSerializer() currency = CodelistSerializer() description = TransactionDescriptionSerializer() humanitarian = serializers.BooleanField() class Meta: model = models.Transaction fields = ( 'ref', 'humanitarian', 'url', 'activity', 'aid_type', 'description', 'disbursement_channel', 'finance_type', 'flow_type', 'provider_organisation', 'receiver_organisation', 'tied_status', 'transaction_date', 'transaction_type', 'value_date', 'value', 'currency', )
def test_prefetch_contact_info(self): """ Test if the prefetches are applied correctly Here we expect 2 queries: 1. Fetch Activity objects 2. Fetch ContactInfo objects 3. Fetch organisation__narratives objects 4. Fetch department__narratives objects 5. Fetch person_name__narratives objects 6. Fetch job_title__narratives objects 7. Fetch mailing_address__narratives objects """ with self.assertNumQueries(7): queryset = Activity.objects.all().prefetch_contact_info() serializer = ActivitySerializer( queryset, many=True, context={'request': self.request_dummy}, fields=('contact_info',)) list(serializer.data)
def test_prefetch_document_links(self): """ Test if the prefetches are applied correctly Here we expect 7 queries: 1. Fetch Activity objects. 2. Fetch DocumentLink objects. 3. Fetch DocumentLinkLanguage objects. 4. Fetch DocumentLinkCategory objects. 5. Fetch Narrative objects. 6. Fetch DocumentLinkCategory objects. 7. Fetch DocumentLinkLanguage objects. TODO: Verify if the queries 6 and 7 can be deleted. """ with self.assertNumQueries(7): queryset = Activity.objects.all().prefetch_document_links() serializer = ActivitySerializer( queryset, many=True, context={'request': self.request_dummy}, fields=('document_links',)) list(serializer.data)
def test_prefetch_locations(self): """ Test if the prefetches are applied correctly Here we expect 2 queries: 1. Fetch Activity objects 2. Fetch Location objects 6. Fetch LocationAdministrative objects 3. Fetch LocationNameNarrative objects 4. Fetch LocationDescriptionNarrative objects 5. Fetch LocationActivityDescriptionNarrative objects TODO: Reduce queries number 09-01-2017 """ # TODO: should be 6 queries, LocationAdministrative gets duplicated - 2017-03-27 with self.assertNumQueries(7): queryset = Activity.objects.all().prefetch_locations() serializer = ActivitySerializer( queryset, many=True, context={'request': self.request_dummy}, fields=('locations', )) list(serializer.data)
def test_prefetch_country_budget_items(self): """ Test if the prefetches are applied correctly Here we expect 2 queries: 1. Fetch Activity objects 2. Fetch CountryBudgetItem objects 4. vocabulary 3. Fetch BudgetItem object 4. code 5. narratives ... etc. 17. .... """ # TODO: write above with 17 objects should be on this prefetch with self.assertNumQueries(17): queryset = Activity.objects.all().prefetch_country_budget_items() serializer = ActivitySerializer( queryset, many=True, context={'request': self.request_dummy}, fields=('country_budget_items',)) list(serializer.data)
class TransactionSerializer(DynamicFieldsModelSerializer): """ Transaction serializer class """ url = serializers.HyperlinkedIdentityField( view_name='transactions:transaction-detail', lookup_field='pk', read_only=True ) transaction_date = serializers.CharField() value_date = serializers.CharField() aid_type = CodelistSerializer() disbursement_channel = CodelistSerializer() finance_type = CodelistSerializer() flow_type = CodelistSerializer() provider_organisation = TransactionProviderSerializer(required=False) receiver_organisation = TransactionReceiverSerializer(required=False) disbursement_channel = CodelistSerializer() sector = TransactionSectorSerializer(required=False, source="transaction_sector") recipient_country = TransactionRecipientCountrySerializer( required=False, source="transaction_recipient_country") recipient_region = TransactionRecipientRegionSerializer( required=False, source="transaction_recipient_region") tied_status = CodelistSerializer() transaction_type = CodelistSerializer() currency = CodelistSerializer() description = TransactionDescriptionSerializer() humanitarian = serializers.BooleanField() activity = ActivitySerializer(read_only=True, fields=('id', 'iati_identifier', 'url', 'title')) activity_id = serializers.CharField(write_only=True) class Meta: model = models.Transaction fields = ( 'id', 'activity', 'activity_id', 'url', 'ref', 'humanitarian', 'transaction_type', 'transaction_date', 'value', 'value_date', 'currency', 'description', 'provider_organisation', 'receiver_organisation', 'disbursement_channel', 'sector', 'recipient_country', 'recipient_region', 'flow_type', 'finance_type', 'aid_type', 'tied_status', ) def validate(self, data): activity = get_or_raise(iati_models.Activity, data, 'activity_id') validated = validators.activity_transaction( activity, data.get('ref'), data.get('humanitarian'), data.get('transaction_type', {}).get('code'), data.get('transaction_date'), data.get('value'), data.get('value_date'), data.get('currency', {}).get('code'), data.get('description', {}).get('narratives'), data.get('provider_organisation', {}).get('ref'), data.get('provider_organisation', {}).get('provider_activity_ref'), data.get('provider_organisation', {}).get('type', {}).get('code'), data.get('provider_organisation', {}).get('narratives'), data.get('receiver_organisation', {}).get('ref'), data.get('receiver_organisation', {}).get('receiver_activity_ref'), data.get('receiver_organisation', {}).get('type', {}).get('code'), data.get('receiver_organisation', {}).get('narratives'), data.get('disbursement_channel', {}).get('code'), data.get('transaction_sector', {}).get('sector', {}).get('code', {}), data.get('transaction_sector', {}).get('vocabulary', {}).get('code', {}), data.get('transaction_sector', {}).get('vocabulary_uri', {}), data.get('transaction_recipient_country', {}).get('country', {}).get('code', {}), data.get('transaction_recipient_region', {}).get('region', {}).get('code', {}), data.get('transaction_recipient_region', {}).get('vocabulary', {}).get('code', {}), data.get('transaction_recipient_region', {}).get('vocabulary_uri'), data.get('flow_type', {}).get('code'), data.get('finance_type', {}).get('code'), data.get('aid_type', {}).get('code'), data.get('tied_status', {}).get('code'), ) return handle_errors(validated) def create(self, validated_data): activity = validated_data.get('activity') description_narratives_data = validated_data.pop('description_narratives', []) provider_data = validated_data.pop('provider_org') provider_narratives_data = validated_data.pop('provider_org_narratives', []) receiver_data = validated_data.pop('receiver_org') receiver_narratives_data = validated_data.pop('receiver_org_narratives', []) sector_data = validated_data.pop('sector') recipient_country_data = validated_data.pop('recipient_country') recipient_region_data = validated_data.pop('recipient_region') instance = models.Transaction.objects.create(**validated_data) if provider_data.get('ref'): provider_org = models.TransactionProvider.objects.create( transaction=instance, **provider_data) save_narratives(provider_org, provider_narratives_data, activity) validated_data['provider_organisation'] = provider_org if receiver_data.get('ref'): receiver_org = models.TransactionReceiver.objects.create( transaction=instance, **receiver_data) save_narratives(receiver_org, receiver_narratives_data, activity) validated_data['receiver_organisation'] = receiver_org if sector_data.get('sector'): models.TransactionSector.objects.create( transaction=instance, reported_transaction=instance, percentage=100, **sector_data ) if recipient_country_data.get('country'): models.TransactionRecipientCountry.objects.create( transaction=instance, reported_transaction=instance, percentage=100, **recipient_country_data ) if recipient_region_data.get('region'): models.TransactionRecipientRegion.objects.create( transaction=instance, reported_transaction=instance, percentage=100, **recipient_region_data ) return instance def update(self, instance, validated_data): activity = validated_data.get('activity') description_narratives_data = validated_data.pop('description_narratives', []) provider_data = validated_data.pop('provider_org') provider_narratives_data = validated_data.pop('provider_org_narratives', []) receiver_data = validated_data.pop('receiver_org') receiver_narratives_data = validated_data.pop('receiver_org_narratives', []) sector_data = validated_data.pop('sector') recipient_country_data = validated_data.pop('recipient_country') recipient_region_data = validated_data.pop('recipient_region') update_instance = models.Transaction(**validated_data) update_instance.id = instance.id update_instance.save() if provider_data.get('ref'): try: provider_org = models.TransactionProvider.objects.get( transaction=instance) except BaseException: provider_org = models.TransactionProvider.objects.create( transaction=instance, **provider_data) save_narratives(provider_org, provider_narratives_data, activity) validated_data['provider_organisation'] = provider_org if receiver_data.get('ref'): try: receiver_org = models.TransactionReceiver.objects.get( transaction=instance) except BaseException: receiver_org = models.TransactionReceiver.objects.create( transaction=instance, **receiver_data) save_narratives(receiver_org, receiver_narratives_data, activity) validated_data['receiver_organisation'] = receiver_org if sector_data.get('sector'): try: models.TransactionSector.objects.get( transaction=instance, reported_transaction=instance) except BaseException: models.TransactionSector.objects.create( transaction=instance, reported_transaction=instance, percentage=100, **sector_data ) if recipient_country_data.get('country'): try: models.TransactionRecipientCountry.objects.get( reported_transaction=instance, ) except BaseException: models.TransactionRecipientCountry.objects.create( transaction=instance, reported_transaction=instance, percentage=100, **recipient_country_data ) if recipient_region_data.get('region'): try: models.TransactionRecipientRegion.objects.get( transaction=instance, reported_transaction=instance) except BaseException: models.TransactionRecipientRegion.objects.create( transaction=instance, reported_transaction=instance, percentage=100, **recipient_region_data ) return update_instance