Skip to content

Commit 17a6a1a

Browse files
authored
Merge pull request #5773 from uktrade/TET-851-company-activity-search-great-data
Tet 851 company activity search great data
2 parents b941cc5 + b77aac2 commit 17a6a1a

15 files changed

+254
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 4.2.16 on 2024-11-12 11:50
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
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')]
9+
10+
dependencies = [
11+
('company_activity', '0012_companyactivity_great_and_more'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='greatexportenquiry',
17+
name='data_enquiry',
18+
field=models.TextField(),
19+
),
20+
migrations.RenameField(
21+
model_name='companyactivity',
22+
old_name='great',
23+
new_name='great_export_enquiry',
24+
),
25+
migrations.AlterField(
26+
model_name='companyactivity',
27+
name='activity_source',
28+
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),
29+
),
30+
migrations.AlterField(
31+
model_name='companyactivity',
32+
name='activity_source',
33+
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),
34+
),
35+
]

datahub/company_activity/models/company_activity.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ActivitySource(models.TextChoices):
2929
event = ('event', 'event')
3030
investment = ('investment', 'investment')
3131
order = ('order', 'order')
32-
great = ('great', 'great')
32+
great_export_enquiry = ('great_export_enquiry', 'great_export_enquiry')
3333

3434
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
3535
company = models.ForeignKey(
@@ -101,7 +101,7 @@ class ActivitySource(models.TextChoices):
101101
),
102102
)
103103

