Skip to content

Commit 445a5f0

Browse files
authored
Merge pull request #339 from tutorcruncher/flag-on-mass-con-create
process images flag on mass contractor create
2 parents 8143f35 + 94246ca commit 445a5f0

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

tcsocket/app/views/contractor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async def contractor_set_mass(request):
4646
Create or update all of companies contractors
4747
"""
4848
data = await request.json()
49+
process_images = data.pop('process_images', True)
4950
conn = await request['conn_manager'].get_connection()
5051
company = request['company']
5152
contractors = [ContractorModel(**con_data) for con_data in data['contractors']]
@@ -54,7 +55,8 @@ async def contractor_set_mass(request):
5455

5556
# starting image processing here due to conflicting db connections on tests
5657
redis = request.app['redis']
57-
if con_details := {(contractor.id, contractor.photo) for contractor in contractors if contractor.photo}:
58+
con_details = {(contractor.id, contractor.photo) for contractor in contractors if contractor.photo}
59+
if process_images and con_details:
5860
await redis.enqueue_job('process_image_mass', company_key=company['public_key'], con_details=con_details)
5961
return json_response(request, status='success')
6062

tests/test_set_contractor.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,115 @@ async def test_mass_contractor_create(cli, db_conn, company, image_download_url,
788788
assert result.id == 369
789789
assert result.first_name == 'Jim'
790790
assert result.last_name == 'Bell'
791+
792+
793+
async def test_mass_contractor_process_images_false(cli, db_conn, other_server, company, image_download_url, worker):
794+
data = {'contractors': [], 'process_images': False}
795+
eas = [
796+
{
797+
'machine_name': 'terms',
798+
'type': 'checkbox',
799+
'name': 'Terms and Conditions agreement',
800+
'value': True,
801+
'sort_index': 0,
802+
},
803+
{'machine_name': 'bio', 'type': 'integer', 'name': 'Teaching Experience', 'value': 123, 'sort_index': 0.123},
804+
{'machine_name': 'date', 'type': 'date', 'name': 'The Date', 'value': '2032-06-01', 'sort_index': 0.123},
805+
]
806+
for i in range(1, 3):
807+
data['contractors'].append(
808+
dict(
809+
id=123 * i,
810+
first_name='Fred',
811+
skills=[
812+
{
813+
'subject_id': 1,
814+
'qual_level_id': 1,
815+
'qual_level': 'GCSE',
816+
'subject': 'Algebra',
817+
'qual_level_ranking': 16.0,
818+
'category': 'Maths',
819+
},
820+
{
821+
'subject_id': 2,
822+
'qual_level_id': 1,
823+
'qual_level': 'GCSE',
824+
'subject': 'Language',
825+
'qual_level_ranking': 16.0,
826+
'category': 'English',
827+
},
828+
],
829+
location=dict(latitude=12.345, longitude=56.789),
830+
review_rating=3.5,
831+
review_duration=7200,
832+
labels=[{'machine_name': 'foobar', 'name': 'Foobar'}],
833+
photo=f'{image_download_url}?format=JPEG',
834+
extra_attributes=eas,
835+
)
836+
)
837+
r = await signed_request(
838+
cli, f'/{company.public_key}/webhook/contractor/mass', signing_key_='this is the master key', **data
839+
)
840+
assert r.status == 200
841+
assert {'status': 'success'} == await r.json()
842+
assert 2 == await count(db_conn, sa_contractors)
843+
await worker.run_check()
844+
assert other_server.app['request_log'] == []
845+
846+
847+
async def test_mass_contractor_process_images_true(
848+
cli, db_conn, other_server, company, image_download_url, monkeypatch, tmpdir, worker
849+
):
850+
monkeypatch.setattr(boto3, 'client', fake_s3_client(tmpdir))
851+
852+
data = {'contractors': [], 'process_images': True}
853+
eas = [
854+
{
855+
'machine_name': 'terms',
856+
'type': 'checkbox',
857+
'name': 'Terms and Conditions agreement',
858+
'value': True,
859+
'sort_index': 0,
860+
},
861+
{'machine_name': 'bio', 'type': 'integer', 'name': 'Teaching Experience', 'value': 123, 'sort_index': 0.123},
862+
{'machine_name': 'date', 'type': 'date', 'name': 'The Date', 'value': '2032-06-01', 'sort_index': 0.123},
863+
]
864+
for i in range(1, 3):
865+
data['contractors'].append(
866+
dict(
867+
id=123 * i,
868+
first_name='Fred',
869+
skills=[
870+
{
871+
'subject_id': 1,
872+
'qual_level_id': 1,
873+
'qual_level': 'GCSE',
874+
'subject': 'Algebra',
875+
'qual_level_ranking': 16.0,
876+
'category': 'Maths',
877+
},
878+
{
879+
'subject_id': 2,
880+
'qual_level_id': 1,
881+
'qual_level': 'GCSE',
882+
'subject': 'Language',
883+
'qual_level_ranking': 16.0,
884+
'category': 'English',
885+
},
886+
],
887+
location=dict(latitude=12.345, longitude=56.789),
888+
review_rating=3.5,
889+
review_duration=7200,
890+
labels=[{'machine_name': 'foobar', 'name': 'Foobar'}],
891+
photo=f'{image_download_url}?format=JPEG',
892+
extra_attributes=eas,
893+
)
894+
)
895+
r = await signed_request(
896+
cli, f'/{company.public_key}/webhook/contractor/mass', signing_key_='this is the master key', **data
897+
)
898+
assert r.status == 200
899+
assert {'status': 'success'} == await r.json()
900+
assert 2 == await count(db_conn, sa_contractors)
901+
await worker.run_check()
902+
assert other_server.app['request_log'] == [('test_image', 'JPEG'), ('test_image', 'JPEG')]

0 commit comments

Comments
 (0)