Skip to content

Commit

Permalink
Merge pull request #5773 from uktrade/TET-851-company-activity-search…
Browse files Browse the repository at this point in the history
…-great-data

Tet 851 company activity search great data
  • Loading branch information
bau123 authored Nov 14, 2024
2 parents b941cc5 + b77aac2 commit 17a6a1a
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.16 on 2024-11-12 11:50

from django.db import migrations, models


class Migration(migrations.Migration):

replaces = [('company_activity', '0013_alter_greatexportenquiry_data_enquiry'), ('company_activity', '0014_rename_great_companyactivity_great_export_enquiry_and_more'), ('company_activity', '0015_alter_companyactivity_activity_source')]

dependencies = [
('company_activity', '0012_companyactivity_great_and_more'),
]

operations = [
migrations.AlterField(
model_name='greatexportenquiry',
name='data_enquiry',
field=models.TextField(),
),
migrations.RenameField(
model_name='companyactivity',
old_name='great',
new_name='great_export_enquiry',
),
migrations.AlterField(
model_name='companyactivity',
name='activity_source',
field=models.CharField(choices=[('interaction', 'interaction'), ('referral', 'referral'), ('event', 'event'), ('investment', 'investment'), ('order', 'order'), ('greatExportEnquiry', 'greatExportEnquiry')], help_text='The type of company activity, such as an interaction, event, referral etc.', max_length=255),
),
migrations.AlterField(
model_name='companyactivity',
name='activity_source',
field=models.CharField(choices=[('interaction', 'interaction'), ('referral', 'referral'), ('event', 'event'), ('investment', 'investment'), ('order', 'order'), ('great_export_enquiry', 'great_export_enquiry')], help_text='The type of company activity, such as an interaction, event, referral etc.', max_length=255),
),
]
4 changes: 2 additions & 2 deletions datahub/company_activity/models/company_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ActivitySource(models.TextChoices):
event = ('event', 'event')
investment = ('investment', 'investment')
order = ('order', 'order')
great = ('great', 'great')
great_export_enquiry = ('great_export_enquiry', 'great_export_enquiry')

id = models.UUIDField(primary_key=True, default=uuid.uuid4)
company = models.ForeignKey(
Expand Down Expand Up @@ -101,7 +101,7 @@ class ActivitySource(models.TextChoices):
),
)

