def handle(self, *args, **options): queryset = models.Company.objects.filter( Q(company_type=company_types.COMPANIES_HOUSE) & ( Q(date_of_creation__isnull=True) | Q(address_line_1__isnull=True) | Q(address_line_1='') ) ) failed = 0 succeded = 0 for company in queryset: try: profile = helpers.get_companies_house_profile(company.number) if profile.get('date_of_creation'): company.date_of_creation = datetime.strptime( profile['date_of_creation'], '%Y-%m-%d' ) if profile.get('registered_office_address'): address = profile['registered_office_address'] company.address_line_1 = address.get('address_line_1', '') company.address_line_2 = address.get('address_line_2', '') company.locality = address.get('locality', '') company.po_box = address.get('po_box', '') company.postal_code = address.get('postal_code', '') company.save() message = f'Company {company.name} updated' self.stdout.write(self.style.SUCCESS(message)) succeded += 1 except Exception as e: self.stdout.write(self.style.ERROR(e)) failed += 1 self.stdout.write(self.style.SUCCESS(f'{succeded} companies updated')) self.stdout.write(self.style.WARNING(f'{failed} companies failed'))
def test_get_companies_house_profile(): profile = {'company_status': 'active'} with requests_mock.mock() as mock: mock.get('https://api.companieshouse.gov.uk/company/01234567', status_code=http.client.OK, json=profile) response = helpers.get_companies_house_profile('01234567') assert response.json() == profile
def handle(self, *args, **options): queryset = models.Company.objects.filter( company_type=company_types.COMPANIES_HOUSE) failed = 0 succeded = 0 for company in queryset: try: profile = helpers.get_companies_house_profile(company.number) if profile.get('company_status'): company.companies_house_company_status = profile.get( 'company_status') else: company.companies_house_company_status = '' company.save() message = f'Company {company.name} updated' self.stdout.write(self.style.SUCCESS(message)) succeded += 1 except Exception as e: self.stdout.write(self.style.ERROR(e)) failed += 1 self.stdout.write(self.style.SUCCESS(f'{succeded} companies updated')) self.stdout.write(self.style.WARNING(f'{failed} companies failed'))
def test_get_profile_response_ok(mock_retrieve_profile): mock_retrieve_profile.return_value = profile_api_200() result = helpers.get_companies_house_profile('01234567') mock_retrieve_profile.assert_called_once_with(number='01234567') assert result == {'date_of_creation': '1987-12-31'}
def test_get_companies_house_profile_response_bad(mock_retrieve_profile): mock_retrieve_profile.return_value = profile_api_400() with pytest.raises(HTTPError): helpers.get_companies_house_profile('01234567')