104-
great = models.ForeignKey(
104+
great_export_enquiry = models.ForeignKey(
105105
'company_activity.GreatExportEnquiry',
106106
unique=True,
107107
null=True,

datahub/company_activity/models/great.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class GreatExportEnquiry(models.Model):
6060
meta_email_address = models.CharField(max_length=MAX_LENGTH)
6161

6262
data_search = models.CharField(max_length=MAX_LENGTH)
63-
data_enquiry = models.CharField(max_length=MAX_LENGTH)
63+
data_enquiry = models.TextField()
6464
data_markets = models.ManyToManyField(
6565
metadata_models.Country,
6666
related_name='great_export_market_enquiries',
@@ -121,8 +121,8 @@ def save(self, *args, **kwargs):
121121
if not self.company_id:
122122
return
123123
CompanyActivity.objects.update_or_create(
124-
great_id=self.id,
125-
activity_source=CompanyActivity.ActivitySource.great,
124+
great_export_enquiry_id=self.id,
125+
activity_source=CompanyActivity.ActivitySource.great_export_enquiry,
126126
defaults={
127127
'date': self.created_on,
128128
'company_id': self.company_id,

datahub/company_activity/tasks/sync.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def relate_company_activity_to_great(batch_size=500):
142142
"""
143143
activity = set(
144144
CompanyActivity.objects.filter(
145-
great_id__isnull=False,
146-
).values_list('great_id', flat=True),
145+
great_export_enquiry_id__isnull=False,
146+
).values_list('great_export_enquiry_id', flat=True),
147147
)
148148

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

153153
objs = [
154154
CompanyActivity(
155-
great_id=great_export_enquiry['id'],
155+
great_export_enquiry_id=great_export_enquiry['id'],
156156
date=great_export_enquiry['created_on'],
157157
company_id=great_export_enquiry['company_id'],
158-
activity_source=CompanyActivity.ActivitySource.great,
158+
activity_source=CompanyActivity.ActivitySource.great_export_enquiry,
159159
)
160160
for great_export_enquiry in great_export_enquiries
161161
if great_export_enquiry['id'] not in activity

datahub/company_activity/tests/factories.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class GreatExportEnquiryFactory(factory.django.DjangoModelFactory):
166166
meta_email_address = 'meta@example.com'
167167

168168
data_search = ''
169-
data_enquiry = ''
169+
data_enquiry = factory.Faker('text')
170170
data_find_out_about = 'twitter'
171171
data_sector_primary = factory.SubFactory(SectorFactory)
172172
data_sector_primary_other = ''
@@ -194,6 +194,11 @@ class GreatExportEnquiryFactory(factory.django.DjangoModelFactory):
194194

195195
@factory.post_generation
196196
def set_markets(self, create, extracted, **kwargs):
197+
# Do not create markets if we are only building the factory without
198+
# creating an instance in the DB.
199+
if not create:
200+
return
201+
197202
self.data_markets.set([CountryFactory()])
198203

199204
class Meta:
@@ -205,8 +210,8 @@ class CompanyActivityGreatExportEnquiryFactory(CompanyActivityBaseFactory):
205210
CompanyActivity factory with an great export enquiry.
206211
"""
207212

208-
activity_source = CompanyActivity.ActivitySource.great
209-
great = factory.SubFactory(GreatExportEnquiryFactory)
213+
activity_source = CompanyActivity.ActivitySource.great_export_enquiry
214+
great_export_enquiry = factory.SubFactory(GreatExportEnquiryFactory)
210215

211216
class Meta:
212217
model = 'company_activity.CompanyActivity'
@@ -219,4 +224,4 @@ def _create(cls, model_class, *args, **kwargs):
219224
model save.
220225
"""
221226
obj = model_class(*args, **kwargs)
222-
return CompanyActivity.objects.get(great_id=obj.great_id)
227+
return CompanyActivity.objects.get(great_export_enquiry_id=obj.great_export_enquiry_id)

datahub/company_activity/tests/models/test_great_export_enquiry.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@ def test_save(self):
1515
`CompanyActivity` model if it already exists.
1616
"""
1717
assert not CompanyActivity.objects.all().exists()
18-
great = GreatExportEnquiryFactory()
18+
great_export_enquiry = GreatExportEnquiryFactory()
1919
assert CompanyActivity.objects.all().count() == 1
2020

21-
company_activity = CompanyActivity.objects.get(great_id=great.id)
22-
assert company_activity.company_id == great.company_id
23-
assert company_activity.date == great.created_on
24-
assert company_activity.activity_source == CompanyActivity.ActivitySource.great
21+
company_activity = CompanyActivity.objects.get(
22+
great_export_enquiry_id=great_export_enquiry.id,
23+
)
24+
assert company_activity.company_id == great_export_enquiry.company_id
25+
assert company_activity.date == great_export_enquiry.created_on
26+
assert (
27+
company_activity.activity_source == CompanyActivity.ActivitySource.great_export_enquiry
28+
)
2529

2630
# Update and save the great export enquiry and ensure if doesn't create another
2731
# `CompanyActivity` and only updates it
2832
new_company = CompanyFactory()
29-
great.company_id = new_company.id
30-
great.save()
33+
great_export_enquiry.company_id = new_company.id
34+
great_export_enquiry.save()
3135

3236
assert CompanyActivity.objects.all().count() == 1
3337
company_activity.refresh_from_db()
3438
assert company_activity.company_id == new_company.id
3539

36-
great.delete()
40+
great_export_enquiry.delete()
3741
assert not CompanyActivity.objects.all().exists()
3842

3943
def test_save_with_no_company(self):

datahub/company_activity/tests/test_tasks/test_great_task.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_great_export_enquiry_are_copied_to_company_activity(self):
2020
"""
2121
Test that great export enquiry are added to the CompanyActivity model.
2222
"""
23-
great = GreatExportEnquiryFactory()
23+
great_export_enquiry = GreatExportEnquiryFactory()
2424
GreatExportEnquiryFactory.create_batch(3)
2525

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

35-
company_activity = CompanyActivity.objects.get(great_id=great.id)
36-
assert company_activity.date == great.created_on
37-
assert company_activity.activity_source == CompanyActivity.ActivitySource.great
38-
assert company_activity.company_id == great.company.id
35+
company_activity = CompanyActivity.objects.get(
36+
great_export_enquiry_id=great_export_enquiry.id,
37+
)
38+
assert company_activity.date == great_export_enquiry.created_on
39+
assert (
40+
company_activity.activity_source == CompanyActivity.ActivitySource.great_export_enquiry
41+
)
42+
assert company_activity.company_id == great_export_enquiry.company.id
3943

4044
@mock.patch('datahub.company_activity.models.CompanyActivity.objects.bulk_create')
4145
def test_great_export_enquiry_are_bulk_created_in_batches(self, mocked_bulk_create, caplog):

datahub/search/company_activity/apps.py

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class CompanyActivitySearchApp(SearchApp):
2929
'order__primary_market',
3030
'order__uk_region',
3131
'order__created_by',
32+
'great_export_enquiry',
33+
'great_export_enquiry__contact',
3234
).prefetch_related(
3335
'interaction__contacts',
3436
Prefetch(

datahub/search/company_activity/dict_utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,19 @@ def activity_order_dict(obj):
6969
'contact': dict_utils.contact_job_dict(obj.contact),
7070
'created_by': dict_utils.contact_or_adviser_dict(obj.created_by),
7171
}
72+
73+
74+
def activity_great_dict(obj):
75+
"""Creates dictionary from an great export enquiry"""
76+
if obj is None:
77+
return None
78+
79+
return {
80+
'id': str(obj.id),
81+
'created_on': obj.created_on,
82+
'meta_full_name': obj.meta_full_name,
83+
'meta_email_address': obj.meta_email_address,
84+
'contact': dict_utils.contact_job_dict(obj.contact),
85+
'meta_subject': obj.meta_subject,
86+
'data_enquiry': obj.data_enquiry,
87+
}

datahub/search/company_activity/fields.py

+14
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,17 @@ def activity_order_field():
6464
'created_by': fields.contact_or_adviser_field(),
6565
},
6666
)
67+
68+
69+
def activity_great_field():
70+
return Object(
71+
properties={
72+
'id': Keyword(),
73+
'created_on': Date(),
74+
'meta_full_name': Text(index=False),
75+
'meta_email_address': Text(index=False),
76+
'contact': fields.contact_job_field(),
77+
'meta_subject': Text(index=False),
78+
'data_enquiry': Text(index=False),
79+
},
80+
)

datahub/search/company_activity/models.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
from datahub.search import dict_utils, fields
44
from datahub.search.company_activity.dict_utils import (
5+
activity_great_dict,
56
activity_interaction_dict,
67
activity_investment_dict,
78
activity_order_dict,
89
activity_referral_dict,
910
)
1011
from datahub.search.company_activity.fields import (
12+
activity_great_field,
1113
activity_interaction_field,
1214
activity_investment_field,
1315
activity_order_field,
@@ -29,6 +31,7 @@ class CompanyActivity(BaseSearchModel):
2931
referral = activity_referral_field()
3032
investment = activity_investment_field()
3133
order = activity_order_field()
34+
great_export_enquiry = activity_great_field()
3235

3336
COMPUTED_MAPPINGS = {}
3437

@@ -38,6 +41,7 @@ class CompanyActivity(BaseSearchModel):
3841
'company': dict_utils.company_dict,
3942
'investment': activity_investment_dict,
4043
'order': activity_order_dict,
44+
'great_export_enquiry': activity_great_dict,
4145
}
4246

4347
SEARCH_FIELDS = (

datahub/search/company_activity/signals.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Company as DBCompany,
66
)
77
from datahub.company_activity.models import CompanyActivity as DBCompanyActivity
8+
from datahub.company_activity.models import GreatExportEnquiry as DBGreatExportEnquiry
89
from datahub.company_referral.models import CompanyReferral as DBCompanyReferral
910
from datahub.interaction.models import (
1011
Interaction as DBInteraction,
@@ -54,15 +55,11 @@ def remove_interaction_from_opensearch(instance):
5455

5556
receivers = (
5657
SignalReceiver(post_save, DBCompanyActivity, sync_activity_to_opensearch),
57-
SignalReceiver(post_save, DBCompany,
58-
sync_related_activities_to_opensearch),
59-
SignalReceiver(post_save, DBInteraction,
60-
sync_related_activity_to_opensearch),
61-
SignalReceiver(post_save, DBCompanyReferral,
62-
sync_related_activity_to_opensearch),
58+
SignalReceiver(post_save, DBCompany, sync_related_activities_to_opensearch),
59+
SignalReceiver(post_save, DBInteraction, sync_related_activity_to_opensearch),
60+
SignalReceiver(post_save, DBCompanyReferral, sync_related_activity_to_opensearch),
6361
SignalReceiver(post_save, DBOrder, sync_related_activity_to_opensearch),
64-
SignalReceiver(post_save, DBInvestmentProject,
65-
sync_related_activity_to_opensearch),
66-
SignalReceiver(post_delete, DBCompanyActivity,
67-
remove_interaction_from_opensearch),
62+
SignalReceiver(post_save, DBGreatExportEnquiry, sync_related_activity_to_opensearch),
63+
SignalReceiver(post_save, DBInvestmentProject, sync_related_activity_to_opensearch),
64+
SignalReceiver(post_delete, DBCompanyActivity, remove_interaction_from_opensearch),
6865
)

datahub/search/company_activity/test/test_dict_utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from datahub.company_activity.tests.factories import GreatExportEnquiryFactory
34
from datahub.company_referral.test.factories import CompanyReferralFactory
45
from datahub.interaction.test.factories import CompanyInteractionFactory
56
from datahub.investment.project.test.factories import InvestmentProjectFactory
@@ -71,3 +72,20 @@ def test_activity_order_dict():
7172
assert result['created_by']['id'] == str(order.created_by_id)
7273
assert result['contact']['id'] == str(order.contact_id)
7374
assert result['primary_market']['id'] == order.primary_market_id
75+
76+
77+
def test_activity_great_dict():
78+
obj = None
79+
result = dict_utils.activity_great_dict(obj)
80+
assert result is None
81+
82+
great = GreatExportEnquiryFactory()
83+
result = dict_utils.activity_great_dict(great)
84+
85+
assert result['id'] == str(great.id)
86+
assert result['created_on'] == great.created_on
87+
assert result['meta_full_name'] == great.meta_full_name
88+
assert result['meta_email_address'] == great.meta_email_address
89+
assert result['contact']['id'] == str(great.contact.id)
90+
assert result['meta_subject'] == great.meta_subject
91+
assert result['data_enquiry'] == great.data_enquiry

0 commit comments

Comments
 (0)