Skip to content

Commit

Permalink
serializer validation and save data in connector
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Dec 27, 2023
1 parent 7538859 commit ff1984e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions apps/deepl_integration/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
9 changes: 2 additions & 7 deletions apps/deepl_integration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions apps/unified_connector/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',)
16 changes: 5 additions & 11 deletions apps/unified_connector/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit ff1984e

Please sign in to comment.