From ff1984e4671419d508e9e6ceae151107141f12b1 Mon Sep 17 00:00:00 2001 From: sudan45 Date: Wed, 27 Dec 2023 16:01:13 +0545 Subject: [PATCH] serializer validation and save data in connector --- apps/deepl_integration/handlers.py | 1 + apps/deepl_integration/serializers.py | 9 ++------- apps/unified_connector/admin.py | 7 +++++++ apps/unified_connector/tests/test_mutation.py | 16 +++++----------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/apps/deepl_integration/handlers.py b/apps/deepl_integration/handlers.py index d26b19cbaf..0db9867479 100644 --- a/apps/deepl_integration/handlers.py +++ b/apps/deepl_integration/handlers.py @@ -668,6 +668,7 @@ def save_lead_data_using_connector_lead( text_extract=connector_lead.simplified_text, word_count=connector_lead.word_count, page_count=connector_lead.page_count, + text_extraction_id=connector_lead.text_extraction_id ) # Save extracted images as LeadPreviewImage instances # TODO: The logic is same for unified_connector leads as well. Maybe have a single func? diff --git a/apps/deepl_integration/serializers.py b/apps/deepl_integration/serializers.py index b55f3db590..2ee2812ab7 100644 --- a/apps/deepl_integration/serializers.py +++ b/apps/deepl_integration/serializers.py @@ -116,7 +116,6 @@ class UnifiedConnectorLeadExtractCallbackSerializer(DeeplServerBaseCallbackSeria """ Serialize deepl extractor """ - url = serializers.CharField() # Data fields images_path = serializers.ListField( child=serializers.CharField(allow_blank=True), @@ -125,20 +124,16 @@ class UnifiedConnectorLeadExtractCallbackSerializer(DeeplServerBaseCallbackSeria text_path = serializers.CharField(required=False, allow_null=True) total_words_count = serializers.IntegerField(required=False, default=0, allow_null=True) total_pages = serializers.IntegerField(required=False, default=0, allow_null=True) - text_extraction_id = serializers.CharField(required=False, allow_null=True) + text_extraction_id = serializers.UUIDField(required=False, allow_null=True) nlp_handler = UnifiedConnectorLeadHandler def validate(self, data): data = super().validate(data) - if data['object'].url != data['url']: - raise serializers.ValidationError({ - 'url': 'Different url found provided vs original connector lead', - }) if data['status'] == self.Status.SUCCESS: errors = {} for key in ['text_path', 'total_words_count', 'total_pages', 'text_extraction_id']: - if key not in data: + if key not in data or data[key] is None: errors[key] = f'<{key}> is missing. Required when the extraction is Success' if errors: raise serializers.ValidationError(errors) diff --git a/apps/unified_connector/admin.py b/apps/unified_connector/admin.py index cdc6d1a94a..e2a2778fc6 100644 --- a/apps/unified_connector/admin.py +++ b/apps/unified_connector/admin.py @@ -25,4 +25,11 @@ class ConnectorSourceLeadAdmin(admin.ModelAdmin): @admin.register(ConnectorLead) class ConnectorLeadAdmin(admin.ModelAdmin): + list_display = [ + 'id', + 'title', + 'created_at' + ] + readonly_fields = ('created_at',) + autocomplete_fields = ('authors', 'source',) search_fields = ('title',) diff --git a/apps/unified_connector/tests/test_mutation.py b/apps/unified_connector/tests/test_mutation.py index eb4ad89b48..73abefed26 100644 --- a/apps/unified_connector/tests/test_mutation.py +++ b/apps/unified_connector/tests/test_mutation.py @@ -492,7 +492,6 @@ def _check_connector_lead_status(connector_lead, status): # ------ Extraction FAILED data = dict( client_id='some-random-client-id', - url='https://example.com/some-random-url', images_path=['https://example.com/sample-file-1.jpg'], text_path='https://example.com/url-where-data-is-fetched-from-mock-response', total_words_count=100, @@ -507,20 +506,15 @@ def _check_connector_lead_status(connector_lead, status): _check_connector_lead_status(connector_lead1, ConnectorLead.ExtractionStatus.PENDING) data['client_id'] = UnifiedConnectorLeadHandler.get_client_id(connector_lead1) - response = self.client.post(url, data) - self.assert_400(response) - _check_connector_lead_status(connector_lead1, ConnectorLead.ExtractionStatus.PENDING) - - data['url'] = connector_lead1.url + data['status'] = DeeplServerBaseCallbackSerializer.Status.SUCCESS.value response = self.client.post(url, data) self.assert_200(response) connector_lead1.refresh_from_db() - _check_connector_lead_status(connector_lead1, ConnectorLead.ExtractionStatus.FAILED) + _check_connector_lead_status(connector_lead1, ConnectorLead.ExtractionStatus.SUCCESS) # ------ Extraction SUCCESS data = dict( client_id='some-random-client-id', - url='https://example.com/some-random-url', images_path=['https://example.com/sample-file-1.jpg', 'https://example.com/sample-file-2.jpg'], text_path='https://example.com/url-where-data-is-fetched-from-mock-response', total_words_count=100, @@ -534,8 +528,8 @@ def _check_connector_lead_status(connector_lead, status): data['client_id'] = UnifiedConnectorLeadHandler.get_client_id(connector_lead2) response = self.client.post(url, data) - self.assert_400(response) - _check_connector_lead_status(connector_lead2, ConnectorLead.ExtractionStatus.PENDING) + self.assert_200(response) + _check_connector_lead_status(connector_lead2, ConnectorLead.ExtractionStatus.SUCCESS) data['url'] = connector_lead2.url response = self.client.post(url, data) @@ -545,5 +539,5 @@ def _check_connector_lead_status(connector_lead, status): preview_image_qs = ConnectorLeadPreviewImage.objects.filter(connector_lead=connector_lead2) preview_image = preview_image_qs.first() self.assertEqual(connector_lead2.simplified_text, SAMPLE_SIMPLIFIED_TEXT) - self.assertEqual(preview_image_qs.count(), 2) + self.assertEqual(preview_image_qs.count(), 4) self.assertIsNotNone(preview_image and preview_image.image.name)