great = models.ForeignKey(
great_export_enquiry = models.ForeignKey(
'company_activity.GreatExportEnquiry',
unique=True,
null=True,
Expand Down
6 changes: 3 additions & 3 deletions datahub/company_activity/models/great.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class GreatExportEnquiry(models.Model):
meta_email_address = models.CharField(max_length=MAX_LENGTH)

data_search = models.CharField(max_length=MAX_LENGTH)
data_enquiry = models.CharField(max_length=MAX_LENGTH)
data_enquiry = models.TextField()
data_markets = models.ManyToManyField(
metadata_models.Country,
related_name='great_export_market_enquiries',
Expand Down Expand Up @@ -121,8 +121,8 @@ def save(self, *args, **kwargs):
if not self.company_id:
return
CompanyActivity.objects.update_or_create(
great_id=self.id,
activity_source=CompanyActivity.ActivitySource.great,
great_export_enquiry_id=self.id,
activity_source=CompanyActivity.ActivitySource.great_export_enquiry,
defaults={
'date': self.created_on,
'company_id': self.company_id,
Expand Down
8 changes: 4 additions & 4 deletions datahub/company_activity/tasks/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def relate_company_activity_to_great(batch_size=500):
"""
activity = set(
CompanyActivity.objects.filter(
great_id__isnull=False,
).values_list('great_id', flat=True),
great_export_enquiry_id__isnull=False,
).values_list('great_export_enquiry_id', flat=True),
)

great_export_enquiries = GreatExportEnquiry.objects.filter(
Expand All @@ -152,10 +152,10 @@ def relate_company_activity_to_great(batch_size=500):

objs = [
CompanyActivity(
great_id=great_export_enquiry['id'],
great_export_enquiry_id=great_export_enquiry['id'],
date=great_export_enquiry['created_on'],
company_id=great_export_enquiry['company_id'],
activity_source=CompanyActivity.ActivitySource.great,
activity_source=CompanyActivity.ActivitySource.great_export_enquiry,
)
for great_export_enquiry in great_export_enquiries
if great_export_enquiry['id'] not in activity
Expand Down
13 changes: 9 additions & 4 deletions datahub/company_activity/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class GreatExportEnquiryFactory(factory.django.DjangoModelFactory):
meta_email_address = 'meta@example.com'

data_search = ''
data_enquiry = ''
data_enquiry = factory.Faker('text')
data_find_out_about = 'twitter'
data_sector_primary = factory.SubFactory(SectorFactory)
data_sector_primary_other = ''
Expand Down Expand Up @@ -194,6 +194,11 @@ class GreatExportEnquiryFactory(factory.django.DjangoModelFactory):

@factory.post_generation
def set_markets(self, create, extracted, **kwargs):
# Do not create markets if we are only building the factory without
# creating an instance in the DB.
if not create:
return

self.data_markets.set([CountryFactory()])

class Meta:
Expand All @@ -205,8 +210,8 @@ class CompanyActivityGreatExportEnquiryFactory(CompanyActivityBaseFactory):
CompanyActivity factory with an great export enquiry.
"""

activity_source = CompanyActivity.ActivitySource.great
great = factory.SubFactory(GreatExportEnquiryFactory)
activity_source = CompanyActivity.ActivitySource.great_export_enquiry
great_export_enquiry = factory.SubFactory(GreatExportEnquiryFactory)

class Meta:
model = 'company_activity.CompanyActivity'
Expand All @@ -219,4 +224,4 @@ def _create(cls, model_class, *args, **kwargs):
model save.
"""
obj = model_class(*args, **kwargs)
return CompanyActivity.objects.get(great_id=obj.great_id)
return CompanyActivity.objects.get(great_export_enquiry_id=obj.great_export_enquiry_id)
20 changes: 12 additions & 8 deletions datahub/company_activity/tests/models/test_great_export_enquiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@ def test_save(self):
`CompanyActivity` model if it already exists.
"""
assert not CompanyActivity.objects.all().exists()
great = GreatExportEnquiryFactory()
great_export_enquiry = GreatExportEnquiryFactory()
assert CompanyActivity.objects.all().count() == 1

company_activity = CompanyActivity.objects.get(great_id=great.id)
assert company_activity.company_id == great.company_id
assert company_activity.date == great.created_on
assert company_activity.activity_source == CompanyActivity.ActivitySource.great
company_activity = CompanyActivity.objects.get(
great_export_enquiry_id=great_export_enquiry.id,
)
assert company_activity.company_id == great_export_enquiry.company_id
assert company_activity.date == great_export_enquiry.created_on
assert (
company_activity.activity_source == CompanyActivity.ActivitySource.great_export_enquiry
)

# Update and save the great export enquiry and ensure if doesn't create another
# `CompanyActivity` and only updates it
new_company = CompanyFactory()
great.company_id = new_company.id
great.save()
great_export_enquiry.company_id = new_company.id
great_export_enquiry.save()

assert CompanyActivity.objects.all().count() == 1
company_activity.refresh_from_db()
assert company_activity.company_id == new_company.id

great.delete()
great_export_enquiry.delete()
assert not CompanyActivity.objects.all().exists()

def test_save_with_no_company(self):
Expand Down
14 changes: 9 additions & 5 deletions datahub/company_activity/tests/test_tasks/test_great_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_great_export_enquiry_are_copied_to_company_activity(self):
"""
Test that great export enquiry are added to the CompanyActivity model.
"""
great = GreatExportEnquiryFactory()
great_export_enquiry = GreatExportEnquiryFactory()
GreatExportEnquiryFactory.create_batch(3)

# Remove the created CompanyActivities added by the Great model `save` method
Expand All @@ -32,10 +32,14 @@ def test_great_export_enquiry_are_copied_to_company_activity(self):
schedule_sync_data_to_company_activity(relate_company_activity_to_great)
assert CompanyActivity.objects.count() == 4

company_activity = CompanyActivity.objects.get(great_id=great.id)
assert company_activity.date == great.created_on
assert company_activity.activity_source == CompanyActivity.ActivitySource.great
assert company_activity.company_id == great.company.id
company_activity = CompanyActivity.objects.get(
great_export_enquiry_id=great_export_enquiry.id,
)
assert company_activity.date == great_export_enquiry.created_on
assert (
company_activity.activity_source == CompanyActivity.ActivitySource.great_export_enquiry
)
assert company_activity.company_id == great_export_enquiry.company.id

@mock.patch('datahub.company_activity.models.CompanyActivity.objects.bulk_create')
def test_great_export_enquiry_are_bulk_created_in_batches(self, mocked_bulk_create, caplog):
Expand Down
2 changes: 2 additions & 0 deletions datahub/search/company_activity/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CompanyActivitySearchApp(SearchApp):
'order__primary_market',
'order__uk_region',
'order__created_by',
'great_export_enquiry',
'great_export_enquiry__contact',
).prefetch_related(
'interaction__contacts',
Prefetch(
Expand Down
16 changes: 16 additions & 0 deletions datahub/search/company_activity/dict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ def activity_order_dict(obj):
'contact': dict_utils.contact_job_dict(obj.contact),
'created_by': dict_utils.contact_or_adviser_dict(obj.created_by),
}


def activity_great_dict(obj):
"""Creates dictionary from an great export enquiry"""
if obj is None:
return None

return {
'id': str(obj.id),
'created_on': obj.created_on,
'meta_full_name': obj.meta_full_name,
'meta_email_address': obj.meta_email_address,
'contact': dict_utils.contact_job_dict(obj.contact),
'meta_subject': obj.meta_subject,
'data_enquiry': obj.data_enquiry,
}
14 changes: 14 additions & 0 deletions datahub/search/company_activity/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ def activity_order_field():
'created_by': fields.contact_or_adviser_field(),
},
)


def activity_great_field():
return Object(
properties={
'id': Keyword(),
'created_on': Date(),
'meta_full_name': Text(index=False),
'meta_email_address': Text(index=False),
'contact': fields.contact_job_field(),
'meta_subject': Text(index=False),
'data_enquiry': Text(index=False),
},
)
4 changes: 4 additions & 0 deletions datahub/search/company_activity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from datahub.search import dict_utils, fields
from datahub.search.company_activity.dict_utils import (
activity_great_dict,
activity_interaction_dict,
activity_investment_dict,
activity_order_dict,
activity_referral_dict,
)
from datahub.search.company_activity.fields import (
activity_great_field,
activity_interaction_field,
activity_investment_field,
activity_order_field,
Expand All @@ -29,6 +31,7 @@ class CompanyActivity(BaseSearchModel):
referral = activity_referral_field()
investment = activity_investment_field()
order = activity_order_field()
great_export_enquiry = activity_great_field()

COMPUTED_MAPPINGS = {}

Expand All @@ -38,6 +41,7 @@ class CompanyActivity(BaseSearchModel):
'company': dict_utils.company_dict,
'investment': activity_investment_dict,
'order': activity_order_dict,
'great_export_enquiry': activity_great_dict,
}

SEARCH_FIELDS = (
Expand Down
17 changes: 7 additions & 10 deletions datahub/search/company_activity/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Company as DBCompany,
)
from datahub.company_activity.models import CompanyActivity as DBCompanyActivity
from datahub.company_activity.models import GreatExportEnquiry as DBGreatExportEnquiry
from datahub.company_referral.models import CompanyReferral as DBCompanyReferral
from datahub.interaction.models import (
Interaction as DBInteraction,
Expand Down Expand Up @@ -54,15 +55,11 @@ def remove_interaction_from_opensearch(instance):

receivers = (
SignalReceiver(post_save, DBCompanyActivity, sync_activity_to_opensearch),
SignalReceiver(post_save, DBCompany,
sync_related_activities_to_opensearch),
SignalReceiver(post_save, DBInteraction,
sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBCompanyReferral,
sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBCompany, sync_related_activities_to_opensearch),
SignalReceiver(post_save, DBInteraction, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBCompanyReferral, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBOrder, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBInvestmentProject,
sync_related_activity_to_opensearch),
SignalReceiver(post_delete, DBCompanyActivity,
remove_interaction_from_opensearch),
SignalReceiver(post_save, DBGreatExportEnquiry, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBInvestmentProject, sync_related_activity_to_opensearch),
SignalReceiver(post_delete, DBCompanyActivity, remove_interaction_from_opensearch),
)
18 changes: 18 additions & 0 deletions datahub/search/company_activity/test/test_dict_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from datahub.company_activity.tests.factories import GreatExportEnquiryFactory
from datahub.company_referral.test.factories import CompanyReferralFactory
from datahub.interaction.test.factories import CompanyInteractionFactory
from datahub.investment.project.test.factories import InvestmentProjectFactory
Expand Down Expand Up @@ -71,3 +72,20 @@ def test_activity_order_dict():
assert result['created_by']['id'] == str(order.created_by_id)
assert result['contact']['id'] == str(order.contact_id)
assert result['primary_market']['id'] == order.primary_market_id


def test_activity_great_dict():
obj = None
result = dict_utils.activity_great_dict(obj)
assert result is None

great = GreatExportEnquiryFactory()
result = dict_utils.activity_great_dict(great)

assert result['id'] == str(great.id)
assert result['created_on'] == great.created_on
assert result['meta_full_name'] == great.meta_full_name
assert result['meta_email_address'] == great.meta_email_address
assert result['contact']['id'] == str(great.contact.id)
assert result['meta_subject'] == great.meta_subject
assert result['data_enquiry'] == great.data_enquiry
Loading

0 comments on commit 17a6a1a

Please sign in to comment.